文章目录
一、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

本文详细介绍了Spring如何通过Annotation装配Bean对象,包括开启注解扫描、使用@Component、@Service、@Repository、@Controller注解注册Bean,以及@Autowired、@Resource、@Qualifier、@Value注解实现依赖注入。还提到了@Configuration和@Bean注解在Spring配置中的作用。
最低0.47元/天 解锁文章
764

被折叠的 条评论
为什么被折叠?



