Spring中 基于注解的装配Bean 与 基于xml的对比总结
来源:互联网 发布:遗传算法c 库 编辑:程序博客网 时间:2024/06/05 23:49
简介
注解:就是一个类,使用@注解名称
开发中:使用注解 取代 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 类
@Servicepublic 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" >
0 0
- Spring中 基于注解的装配Bean 与 基于xml的对比总结
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- spring-装配Bean 基于注解
- spring装配bean(基于注解)
- Spring 装配Bean 基于注解
- Spring-装配Bean 基于注解
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- spring装配Bean(基于xml)
- Spring 装配Bean 基于XML
- spring装配Bean(基于xml)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- spring笔记(二)---基于XML的Bean装配
- Spring学习历程---Bean基于xml的装配
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
- 基于XML方式的Bean装配实例
- 装配Bean 基于注解
- bean装配基于注解
- 装配Bean基于注解
- JDBC操作小例子
- Codeforce
- webrtc客户端与TURN和信令服务器通讯流程图
- 使用观察者模式解决包相互依赖的问题
- Android App的设计架构:MVC,MVP,MVVM与架构经验谈
- Spring中 基于注解的装配Bean 与 基于xml的对比总结
- table标签,认识网页上的表格
- HTML嵌入到iOS页面的开发
- 2017年你应该尝试的25个Android库
- Android WebView常见问题解决方案汇总
- codeforces 777b Game of Credit Cards
- Ubuntu下安装rmagick包
- 算法训练 6-1 递归求二项式系数值
- 【实战演练】SQL SERVER——SQL语句优化建议