前言

Spring是一个致力于降低Java开发复杂性的框架,其中最重要的部分就是依赖注入和切面编程。
本文主要讲解Spring的bean和它的装配即依赖注入的基本知识和用法。

Spring bean 简介

什么是Java bean
  1. 所有属性为private
  2. 提供默认构造方法
  3. 提供getter和setter
Spring bean的生命周期
  1. Spring对bean进行实例化;
  2. Spring将值和bean的引用注入到bean对应的属性中;
  3. 如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBean-Name()方法;
  4. 如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入;
  5. 如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext()方法,将bean所在的应用上下文的引用传入进来;
  6. 如果bean实现了BeanPostProcessor接口,Spring将调用它们的post-ProcessBeforeInitialization()方法;
  7. 如果bean实现了InitializingBean接口,Spring将调用它们的after-PropertiesSet()方法。类似地,如果bean使用init-method声明了初始化方法,该方法也会被调用;
  8. 如果bean实现了BeanPostProcessor接口,Spring将调用它们的post-ProcessAfterInitialization()方法;
  9. 此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁;
  10. 如果bean实现了DisposableBean接口,Spring将调用它的destroy()接口方法。同样,如果bean使用destroy-method声明了销毁方法,该方法也会被调用。
Spring bean的作用域
  1. 单例:应用中只创建一次,@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
  2. 原型:每次注入或者Spring通过上下文获取时都创建一个,@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  3. 会话:在web应用中,为每个会话创建一个实例
  4. 请求:在web应用中,为每个请求创建一个实例。

bean的装配(依赖注入)

在面向对象编程中,一个系统往往是由很多个类组成,这些类负责管理自己和与自己相互协作的对象的引用(依赖),这样就会导致高度耦合和不可测试的代码。通过依赖注入(DI),我们可以将依赖关系在创建对象的时候进行设定,对象无需自行创建和管理它们的依赖关系。举个例子,系统中有一个Person类,Person有一个方法是eat,eat依赖的另一个对象是Fruit,如果我们在Person类中定义一个Fruit属性,并在Person创建的时候也创建这个Fruit对象,那么这个Person和Fruit就耦合在一起了,想要改变Fruit为其他的东西就很麻烦。如果不在Person里面创建这个对象,而是将Fruit当做参数传给Person是不是会好很多?只要是Fruit的对象都可以,测试也会好做很多,先看这样的代码实现:

1
2
3
4
Person person = new Person("lkxiaolou");
Fruit fruit = new Fruit("apple");
person.setFruit(fruit);
person.eat();

这看起来没什么,如果Person类需要装载100个依赖呢?是不是得创建100个对象并且塞给Person?这个时候就该Spring出场了。

使用XML装配bean
  • 如果使用xml
    Spring通过ApplicationContext来装载bean,先在xml文件中配置,然后载入配置文件,就可以获取使用配置中的bean了。(记得放在resources目录下)
    先看个简单例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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="autopackage.Person">
<constructor-arg value="lkxiaolou" />
<constructor-arg ref="fruit" />
</bean>
<bean id="fruit" class="autopackage.Fruit">
<constructor-arg value="apple" />
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package autopackage;
public class Person {
private Fruit fruit;
private String name;
public Person(String name, Fruit fruit) {
this.name = name;
this.fruit = fruit;
}
public void eat() {
System.out.println(name+"正在吃"+fruit.getName()+"...");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
package autopackage;
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
package main;
import autopackage.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Person.xml");
Person person = (Person) context.getBean("person");
person.eat();
}
}

看完例子来介绍一下如何使用xml装配一个bean:

在xml中声明一个bean很简单

1
<bean id="person" class="leaningSpring.Person" />

或者带有构造器

1
2
3
<bean id="person" class="leaningSpring.Person">
<constructor-arg ref="fruit">
<bean/>

这里的ref中的值是bean的id,如果你想构造器是一个值,可以这样:

1
2
3
4
<bean id="person" class="leaningSpring.Person">
<constructor-arg value="lkxiaolou">
<constructor-arg ref="fruit">
<bean/>

或者是个集合呢?

1
2
3
4
5
6
7
8
<bean id="person" class="leaningSpring.Person">
<constructor-arg>
<list>
<ref bean="fruit" />
<ref bean="apple" />
</list>
</constructor-arg>
<bean/>

如果不是构造注入?而是属性注入呢?

1
2
3
<bean id="person" class="leaningSpring.Person">
<property name="fruit" ref="fruit" />
<bean/>

当然这只是最基本的用法,还有一些高级的用法会介绍。

使用注解装配bean

声明bean可以采取JavaConfig的方式,JavaConfig装载方式相比xml而言更强大、类型安全、易重构;通常将JavaConfig代码放在单独的包内以作区分。

使用JavaConfig声明一个bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package autopackage;
import org.springframework.stereotype.Component;
@Component
public class Person {
private Fruit fruit;
private String name;
public Person(String name, Fruit fruit) {
this.name = name;
this.fruit = fruit;
}
public void eat() {
System.out.println(name+"正在吃"+fruit.getName()+"...");
}
}

只是多了一个注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package autopackage;
import org.springframework.stereotype.Component;
@Component
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

这个是JavaConfig代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package config;
import autopackage.Fruit;
import autopackage.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {"autopackage"})
public class PersonConfig {
@Bean(name="person")
public Person person() {
return new Person("lkxiaolou", fruit());
}
@Bean(name="fruit")
public Fruit fruit() {
return new Fruit("origin");
}
}

测试一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main;
import autopackage.Person;
import config.PersonConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PersonMain2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = (Person) annotationConfigApplicationContext.getBean("person");
person.eat();
}
}

好了,上面的例子已经能看出简单的注入方式,当然依赖注入还有很多高级的用法,如Spring表达式等,这里就不再多说了,后续的文章再对这些做更进一步的讲解。