文章目录
Bean的作用域
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);
}
-
结果
说明bean的单例模式中,从容器中get的是同一个对象
2.原型模式
- bean的属性配置
<bean id="user1" class="com.ming.pojo.User" c:age="20" c:name="张三" scope="prototype"/>
-
测试同上
-
结果
说明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();
}
- 运行结果
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;