@Autowired注解的使⽤
springframework作用使⽤ @Autowired 注释的 Boss.java
1.package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;
public class Boss {
@Autowired
private Car car;
@Autowired
private Office office;
此处省略了getter和setter⽅法。
在域变量上加上标签@Autowired,并且去掉相应的get 和set⽅法
2.在l中把原来引⽤的<porpery >标签也去掉。
<!-- 该 BeanPostProcessor 将⾃动起作⽤,对标注 @Autowired 的 Bean 进⾏⾃动注⼊ -->
事先在Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<!-- 移除 boss Bean 的属性注⼊配置的信息<property> -->
<bean id="boss" class="com.baobaotao.Boss"/>
此处省略了<property>配置信息。
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 红旗CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就到和其匹配(默认按类型匹配)的 Bean,并注⼊到对应的地⽅中去。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。