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
原创粉丝点击