FactoryBean
Spring中有两种类型的bean,一种是普通bean,另一种是工厂bean,即FactoryBean。
工厂bean跟普通bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂bean的getObject方法所返回的对象。支持开发人员参与到bean对象的创建过程中
工厂bean必须实现org.springframework.beans.factory.FactoryBean接口。
①创建Person类
package spring03_test;
public class Person {
private String name;
public Person() {
super();
System.out.println("Person类的构造方法被执行");
}
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
②创建工厂bean,MyPersonFactoryBean
package spring03_test;
import org.springframework.beans.factory.FactoryBean;
public class MyPersonFactoryBean implements FactoryBean<Person> {
public MyPersonFactoryBean() {
super();
System.out.println("MyPersonFactoryBean的构造方法");
}
@Override
public Person getObject() throws Exception {
Person p = new Person();
p.setName("张三丰");
return p;
}
@Override
public Class<?> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
③创建并配置spring_factoryBean.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="person" class="spring03_test.MyPersonFactoryBean"></bean>
</beans>
④ 编写测试类Test03
package spring03_test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test03 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_factoryBean.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person);
}
⑤执行结果
MyPersonFactoryBean的构造方法
Person类的构造方法被执行
Person [name=张三丰]
bean
bean的作用域
在Spring中,可以在元素的scope属性里设置bean的作用域,以决定这个bean是单实例的还是多实例的。
默认情况下,Spring只为每个在IOC容器里声明的bean创建唯一一个实例,整个IOC容器范围内都能共享该实例:所有后续的getBean()调用和bean引用都将返回这个唯一的bean实例。该作用域被称为singleton,它是所有bean的默认作用域。
- singleton:单例的,单例的Bean是在IOC容器对象创建时创建,且只会创建一次,每次通过getBean()方法获取bean对象的时候,返回的都是一个对象。
- prototype:多例的,多例的bean不会在IOC容器创建时创建,而是每次通过调用getBean()时都会返回一个新的对象。
- request:在一次请求期间创建一个bean对象。
- session:在一次会话期间创建一个bean对象。
当bean的作用域为单例时,Spring会在IOC容器对象创建时就创建bean的对象实例。而当bean的作用域为prototype时,IOC容器在获取bean的实例时创建bean的实例对象。
示例:
①创建User类
package spring04_test;
public class User {
private Integer id;
private String username;
private String password;
public User() {
super();
System.out.println("Uer无参构造方法被调用");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
②创建并配置spring_scope.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="user" class="spring04_test.User" scope="singleton">
<property name="id" value="1001"></property>
<property name="username" value="张三丰"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
测试类:
package spring04_test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_scope.xml");
User user1 = context.getBean("user", User.class);
System.out.println(user1);
System.out.println("++++++++++++++++++");
User user2 = context.getBean("user", User.class);
System.out.println(user2);
System.out.println(user1==user2);
}
}
结果:
Uer无参构造方法被调用
User [id=1001, username=张三丰, password=123456]
++++++++++++++++++
User [id=1001, username=张三丰, password=123456]
true
修改spring_scope.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="user" class="spring04_test.User" scope="prototype">
<property name="id" value="1001"></property>
<property name="username" value="张三丰"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
结果:
Uer无参构造方法被调用
User [id=1001, username=张三丰, password=123456]
++++++++++++++++++
Uer无参构造方法被调用
User [id=1001, username=张三丰, password=123456]
false
bean的生命周期
-
Spring IOC容器可以管理bean的生命周期,Spring允许在bean生命周期内特定的时间点执行指定的任务。
-
Spring IOC容器对bean的生命周期进行管理的过程:
① 通过构造器或工厂方法创建bean实例
② 为bean的属性设置值和对其他bean的引用
③ 调用bean的初始化方法
④ bean可以使用了
⑤ 当容器关闭时,调用bean的销毁方法
Ø 创建Book类,并且在Book类中自己定义初始化和销毁方法
package spring05_test;
public class Book {
private String isbn;
private String name;
public Book() {
System.out.println("Bean的生命周期阶段一:通过构造方法创建bean对象");
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
System.out.println("Bean的生命周期阶段二:给对象属性赋值");
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book [isbn=" + isbn + ", name=" + name + "]";
}
//初始化方法
public void init() {
System.out.println("Bean的生命周期阶段三:bean的初始化方法");
}
//销毁方法
public void destroy() {
System.out.println("Bean的生命周期阶段五:bean的销毁方法");
}
}
Ø 创建并配置spring_lifecycle.xml文件,需要在配置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="book" class="spring05_test.Book" init-method="init" destroy-method="destroy">
<property name="isbn" value="ISBN-001"></property>
<property name="name">
<value><![CDATA[<<Java编程思想>>]]></value>
</property>
</bean>
</beans>
Ø 测试类:
package spring05_test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring_lifecycle.xml");
Book book = context.getBean("book", Book.class);
System.out.println("Bean的生命周期阶段四:使用bean");
context.close();
}
}
Ø 结果:
Bean的生命周期阶段一:通过构造方法创建bean对象
Bean的生命周期阶段二:给对象属性赋值
Bean的生命周期阶段三:bean的初始化方法
Bean的生命周期阶段四:使用bean
八月 30, 2019 8:24:20 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7eda2dbb: startup date [Fri Aug 30 20:24:20 CST 2019]; root of context hierarchy
Bean的生命周期阶段五:bean的销毁方法
bean的后置处理器
① bean后置处理器允许在调用初始化方法前后对bean进行额外的处理
② bean后置处理器对IOC容器里的所有bean实例逐一处理,而非单一实例。
其典型应用是:检查bean属性的正确性或根据特定的标准更改bean的属性。
③ bean后置处理器需要实现接口:
org.springframework.beans.factory.config.BeanPostProcessor。
在初始化方法被调用前后,Spring将把每个bean实例分别传递给上述接口的
以下两个方法:
●postProcessBeforeInitialization(Object, String)
●postProcessAfterInitialization(Object, String)
添加bean后置处理器后bean的生命周期
①通过构造器或工厂方法创建bean实例
②为bean的属性设置值和对其他bean的引用
③将bean实例传递给bean后置处理器的postProcessBeforeInitialization()方法
④调用bean的初始化方法
⑤将bean实例传递给bean后置处理器的postProcessAfterInitialization()方法
⑥bean可以使用了
⑦当容器关闭时调用bean的销毁方法
引用外部属性文件
当bean的配置信息逐渐增多时,查找和修改一些bean的配置信息就变得愈加困难。这时可以将一部分信息提取到bean配置文件的外部,以properties格式的属性文件保存起来,同时在bean的配置文件中引用properties属性文件中的内容,从而实现一部分属性值在发生变化时仅修改properties属性文件即可。这种技术多用于连接数据库的基本信息的配置。
直接配置
(1)导入mysql数据库驱动和druid连接池的jar包
- druid-1.1.9.jar
- mysql-connector-java-5.1.37-bin.jar
(2)创建并配置spring_datasource.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="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test01"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
</beans
(3)测试类
package spring05_test;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_datasource.xml");
DataSource dataSource = context.getBean("datasource", DataSource.class);
System.out.println(dataSource);
System.out.println(dataSource.getConnection());
}
}
(4)测试结果
{
CreateTime:"2019-08-30 21:01:56",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
八月 30, 2019 9:01:56 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@2c039ac6
使用外部的属性文件
Ø 创建db.properties属性文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/test01
jdbc.username=root
jdbc.password=123456
Ø 引入context名称空间, 指定properties属性文件的位置
<?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-4.0.xsd">
<!--指定properties属性文件的位置,classpath:xxx,表示属性文件位于类路径下-->
<context:property-placeholder location="classpath:properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
结果(和上面一样):
{
CreateTime:"2019-08-30 21:18:34",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
八月 30, 2019 9:18:34 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@4e718207
容易出错的地方:如果在db.properties中配置用户名的时候,没有加前缀,直接使用username,在通过${username}取值的时候,会和框架中获取系统用户的名字冲突,所以我们在开发的时候,应该尽量加上前缀,避免使用冲突性高的名字
通过import标签导入配置文件
Ø 创建applicationContext.xml文件,并在其中加入import标签
<import resource="spring-datasource.xml"/>
自动装配
-
手动装配:以value或ref的方式明确指定属性值都是手动装配。
-
自动装配:根据指定的装配规则,不需要明确指定,Spring自动将匹配的属性值注入bean中。注意:自动装配只能装配通过ref引用的属性。
案例
演示通过xml的方式,实现自动分配。
(1) 创建员工类Employee,并重新拼接toString方法。
package spring06_test;
public class Employee {
private String empName;
private String friendName;
private Address address;
private Car car;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getFriendName() {
return friendName;
}
public void setFriendName(String friendName) {
this.friendName = friendName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return this.empName+"开着价值"+car.getPrice()+"的"+car.getBrand()+"车,带着"+this.friendName
+"到"+address.getCity()+"的"+address.getLocation()+"去happy!!";
}
}
(2)创建地址Address类
package spring06_test;
public class Address {
private String city;
private String location;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
(3)创建汽车Car类
package spring06_test;
public class Car {
private String brand;
private Double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
(4)创建spring_autoware.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">
<!--将Address对象的创建交给容器 -->
<bean id="address" class="spring06_test.Address">
<property name="city" value="北京"/>
<property name="location" value="三里屯"/>
</bean>
<!--将Car对象的创建交给容器 -->
<bean id="car" class="spring06_test.Car">
<property name="brand" value="奔驰"/>
<property name="price" value="1000000"/>
</bean>
<!--配置员工对象 -->
<bean id="employee" class="spring06_test.Employee" autowire="byName">
<property name="empName" value="张三"/>
<property name="friendName" value="李四"/>
</bean>
</beans>
(5)结果
张三开着价值1000000.0的奔驰车,带着李四到北京的三里屯去happy!!
通过注解配置bean
- 普通组件:@Component
标识一个受Spring IOC容器管理的组件
- 持久化层组件:@Repository
标识一个受Spring IOC容器管理的持久化层组件
- 业务逻辑层组件:@Service
标识一个受Spring IOC容器管理的业务逻辑层组件
- 表述层控制器组件:@Controller
标识一个受Spring IOC容器管理的表述层控制器组件
组件命名规则
①默认情况:使用组件的简单类名首字母小写后作为bean的id。
②使用组件注解的value属性指定bean的id。
/*
相当于配置在配置文件中配置了
<bean id="userController" class="spring05_test.UserController"></bean>
*/
@Controller
public class UserController{
}
扫描组件
组件被注解标识后还需要通过Spring进行扫描才能够侦测到。
指定被扫描的packpage
创建并配置spring_annotation.xml文件
<!--组件扫描-->
<context:component-scan base-package="spring02"></context:component-scan>
</beans>
详细说明:
①base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包及其子包中的所有类。
②当需要扫描多个包时可以使用逗号分隔。
③如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
<context:component-scan
base-package="com.atguigu.component"
resource-pattern="autowire/*.class"/>
④包含与排除
●context:include-filter子节点表示要包含的目标类
注意:通常需要与use-default-filters属性配合使用才能够达到“仅包含某些组件”这样的效果。即:通过将use-default-filters属性设置为false,禁用默认过滤器,然后扫描的就只是include-filter中的规则指定的组件了。
●context:exclude-filter子节点表示要排除在外的目标类,需要修改use-default-filters为true,先包含进来,才能进行过滤
组件装配
- 需求
Controller组件中往往需要用到Service组件的实例,Service组件中往往需要用到Repository组件的实例。Spring可以通过注解的方式帮我们实现属性的装配。
- 实现依据
在指定要扫描的包时,context:component-scan 元素会自动注册一个bean的后置处理器:AutowiredAnnotationBeanPostProcessor的实例。该后置处理器可以自动装配标记 了**@Autowired**、@Resource或@Inject注解的属性。
- @Autowired注解
①根据类型实现自动装配。
②构造器、普通字段(即使是非public)、一切具有参数的方法都可以应用@Autowired注解
③默认情况下,所有使用@Autowired注解的属性都需要被设置。当Spring找不到匹配的bean装配属性时,会抛出异常。
④若某一属性允许不被设置,可以设置@Autowired注解的required属性为 false
⑤默认情况下,当IOC容器里存在多个类型兼容的bean时,Spring会尝试匹配bean的id值是否与变量名相同,如果相同则进行装配。如果bean的id值不相同,通过类型的自动装配将无法工作。此时可以在@Qualifier注解里提供bean的名称。Spring甚至允许在方法的形参上标注@Qualifiter注解以指定注入bean的名称。 ⑥@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的bean进行自动装配。
⑦@Autowired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的bean。
⑧@Autowired注解用在java.util.Map上时,若该Map的键值为String,那么 Spring将自动装配与值类型兼容的bean作为值,并以bean的id值作为键。
- @Resource
@Resource注解要求提供一个bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为bean的名称。
- @Inject
@Inject和@Autowired注解一样也是按类型注入匹配的bean,但没有reqired属性。