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。