Bean作用域、自动、手动、注解实现装配

 bean的作用域

有六种作用域:

(1)singleton单例模式,也是spring默认模式

<?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">
    <!--这种方式等于下边写 scope="singleton" -->
    <bean id="student" class="com.lxc.domain.Student" scope="singleton">
        <property name="name" value="Spring"/>
    </bean>
    <!-- <bean id="student" class="com.lxc.domain.Student" scope="singleton">
        <property name="name" value="Spring"/>
    </bean> -->
</beans>

测试下,下边调用两次 getBean( )

import com.lxc.domain.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        // spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans2.xml");
        Student student = (Student) ctx.getBean("student");
        Student student1 = ctx.getBean("student", Student.class);
        System.out.println(student == student1);
    }
}

 结果输出true,说明,两次的bean都是同一个对象。

 

(2)prototype原型模式

<?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="student" class="com.lxc.domain.Student" scope="prototype">
        <property name="name" value="Spring"/>
    </bean>
</beans>

测试调用:

(3)其余几个request、session、application这些只能在web开发中使用,在MVC时在记录。

 

Bean的自动装配

· 上一篇文章中,全都是手动装配的,而自动装配是Spring满足bean依赖的一种方式!
· Spring会在上下文中自动寻找,并自动给bean装配属性

Spring中三种装配的方式

(1)在xml中显示的手动配置。
(2)在java中显示的配置。
(3)隐式自动装配(很重要)

 

1、先来看下手动配置

Person

package com.lxc.domain;

public class Person {
    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

package com.lxc.domain;
public class Dog {
    public void shout() {
        System.out.println("汪汪");
    }
}

Cat

package com.lxc.domain;

public class Cat {
    public void shout() {
        System.out.println("喵");
    }
}
<!-- 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="cat" class="com.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person">
        <property name="name" value="Spring"/>
        <property name="cat" ref="cat" />
        <property name="dog" ref="dog" />
    </bean>
</beans>

测试

import com.lxc.domain.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Person person = ctx.getBean("person", Person.class);
        person.getCat().shout();
        person.getDog().shout();
    }
}

输出结果

 

2、自动配置 通过属性 byName

         只需要改动beans.xml代码即可:

         添加 autowire="byName" 属性;

byName原理:它会自动在容器上下文中查找, 和自己对象set方法后边的值对应的<beans>标签上的id是否一致,一致的话,property就不需要引用cat和dog了,使用byName还需要保证bean的id必须唯一。

<?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.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person" autowire="byName">
        <property name="name" value="lxc"/>
    </bean>
</beans>

 

3、使用注解自动装配

jdk1.5支持注解

@Autowired
在属性或set方法上边使用,在属性上边使用注解,下边的set方法可以省略。

步骤:

· 导入约束 —— context

·配置注解支持

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

 

还是看上边的例子:

<?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.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person">
        <property name="name" value="lxc" />
    </bean>

</beans>

Person

在Cat和Dog对象上加上 @Autowired注解即可

package com.lxc.domain;

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

public class Person {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    // 可以去掉下边set方法,因为上边上属性使用了注解
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    // 同上,下边set方法可以省略
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

测试

 

4、补充

@Nullable   字段标记了这个注解,说明该字段可以为null;

@Autowired(required  = false)
private String name;  // 加上@Autowired(required  = false)该字段表示可以为null。

 

5、使用自动装配实现不了的情况

@Qualifier(value = "xxx")

如果自动装配的环境复杂,使用 @Autowired 解决不了问题,那么我们可以使用 @Autowired 和 @Qualifier(value = "xxx") 结合起来使用。

<?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.lxc.domain.Cat" />
    <bean id="cat1" class="com.lxc.domain.Cat" />
    <bean id="cat11" class="com.lxc.domain.Cat" />
    <bean id="dog" class="com.lxc.domain.Dog" />
    <bean id="person" class="com.lxc.domain.Person">
        <property name="name" value="lxc" />
    </bean>

</beans>
package com.lxc.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.*;
public class Student {
    @Autowired
    @Qualifier(value = "address")
    private Address address;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值