注解使用自动装配


<?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/>

</beans>

1.环境搭建

链接:https://www.cnblogs.com/kakafa/p/15770393.html

和链接里面的一样


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/>

    <bean id="cat" class="com.kakafa.pojo.Cat"/>
    <bean id="dog" class="com.kakafa.pojo.Dog"/>
    <bean id="people" class="com.kakafa.pojo.People"/>


</beans>


@Resource和@Autowired注解都是用来实现依赖注入的。只是@AutoWried先按bytype自动注入,而@Resource默认先按byName自动注入。


posted @ 2022-01-06 13:58  卡卡发  阅读(39)  评论(0)    收藏  举报