Spring学习笔记(八)模拟实际开发过程的调用过程注解版-传统方式
传统的@Component和@Resource(等价于XML版setter注入)使用说明
@Component是个泛化概念,可以用在任何层次
源码获取github
1.项目结构(jar同上)
2.核心配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!--1.启动Spring注解-->
<context:annotation-config></context:annotation-config>
<!--2.扫描-->
<context:component-scan base-package="com.hs"/>
</beans>
3.dao层
userDao.java
package com.hs.dao;
public interface UserDao {
void add();
}
UserDaoImpl.java
package com.hs.dao.impl;
import com.hs.dao.UserDao;
import org.springframework.stereotype.Component;
@Component//(value = "userDao") //相当于<bean id="userDaoImpl" class="com.hs.dao.impl.UserDaoImpl"/>
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("UserDao层的实现类--->ADD");
}
}
4.Service层
UserService.java
package com.hs.service;
public interface UserService {
void add();
}
UserServiceImpl.java
package com.hs.service.impl;
import com.hs.dao.UserDao;
import com.hs.service.UserService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component //相当于<bean id="userServiceImpl" class="com.hs.service.impl.UserServiceImpl">
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
/*相当于<property name="userDao"> <ref bean="userDaoImpl"/> </property>*/
@Resource(name = "userDaoImpl")
//下面的查找顺序 先按照名字进行搜索(没有写名字默认就是set后面那个名字) 再按照类型进行搜索(UserDao的实现类)
//@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add() {
System.out.println("UserService层的实现类--->ADD");
this.userDao.add();
}
}
知识点:@Resource注解
先按名称搜索,再按照类型搜索
是javax下的,不是Spring自带的
- 后面会有Spring自带的注解@Autowired推荐
5.Controller层
UserController.java
package com.hs.web;
import com.hs.service.UserService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component //相当于<bean id="userController" class="com.hs.web.UserController"/>
public class UserController {
//简写方式,写个就不用写set方法了
//如果你所在的公司用了测试框架(不是我现在用的单元测试框架),那么不建议使用简写方式,对测试有影响
//@Resource(name = "userServiceImpl")
private UserService userService;
public UserService getUserService() {
return userService;
}
@Resource(name = "userServiceImpl") //相当于p:userService-ref="userServiceImpl"
public void setUserService(UserService userService) {
this.userService = userService;
}
public void add() {
System.out.println("UserController层--->ADD");
this.userService.add();
}
}
6.测试
package com.hs.test;
import com.hs.web.UserController;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MethodTest {
@Test
public void test01() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserController controller = applicationContext.getBean("userController", UserController.class);
controller.add();
}
}