文章目录
一、Spring 基于 Annotation 装配 Bean对象
在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。在 Spring 框架中提供了一些用于装配 Bean 对象的注解用于替换 xml 文件的中配置。
1、 jar 包依赖
Spring 核心容器模块:
spring-beans-5.2.7.RELEASE.jar
spring-context-5.2.7.RELEASE.jar
spring-core-5.2.7.RELEASE.jar
spring-expression-5.2.7.RELEASE.jar
Spring AOP 模块:
spring-aop-5.2.7.RELEASE.jar
Commons-Loggin 日志:
commons-logging-1.2.jar
2、 在 Spring 的配置文件中开启注解扫描
<context:component-scan base-package="扫描注解的包名"/>
3、 注册 Bean 对象
第一步记得开启注解扫描
<?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