Spring 如何基于 Annotation 装配 Bean 对象的讲解

本文详细介绍了Spring如何通过Annotation装配Bean对象,包括开启注解扫描、使用@Component、@Service、@Repository、@Controller注解注册Bean,以及@Autowired、@Resource、@Qualifier、@Value注解实现依赖注入。还提到了@Configuration和@Bean注解在Spring配置中的作用。
摘要由CSDN通过智能技术生成


一、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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值