Bean的作用域和自动装配


Bean的作用域

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fFVzWO6R-1611044586759)![(D:/Typora_area/%25E4%25BE%259D%25E8%25B5%2596%25E6%25B3%25A8%25E5%2585%25A5.assets/image-20210118153850287.png)]](https://img-blog.csdnimg.cn/20210119162319526.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21pbmdpYW8=,size_16,color_FFFFFF,t_70](https://img-

1.单例模式(spring默认机制)

  • bean属性设置
 <bean id="user1" class="com.ming.pojo.User" c:age="20" c:name="张三" scope="singleton"/>
  • 测试
  @Test
    public void test(){
       ApplicationContext context=new ClassPathXmlApplicationContext("userbean.xml");
       User user=context.getBean("user1",User.class);
       User user1=context.getBean("user1",User.class);
       System.out.println(user==user1);
    }
  • 结果

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2iszS2tD-1611044586761)(C:%5CUsers%5CHP%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210118163517771.png)]

说明bean的单例模式中,从容器中get的是同一个对象


2.原型模式

  • bean的属性配置
<bean id="user1" class="com.ming.pojo.User" c:age="20" c:name="张三" scope="prototype"/>
  • 测试同上

  • 结果

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cgj4HFMw-1611044586762)(C:%5CUsers%5CHP%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210118163851690.png)]

说明bean的原型模式中,每次从容器中get的时候都会产生一个新对象!


3.其他模式

其余的request,session,application,这些只能在web开发中使用。




Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会自动在配置文件中寻找,并自动给bean配置属性

在spring中的三种装配方式

​ 1.在xml中显示的配置

​ 2.在java中显示的配置

​ 3.隐式的自动装配bean


1.ByName自动装配

<bean id="people" class="com.ming.pojo.People" autowire="byName">
      <property name="name" value="mingiao"/>
</bean>

ByName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!

注意:需要保证所有bean的id唯一,并且在这个bean需要和自动注入的属性的set方法的值一致!


2.ByType自动配置

<bean id="people" class="com.ming.pojo.People" autowire="byType">
        <property name="name" value="mingiao"/>
</bean>

byType:会自动在容器上下文查找,和自己对象属性类型相同的bean!

注意:需要保证所有bean的class唯一,并且在这个bean需要和自动注入的属性的类型一致!


例如:

  • Peopel.java
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;
    }
}
  • Dog.java
public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}
  • Cat.java
public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}
  • 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.ming.pojo.Cat"/>
    <bean id="dog" class="com.ming.pojo.Dog"/>

    <bean id="people" class="com.ming.pojo.People" autowire="byName">
        <property name="name" value="mingiao"/>
    </bean>

</beans>
  • 测试
    @Test
    public void test1(){
        ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
        People people=context.getBean("people",People.class);
        people.getDog().shout();
        people.getCat().shout();
}
  • 运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OxtWuwg8-1611044586763)(C:%5CUsers%5CHP%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210118174504352.png)]


3.使用注解实现自动装配

1)使用注解须知:
  • 导入约束:context
xmlns:context="http://www.springframework.org/schema/context"
  • 配置注解的支持:
<context:annotation-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"     
    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>

2) @Autowired注解
  • 直接在属性上使用即可
 @Autowired
 private Cat cat;

​ 也可以在set方法上使用

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

  • 使用该注解,可以不用编写set方法(前提是自动装配的属性在IOC(spring)容器中存在,且符合类型byType)

  • 如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
 @Autowired(required=false)//required值默认为ture
 private Cat cat;

​ @Nullable标记了某一属性,也说明该属性可以为空

 public People(@Nullable String name){
        this.name=name;
 }

注意:@Autowired可以允许对象(属性值)为空,但必须要求这个对象存在


  • 如果@Autowired自动装配的环境比较复杂,可以使用@Qualifier来找到相应配置,指定一个唯一的bean对象

bean配置

    <bean id="cat" class="com.ming.pojo.Cat"/>
    <bean id="dog1" class="com.ming.pojo.Dog"/>
    <bean id="dog2" class="com.ming.pojo.Dog"/>
    <bean id="dog3" class="com.ming.pojo.Dog"/>

注解配置

    @Autowired
    @Qualifier(value ="dog1")
    private Dog dog;

3)Resource注解
  • 可以放在属性字段上使用
  • 默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到,则报错。
    @Resource
    private Cat cat;
    @Resource(name = "dog1")//通过name找到唯一的bean对象
	private Dog dog;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值