一.自动注解的方式
1. 首先在com.springmvc包下创建controller, service, dao, model文件夹,并且分别添加对应的java文件
2. 在文件中添加任意方法,保证controller--> service --> dao 流程顺利执行
controller层代码
@Controller
public class login {
@Autowired
private LoginService loginService;
@RequestMapping(value="/Login")
public String login(Model model){
User user = loginService.getUserInfo();
System.out.printf("姓名%s --年龄%s --性别%s", user.getName(), user.getAge(), user.getSex()) ;
model.addAttribute("message", "Hello World!!!");
return "login";
}
}
service层代码
@Service
public class LoginService {
@Autowired
private LoginDao loginDao = null;
public User getUserInfo() {
return loginDao.getUserInfo();
}
}
dao层代码
@Repository
public class LoginDao {
public User getUserInfo() {
User user = new User();
user.setName("XXX");
user.setPassword("XXXXX");
user.setAge("XX");
user.setSex("X");
return user;
}
}
注意:(1) @Service("XXX") 可以指定实例名称
(2) @Autowired注解, 可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
(3) @Autowired是默认按照类型装配的 @Resource默认是按照名称装配
3. 在servlet文件中添加配置属性
<!-- service 注入 -->
<context:component-scan base-package="com.springmvc"></context:component-scan>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
二. bean属性中配置每一个service与dao
1. 第二种方式各层代码如下,dao层代码保持不变
controller层代码
@Controller
public class login {
@Autowired
private LoginService loginService;
@Resource(name="loginService")
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
@RequestMapping(value="/Login")
public String login(Model model){
User user = loginService.getUserInfo();
System.out.printf("姓名%s --年龄%s --性别%s", user.getName(), user.getAge(), user.getSex()) ;
model.addAttribute("message", "Hello World!!!");
return "login";
}
}
service层代码
@Service
public class LoginService {
@Autowired
private LoginDao loginDao = null;
@Resource(name="loginDao")
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
public User getUserInfo() {
return loginDao.getUserInfo();
}
}
3. 在servlet文件中添加配置属性
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="loginService" class="com.springmvc.service.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="loginDao" class="com.springmvc.dao.LoginDao" />
注意: (1) id="loginService" 与注解@Resource的name保持一致
(2) @Resource可以替换为@Qualifier
(3) 可以把配置文件中的关于service和dao的配置信息单独提取到一个文件中, 例如
<import resource="spring-test.xml"/>
spring-test.xml文件中代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="loginService" class="com.springmvc.service.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="loginDao" class="com.springmvc.dao.LoginDao" />
</beans>