PropertyPlaceholderConfigurer使用笔记
简而言之:就是在spring的配置文件中(.xml)加入外部文件
PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。关于BeanFactoryPostProcessor和BeanPostProcessor类似。我会在其他地方介绍。
PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。这样的话,我只需要对properties文件进行修改,而不用对xml配置文件进行修改。
作用是什么呢?
有一些属性值不需要经常变更,但是有一些属性值可能随时改变,把经常会改动的属性值放在另一个文件中的话,程序使用起来也更方便。
下面将通过一个例子来理解PropertyPlaceholderConfigurer。
首先是配置文件,代码如下:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
<bean id="student" class="co.jp.beanFactoryPost.Student">
<property name="name">
<value>
${name}
</value>
</property>
<property name="age">
<value>
${age}
</value>
</property>
<property name="birth">
<value>
${birth}
</value>
</property>
</bean>
</beans>
在这个配置文件中最重要的就是下面这段代码,
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
PropertyPlaceholderConfigurer会读取location所设定的属性文件,并将properties文件中的值,设定给${name},${age},${birth}从而完成依赖注入。
相应的properties文件的定义如下:
name=xiaohailinage=27birth=19820123
这里用到的student bean,的class的代码如下:
比较简单,主要是一个get,set方法
public class Student {
private String name;
private String age;
private String birth;
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public String getBirth() {
return this.birth;
}
}
接着,写一个测试的主类,代码如下:
public class PropertyHolderPlaceDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"propertyPlaceHolder.xml");
Student student = (Student) ctx.getBean("student");
System.out.println(student.getAge());
System.out.println(student.getBirth());
System.out.println(student.getName());
}
}
web.xml配置信息
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter> 使用spring的代理filter
延迟初始化bean:lazy-init="true"/ <bean>标签属性 或者 <beans>标签上的default-lazy-init="true
需要说明的是,如果一个bean被设置为延迟初始化,而另一个非延迟初始化的singleton bean依赖于它,那么当ApplicationContext提前实例化singleton bean时,它必须也确保所有上述singleton 依赖bean也被预先初始化,当然也包括设置为延迟实例化的bean。因此,如果Ioc容器在启动的时候创建了那些设置为延迟实例化的bean的实例,你也不要觉得奇怪,因为那些延迟初始化的bean可能在配置的某个地方被注入到了一个非延迟初始化singleton bean里面。
自动装配(autowire)
表 3.2. Autowiring modes
模式 | 说明 |
no |
|
byName | 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含 master 属性(同时提供 setMaster(..) 方法),Spring就会查找名为 master 的bean定义,并用它来装配给master属性。 |
byType | 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用 byType 方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置 dependency-check="objects" 让Spring抛出异常。 |
constructor | 与 byType 的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。 |
autodetect | 通过bean类的自省机制(introspection)来决定是使用 constructor 还是 byType 方式进行自动装配。如果发现默认的构造器,那么将使用 byType 方式。 |
autowire-candidate = “false” 属性可以将设置不自动装配
default-autowire-candidates <bean>属性 可以使用bean名字进行自动装配
比如,将自动装配限制在名字以'Repository'结尾的bean,那么可以设置为"*Repository“
idref元素
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
上述bean定义片段完全地等同于(在运行时)以下的片段:
<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
<property name="targetName" value="theTargetBean" />
</bean>
依赖检查
表 3.3. 依赖检查方式
模式 | 说明 |
none | 没有依赖检查,如果bean的属性没有值的话可以不用设置。 |
simple | 对于原始类型及集合(除协作者外的一切东西)执行依赖检查 |
object | 仅对协作者执行依赖检查 |
all | 对协作者,原始类型及集合执行依赖检查 |
方法注入()
lookup-method
replaced-method:不常使用
bean的作用域
表 3.4. Bean作用域
作用域 | 描述 |
在每个Spring IoC容器中一个bean定义对应一个对象实例。 | |
一个bean定义对应多个对象实例。 | |
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext 情形下有效。 | |
在一个HTTP Session 中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext 情形下有效。 | |
在一个全局的HTTP Session 中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext 情形下有效。 |
根据经验
对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
初始化回调(如果想在初始化时做些事情可使用初始化回调)
方法一:
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
方法二:
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
析构回调
析构回调的方式也有两种原理同初始化回调
implements DisposableBean
destroy-method="methodName
default-init-method="init"" : 可以配置全局初始化方法名称