spring对象自动装配

byType    根据属性类型自动装配。BeanFactory查找容器中的全部Bean,如果正好有一个与依赖属性类型相同的Bean,就自动装配这个属性;如果有多个这样的Bean,Spring无法决定注入哪个Bean,就抛出一个致命异常;如果没有匹配的Bean,就什么都不会发生,属性不会被设置

byName    根据属性名自动装配。BeanFactory查找容器中的全部Bean,找出id与属性的setter方法入参匹配的Bean。找到即自动注入,否则什么都不做

constructor  与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的Bean,那么将会抛出异常

一、xml配置
byName:
spring-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!--根据byName类型识别,id要取名为属性名就可以自动装配 -->
    <bean id="tool" class="com.wance.entity.Plane">
        <!--<property name="message" value="Hello Spring"/>-->
    </bean>
	<!--自动装配 -->
    <bean class="com.wance.entity.SuperMan" id="sm" autowire="byName">
    </bean>
</beans>

byType:
spring-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="tool" class="com.wance.entity.Plane">
        <!--<property name="message" value="Hello Spring"/>-->
    </bean>

    <bean class="com.wance.entity.SuperMan" id="sm" autowire="byType">
    </bean>
</beans>

constructor:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="tool" class="com.wance.entity.Plane">
        <!--<property name="message" value="Hello Spring"/>-->
    </bean>
	<!--构造器默认会先使用byName -->
    <bean class="com.wance.entity.SuperMan" id="sm" autowire="constructor">
    </bean>
</beans>

在这里插入图片描述
二、注解注入
plane.java:

//如果不写,名字默认是类名首字母小写
@Component("f1")
@Scope("prototype")
public class Plane implements Fly {
    public Plane() {
        System.out.println("飞机被创建");
    }

    @Override
    public void fly() {
        System.out.println("飞机飞");
    }
}

HotBallon.java:

@Component
public class HotBallon implements Fly {
    @Override
    public void fly() {
        System.out.println("热气球在飞");
    }
}

SuperMan.java:

//超人可以通过工具飞,也可以自己飞
@Component
public class SuperMan implements Fly {
	//自动装配
    @Autowired
    @Qualifier("hotBallon")
    private Fly tool;//持有抽象属性
    @Value("king")
    private String name;
    public SuperMan() {

    }
    public SuperMan(Fly tool,String name){
        this.tool=tool;
        this.name=name;
    }
    //构造时,指定工具
    public SuperMan(Fly tool){
        this.tool=tool;
    }

    public void setTool(Fly tool) {
        this.tool = tool;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void fly() {
        if (tool==null){
            System.out.println("超人"+name+"自己飞");
        }else {
            System.out.println("超人"+name+"通过");
            tool.fly();//工具飞行
        }
    }
    @PostConstruct
    public void init(){
        System.out.println("超人完成初始化");
    }

    @PreDestroy
    public void destor(){
        System.out.println("超人完成销毁");
    }
}

测试:

    @Test
    public void test2(){
        ApplicationContext factory=new ClassPathXmlApplicationContext("spring-config.xml");
        SuperMan p1=factory.getBean("superMan",SuperMan.class);
        p1.fly();
    }

结果:

超人完成初始化
超人king通过
热气球在飞
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值