我是靠谱客的博主 顺利硬币,这篇文章主要介绍Spring MVC如何使用@Autowired及@Service进行依赖注入呢?,现在分享给大家,希望可以做个参考。

转自:

Spring MVC如何使用@Autowired及@Service进行依赖注入呢?

 Spring MVC 框架本身拥有依赖注入的特性
   我们可以使用org.springframework.beans.factory. annotation.Autowired 注解类型
    依赖注入到一个属性(成员变量)或方法

 如:

@Autowired
public  userService userService;

可使用 org.springframework.stereotype.Service 注解
 为 @Service(一个服务)

注意事项:
    此时需在配置文件中采用
 <context:component-scan base-package=“基本包”/> 元素来扫描依赖基本包。


Spring MVC中将一些业务逻辑分离出来,运用Service层实现
  
UserService 接口的具体代码如下:

复制代码
1
2
3
4
5
6
7
package service; import pojo.UserForm; public interface UserService { boolean login(UserForm user); boolean register(UserForm user); } UserServiceImpl 实现类的具体代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package service; import org.springframework.stereotype.Service; import pojo.UserForm; //注解为一个服务 @Service public class UserServiceImpl implements UserService { public boolean login(UserForm user) { if ("java265User".equals(user.getUname()) && "88888".equals(user.getUpass())) { return true; } return false; } public boolean register(UserForm user) { if ("zhangsan".equals(user.getUname()) && "123456".equals(user.getUpass())) { return true; } return false; } }

另配置文件中添加以下信息 <context:component-scan base-package=“基本包”/>元素,具体代码如下:

<context:component-scan base-package="service" />
修改 UserController类 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package controller; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import pojo.UserForm; import service.UserService; import com.sun.org.apache.commons.logging.Log; import com.sun.org.apache.commons.logging.LogFactory; @Controller @RequestMapping("/user") public class UserController { // 得到一个用来记录日志的对象,这样在打印信息的时候能够标记打印的是哪个类的信息 private static final Log logger = LogFactory.getLog(UserController.class); // 将服务依赖注入到属性userService @Autowired public UserService userService; /** * 处理登录 */ @RequestMapping("/login") public String login(UserForm user, HttpSession session, Model model) { if (userService.login(user)) { session.setAttribute("u", user); logger.info("成功"); return "main"; // 登录成功,跳转到 main.jsp } else { logger.info("失败"); model.addAttribute("messageError", "用户名或密码错误"); return "login"; } } /** * 处理注册 */ @RequestMapping("/register") public String register(@ModelAttribute("user") UserForm user) { if (userService.register(user)) { logger.info("成功"); return "login"; // 注册成功,跳转到 login.jsp } else { logger.info("失败"); // 使用@ModelAttribute("user")与model.addAttribute("user",user)的功能相同 // 在register.jsp页面上可以使用EL表达式${user.uname}取出ModelAttribute的uname值 return "register"; // 返回register.jsp } } }

最后

以上就是顺利硬币最近收集整理的关于Spring MVC如何使用@Autowired及@Service进行依赖注入呢?的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部