spring依赖注入之手工装配

本文介绍了在Spring框架中手工装配依赖对象的两种主要方法:通过配置文件进行bean节点配置及使用@Autowired或@Resource注解的方式,并详细解释了如何在配置文件中启用注解注入支持。

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

2010-09-05 

在spring中手工装配依赖对象,可以有两种方式:
1,在spring配置文件中,通过bean节点下配置,比如:
<bean id="service" class="com.hust.business.BuyService">
<property name="name" value="zhangsan"/>
<constructor-arg index="0" type="java.lang.String" value="wuhan">
</bean>
2,使用@Autowired或@Resource注解方式进行装配,此时需要在spring配置文件中添加以下配置:
<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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
其中红色部分是要加入的内容,是使用注解功能进行注入所需要的命名空间和xml schema,该配置文件隐式注册了多个对注解进行解析处理的解析器AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值