[Spring] Spring学习笔记--IoC [原]

Spring学习进度比较缓慢,一方面自己没有投入太多的精力,很容易找些无关紧要的借口进行搪塞;另一方面,购买的《精通Spring》质量也不是太好,很多地方一点而过,没有讲清楚。一直知道自己不求甚解的毛病,所以逼促自己要弄清楚为止,不能得过且过,进度自然耽搁了不少。

下面笔记摘要,算是对《精通Spring》第三章IoC的补全,Spring的教材还是推荐SpringFramework中文论坛翻译的官方的《Spring 开发参考手册》。

(1) PropertyPlaceholderConfigurer 和 PropertyOverrideConfigurer
PropertyPlaceholderConfigurer 能够在Spring配置文件外部配置其它应用需要使用到的属性。最具代表是从properties中获取配置信息,并传递到Bean中。PropertyOverrideConfigurer和PropertyPlaceholderConfigurer类似,只是读取配置文件最后一个信息为准。

Example1:

PropertyBean.java:

package com.my.dome;

public class PropertyBean {

private String attr;

private int i;

private EasyBean easyBean;

private String aaa;

private String bbb;

public String getAttr() {
return attr;
}

public void setAttr(String attr) {
this.attr = attr;
}

public EasyBean getEasyBean() {
return easyBean;
}

public void setEasyBean(EasyBean easyBean) {
this.easyBean = easyBean;
}

public String getAaa() {
return aaa;
}

public void setAaa(String aaa) {
this.aaa = aaa;
}

public String getBbb() {
return bbb;
}

public void setBbb(String bbb) {
this.bbb = bbb;

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}

}


appcontext.xml:

   <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
            <value>conf.properties</value>
            </property>
            </bean>
            
            <bean id="PropertyBean" class="com.my.dome.PropertyBean">
            <property name="attr">
            <value>test</value>
            </property>
            <property name="i">
        <value>1</value>
        </property>
            <property name="easyBean">
            <ref local="EasyBean" />
            </property>
            <property name="aaa">
            <value>${aaa}</value>
            </property>
            <property name="bbb">
            <value>${bbb}</value>
            </property>
        </bean>

 

其中,${aaa}、${bbb}将从属性文件conf.properties中获取,并通过JavaBean的Setter方法设置到JavaBean里。

conf.properties:

aaa=111
bbb=222


Test.java:

package com.my.dome;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

public static void main(String[] args) throws Exception {
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);

//下面倆行非常关键,将BeanFactory和PropertyPlaceholderConfigurer绑定
PropertyPlaceholderConfigurer cfg = (PropertyPlaceholderConfigurer) factory
.getBean("propertyConfigurer");
cfg.postProcessBeanFactory((ConfigurableListableBeanFactory) factory);

PropertyBean propertyBean = (PropertyBean) factory
.getBean("PropertyBean");
System.out.println("aaa="+propertyBean.getAaa());
System.out.println("bbb="+propertyBean.getBbb());

}
}

程序输出:

aaa=111
bbb=222


(2) BeanFactoryAware

将应用的BeanFactory实例注入到JavaBean实例中。

Example2(基于Example1):

AwareBean:

package com.my.dome;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class AwareBean implements BeanFactoryAware {

BeanFactory factory;

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
factory = beanFactory;
}

//需要实现的方法,将factory注入进来
public PropertyBean getPropertyBean() {
PropertyPlaceholderConfigurer cfg = (PropertyPlaceholderConfigurer) factory
.getBean("propertyConfigurer");
cfg.postProcessBeanFactory((ConfigurableListableBeanFactory) factory);

PropertyBean propertyBean = (PropertyBean) factory
.getBean("PropertyBean");

return propertyBean;
}

}


appcontext.xml:

<bean id="AwareBean" class="com.my.dome.AwareBean">    
    </bean>        


Test.java:

package com.my.dome;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

public static void main(String[] args) throws Exception {
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);

AwareBean awareBean = (AwareBean) factory.getBean("AwareBean");
System.out.println(awareBean.getPropertyBean().getAaa());

}
}

posted @ 2006-08-24 18:12  vanuan  阅读(132)  评论(0)    收藏  举报