Spring中 基于注解的装配Bean 与 基于xml的对比总结
2017-02-26 09:06
411 查看
简介
注解:就是一个类,使用@注解名称开发中:使用注解 取代 xml配置文件。
这边引用他人的总结
1. @Component取代<bean class=""> @Component("id") 取代 <bean id="" class=""> 2.web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class=""> @Repository :dao层 @Service:service层 @Controller:web层 3.依赖注入 ,给私有字段设置,也可以给setter方法设置 普通值:@Value("") 引用值: 方式1:按照【类型】注入 @Autowired 方式2:按照【名称】注入1 @Autowired @Qualifier("名称") 方式3:按照【名称】注入2 @Resource("名称") 4.生命周期 初始化:@PostConstruct 销毁: @PreDestroy 5.作用域 @Scope("prototype") 多例
程序入口
public class TestAnnoWeb { @Test public void demo02() { String xmlPath = "com/guxiang/g_annotation/b_web/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext( xmlPath); StudentAction studentAction = (StudentAction) applicationContext .getBean("studentActionId"); studentAction.execute(); } }
首先 加载配置文件 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: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.xsd"> <!-- 组件扫描,扫描含有注解的类 --> <context:component-scan base-package="com.guxiang.annotation.web"></context:component-scan> </beans>
之后 第二句getBean(“studentActionId”)
在beans.xml 中没有找到 studentActionId
但是读取到了
<context:component-scan base-package="com.guxiang.annotation.web"> </context:component-scan>
于是开始从 注解中解析
读取到了
@Controller(“studentActionId”)
@Controller("studentActionId") public class StudentAction { @Autowired //默认按照类型 private StudentService studentService; public void execute() { studentService.addStudent(); } }
在读取到studentActionId后
之中的这个
@Autowired //默认按照类型 private StudentService studentService;
由于我们所配置的 context:component-scan 中的package 下 只有一个实现了 studentService接口的类 StudentServiceImpl类
于是这个注解实际上等效于
StudentService studentService =new StudentServiceImpl();
或者说等效于
<property name="studentService" ref="studentServiceId"></property> <bean id="studentServiceId" class="com.guxiang.annotation.web.StudentServiceImpl" >
于是我们开始实例化StudentServiceImpl 类
@Service public class StudentServiceImpl implements StudentService { private StudentDao studentDao; @Autowired @Qualifier("studentDaoId") public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } @Override public void addStudent() { studentDao.save(); } }
我们在set方法上方发现了注解
@Autowired @Qualifier("studentDaoId") public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; }
这是一种声明式按照名称的依赖注入的方式
在 于是 spring 开始搜寻 id 为 studentDaoId 的类
搜寻到
@Repository("studentDaoId") public class StudentDaoImpl implements StudentDao { @Override public void save() { System.out.println("dao"); } }
于是实例化这个类对象
等效于
StudentDao studentDao = new StudentDaoImpl ();
或者说等效于
<property name="studentDao" ref="studentDaoId"></property> <bean id="studentDaoId" class="com.guxiang.annotation.web.studentDaoId" >
相关文章推荐
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring装配bean基于XML,注解
- Spring学习历程---Bean基于xml的装配
- Spring(七)基于注解装配bean
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- Spring基于注解装配bean
- 【Spring】使用XML和注解装配Bean
- Spring 装配Bean 基于XML
- Spring(1-1、基于xml装配Bean)
- Spring(1-2、基于xml装配Bean)
- spring笔记(二)---基于XML的Bean装配
- 简化Spring的XML配置(二)——使用注解装配bean
- Spring-装配Bean 基于注解
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- spring-装配Bean 基于注解
- spring基础-Bean的装配方式(一)基于xml的装配
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配