Spring中 基于注解的装配Bean 与 基于xml的对比总结

本文对比了Spring中基于注解的装配Bean与基于XML的方式。通过注解,开发中可以避免使用XML配置文件。以程序入口为例,当尝试获取'beanId'时,Spring首先查找XML配置,未找到后转向注解解析。注解如@Controller指定beanId,通过<context:component-scan>扫描特定包,找到实现特定接口的类进行实例化。在类的set方法上,使用注解声明依赖注入,Spring会寻找并实例化相应id的类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

注解:就是一个类,使用@注解名称
开发中:使用注解 取代 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" >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值