文章目录
自动注入
XML自动配置
XML自动配置通过四个注解实现,功能一样的
注解 | 描述 |
---|---|
@Component | 一般用在身份不明确的组件上 |
@Service | 一般用在service层 |
@Controller | 一般用在控制层 |
@Repository | 一般用在数据库访问层 |
在applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
<!--扫描全部 -->
<!-- <context:component-scan base-package="com.sxt.*.dao.impl"></context:component-scan>
<!--扫描指定文件,一条语句完成 -->
<context:component-scan base-package="com.sxt.dao.impl,com.sxt.service.impl,com.sxt.controller"></context:component-scan>-->
<!--扫描指定文件 -->
<context:component-scan base-package="com.sxt.dao.impl"></context:component-scan>
<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
<context:component-scan base-package="com.sxt.controller"></context:component-scan>
</beans>
在Controller配置
@Controller//该注解作用和在applicationContext.xml中的<bean>的作用是一样的
public class UserController {
private IUserService service;
@Resource
public void setService(IUserService service) {
this.service = service;
}
public void say(String msg){
System.out.println("controller:"+service.say(msg));
}
}
在dao层的实现类配置
@Repository
public class UserDAOImpl implements IUserDAO {
@Override
public String say(String msg) {
System.out.println("--->"+msg);
return "hello";
}
}
在service层实现类配置
@Service
public class UserServiceImpl implements IUserService {
// 调用者与被调用者之间 DI 依赖注入 设值
@Resource
private IUserDAO dao;
@Override
public String say(String msg) {
return dao.say(msg);
}
/**
* 设值注入必要提供的setter方法
* @param dao
*/
public void setDao(IUserDAO dao) {
this.dao = dao;
}
}
测试
public class test {
public static void main(String[] args) {
ApplicationContext ac=
new ClassPathXmlApplicationContext("applicationContext.xml");
UserController controller = ac.getBean(UserController.class);
controller.say("aaa");
}
}
java自动配置
①配置javaConfig
@Configuration
//添加扫描
@ComponentScan("com.sxt.*")
//多个文件扫描
//@ComponentScans(value={@ComponentScan(""),@ComponentScan("")})
/*@ComponentScan(value="com.sxt.*"
,useDefaultFilters=true
,excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes=Service.class)})*/
public class javaConfig {
}
②配置dao层实现类
//@Component // 等价于 <bean class="com.sxt.service.impl.UserDaoImpl" name="userDaoImpl"/>
@Repository
public class UserDAOImpl implements IUserDAO {
@Override
public String say(String msg) {
System.out.println("--->"+msg);
return "hello";
}
}
③配置service实现类
//@Component // 将当前类交给Spring容器管理
@Service
public class UserServiceImpl implements IUserService {
// 调用者与被调用者之间 DI 依赖注入 设值
@Resource
private IUserDAO dao;
@Override// 只能根据类型查找
public String say(String msg) {
return dao.say(msg);
}
}
④配置控制层
@Controller
public class UserController {
@Resource
private IUserService service;
public void sleep(){
System.out.println("休息一会....");
}
public void say(String msg){
System.out.println("controller:"+service.say(msg));
}
}
⑤测试
public class test {
public static void main(String[] args) {
ApplicationContext ac=
new AnnotationConfigApplicationContext(javaConfig.class);
UserController controller = ac.getBean(UserController.class);
controller.say("aaa");
}