spring-自动装配(annotation)

Spring 基于annotion 的自动装配
1:准备类
Cat

public class Cat {
    public void shout(){
        System.out.println("喵~");
    }
}

Dog

public class Dog {
    public void shout(){
        System.out.println("庄忠旺~");
    }
}

People

public class People {
    private Dog dog;
    private Cat cat;
    private String name;

2:注册bean
方式1:不用注解


<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!--    ===========不使用注解的自动装配==================-->
    <bean id="cat" class="com.sun.Cat"/>
    <bean id="dog" class="com.sun.Dog"/>
    <!--
        autowire="byName" 会自动在容器上下文中查找和自己对象set方法后面值对应的bean id值,例如:cat,dog,不过要一模一样,不能是dog222这类的
        autowire="byType" 会自动在容器上下文中查找和自己属性对象属性类型对应的bean id值  例如【class="com.sun.Cat"】
     -->
    <bean id="people" class="com.sun.People" autowire="byType">
        <property name="name" value="阿杰"/>
    </bean>
</beans>

测试:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
        System.out.println(people);

    }

结果:
在这里插入图片描述

方式2:注解
先设计配置文件(注解要开启)
beans.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--    开启注解-->
    <context:annotation-config/>

<!--    注解实现自动装配
            通过【@Autowired】这个注解实现自动装配
-->
    <bean id="cat" class="com.sun.Cat"/>
    <bean id="dog22" class="com.sun.Dog"/>
    <bean id="dog" class="com.sun.Dog"/>
    <bean id="people" class="com.sun.People"/>
</beans>

然后在People类中使用注解 @Autowired
如果Autowired不能唯一自动装配上属性(就是有多个相同类或者名字),则需要通过@Qualifier(value=“xxx”")

public class People {
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
    }

测试:

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
        System.out.println(people);

    }

结果:
在这里插入图片描述

额外补充:
jdk自带的自动装配注解
@ReSource:自动装配通过名字,类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值