Spring自动装配【Bean的作用域、@Autowried、@Resource】

Bean 作用域

1、singleton(单例模式)

默认就是单例模式,不需要单独设置。

    <bean id="user" class="com.study.pojo.User" p:name="燕双鹰" p:age="25" scope="prototype"/>
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

Person person = context.getBean("person", Person.class);
Person person1 = context.getBean("person", Person.class);

System.out.println(person==person1);    //true

2、prototype

    <bean id="user" class="com.study.pojo.User" p:name="燕双鹰" p:age="25" scope="singleton"/>

每次从容器中 get ,都会产生一个新对象! 

ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

Person person = context.getBean("person", Person.class);
Person person1 = context.getBean("person", Person.class);

System.out.println(person==person1);    //false

3、其它

request、session、application 这些只在 web 开发中使用到!

 Bean 的自动装配

  • 自动装配是Spring满足Bean依赖的一种方式!
  • Spring会在上下文中自动寻找,并自动给Bean装配属性!

1、测试

编写三个类Cat、Dog、People。

public class Dog {
    public void shout(){
        System.out.println("woof~");
    }
}
public class Cat {
    public void shout(){
        System.out.println("miu~");
    }
}
public class People {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

在Spring的beans.xml中注册,这也是我们普通的装配方式。

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>

    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
        <property name="name" value="李元芳"/>
    </bean>

byName 自动装配

byName需要bean标签的id属性和set方法对应!setDog方法必须对应 bean标签属性id为 "dog"。

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <!--
    byName:会自动在容器上下文自动查找和自己对象set方法对应的beanid
    -->
    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="name" value="李元芳"/>
    </bean>

byType 自动装配

byType不需要设置bean标签的id属性,因为它是根据bean的类去查找的,所以必须保证所以bean标签的 class 属性唯一。

    <bean class="com.study.autowried.Cat"/>
    <bean class="com.study.autowried.Dog"/>
    <!--
    byType:会自动在容器上下文自动查找和自己对象类型相同的bean
    -->
    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="name" value="李元芳"/>
    </bean>

使用注解实现自动装配@Autowired

使用注解需要在Spring的beans.xml中

  • 导入约束:  xmlns:context="http://www.springframework.org/schema/context"
  • 配置注解的支持:<context:annotation-config/>
<?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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

        默认情况下,@Autowired注解是通过byType方式实现自动装配的,如果希望通过byName方式进行自动装配,可以在@Autowired注解上结合@Qualifier注解使用。

测试

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


    <!--开启注解的支持-->
    <context:annotation-config/>

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>

    <bean id="people" class="com.study.autowried.People"/>

</beans>

使用Autowried注解我们可以不用编写Set方法。

import org.springframework.beans.factory.annotation.Autowired;

public class People {
    //直接加载属性上即可实现自动装配
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    
    public Dog getDog() {
        return dog;
    }

    public String getName() {
        return name;
    }
    
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

其它

@Autowried的required属性

如果Autowried的属性required的值为false,则这个对象可以为null,否则不可以为空(默认为true,不可为空)。

public class People {
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

@Qualifier

如果@Autowried自动装配的环境比较复杂,我们可以使用@Qualifier(value="beanid")来指定唯一的bean对象注入!

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

    <!--开启注解的支持-->
    <context:annotation-config/>

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="cat1" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="dog1" class="com.study.autowried.Dog"/>

    <bean id="people" class="com.study.autowried.People"/>

</beans>

多个Dog对象和Cat对象,我们需要通过@Qualifier 指定具体的bean的id。 

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

@Resource

上面的都是spring中的注解,而这个@Resource是我们java原生的注解,它同样可以实现自动装配。它会先通过byName的方式通过set方法查找对应beanid的bean;如果找不到,就会通过byType的方式通过class找唯一类型的bean;如果我们有多个相同类型的bean,我们可以通过@Resource(name="")的形式来指定对应的bean。

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

    <!--开启注解的支持-->
    <context:annotation-config/>

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="cat1" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="dog1" class="com.study.autowried.Dog"/>

    <bean id="people" class="com.study.autowried.People"/>

</beans>

 

public class People {
    @Resource
    private Cat cat;    //匹配到bean id=cat的bean对象
    @Resource(name="dog1")
    private Dog dog;    //匹配到bean id=dog1的bean对象
    private String name;
}

小结

@Resource和@Autowried的区别:

  • @Autowried 默认通过 byType实现,如果有多个bean可以搭配Qualifier(value="beanid")来实现。
  • @Resource 默认通过 byName实现,如果找不到,会使用 byType实现,同样可以通过@Resource(name="beanid")来匹配指定id的bean。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

让线程再跑一会

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

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

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

打赏作者

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

抵扣说明:

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

余额充值