Spring Bean

Spring Bean文件解析。

beans

1
2
3
4
<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">

xmlns是XML NameSpace的缩写,用来区分xml文件,类似java中的package。

xmlns:xsi是XML Schema Instance的缩写,是指具体用到的schema资源文件里定义的元素所遵守的规范。

xsi:schemaLocation是指文档遵守的xml规范,schemaLocation属性用来引用schema模式文档,解析器可以在需要的情况下使用该文档对XML实例文档校验。该值成对出现,第一个表示命名空间,的哥标示描述其命名空间的模式文档的具体问题。

bean

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 定义第一个Bean实例:bean1 -->
<bean id="bean1" class="com.Bean1" />

<!-- 定义第二个Bean实例:bean2 -->
<bean id="bean2" class="com.Bean2" />

</bean>

<bean>..</bean>代表一个bean实例,beans由多个bean实例组成。

id是bean的唯一标识符,容器对bean的管理、访问以及依赖都需要该标志。

class是bean的具体实现类,默认使用new关键字创建该bean实例。

bean作用域

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置一个singleton Bean实例:默认 -->
<bean id="bean1" class="com.Bean1" />
<!-- 配置一个prototype Bean实例 -->
<bean id="bean2" class="com.Bean2" scope="prototype"/>
</beans>

spring中bean共包含五种模式,默认的作用域为单例模式,使用scope来显示的设置作用域。

  • singleton单例模式,在每一个spring容器中,一个bean定义只有一个对象实例;
  • prototype原型模式,允许bean被多次实例,每次调用都创建一个实例;
  • request模式,在每一次http请求中,每个bean定义对应一个实例,该作用域在SpringMVC中才有效;
  • session模式,在一个http session中,每个bean定义对应一个实例,该作用域在基于web的spring mvc中才有效;
  • global-session模式,在一个全局http session中,每一个bean对应一个实例,该作用阈仅在Portlet Context才有效。

init-method&destroy-method

init-method属性指定一个方法,在实例化bean时,立即调用该方法。

1
<bean id="bean1" class="com.Bean1" init-method="init">
1
2
3
4
5
public class Bean1 {
public void init() {
...
}
}

destroy-method属性制定一个方法,从容器中移除bean之后,才调用该方法。

1
<bean id="bean1" class="com.Bean1" destroy-method="init">
1
2
3
4
5
public class Bean1 {
public void destrory() {
...
}
}

init-methoddestroy-method是无参函数。

此外,也可以通过实现InitializingBeanDisposableBean来实现初始化和销毁操作。

1
2
3
4
5
6
7
8
9
10
11
public class Bean1 implements InitializingBean {
public void afterPropertiesSet() {
...
}
}

public class Bean1 implements DisposableBean {
public void destroy() {
....
}
}

可以通过在beans属性中设置default-init-methoddefault-destroy-method来批量设置bean的初始化销毁操作

最后,还通过使用注解的方式来配置初始化和销毁操作,开启注解功能,

1
<context:annotation-config />

使用@PostConstruct注解初始化方法,使用@PreDestroy注解销毁方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class UserDAO {
@PreDestroy
public void destory()
{
System.out.println("销毁方法");
}
@PostConstruct
public void init()
{
System.out.println("初始化方法");
}
}

依赖注入

依赖注入就是把代码的依赖关系使用配置的方式组织起来,同时保证代码的独立性。

依赖注入包含两种:基于构造函数的依赖注入基于设值函数的依赖注入

基于构造函数

1
2
3
4
5
6
7
8
9
10
class Bean1 {
public Bean2 bean2;
public Bean1(Bean2 bean2) {
this.bean2 = bean2;
}
}

class Bean2 {
...
}

传统的编码方式使用代码硬编码依赖关系,

1
2
Bean2 bean2 = new Bean2();
Bean1 bean1 = new Bean1(bean2);

而使用依赖注入,会让代码的可维护性提高,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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-3.0.xsd">

<bean id="bean1" class="com.Bean1">
<constructor-arg ref="bean2"/>
</bean>

<bean id="bean2" class="com.Bean2"/>
</bean>
</beans>

基于设值函数

1
2
3
4
5
6
7
8
9
10
11
12
13
class Bean1 {
private Bean2 bean2;
public setBean2(Bean2 bean2) {
this.bean2 = bean2;
}
public getBean2() {
return bean2;
}
}

class Bean2 {
...
}

同样,使用代码硬编码依赖关系,

1
2
3
Bean2 bean2 = new Bean2();
Bean1 bean1 = new Bean1();
bean1.setBean2(bean2);

而使用依赖注入如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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-3.0.xsd">

<bean id="bean1" class="com.Bean1">
<property name="bean2" ref="bean2"/>
</bean>

<bean id="bean2" class="com.Bean2"/>
</beans>

p-namespace

使用p-namespace来简化配置,增加xmlns:p="http://www.springframework.org/schema/p"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="bean1" class="com.Bean1"
p:bean2-ref="bean2">
</bean>

<bean id="bean2" class="com.Bean2"/>
</beans>

注入集合类型参数

目前Spring提供了listsetmapprops

1
2
3
4
5
6
<property name="listParams">
<list>
<value>param1</value>
<value>param2</value>
</list>
</property>
1
2
3
4
5
6
<property name="setParams">
<set>
<value>param1</value>
<value>param2</value>
</set>
</property>
1
2
3
4
5
6
<property name="mapParams">
<map>
<entry key="key1" value="param1"/>
<entry key="key2" value="param2"/>
</map>
</property>
1
2
3
4
5
6
<property name="propsParams">
<props>
<prop key="key1">param1</prop>
<prop key="key2">param2</prop>
</props>
</property>