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。