spring配置文件占位符的使用
定义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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:application="http://www.foo.com/schema"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.foo.com/schema http://www.foo.com/schema/application.xsd">
<!--测试IOC DI-->
<!--定义demo 类-->
<!--<bean id="demo" class="com.foo.Demo"></bean>-->
<context:property-placeholder location="classpath:context.properties"/>
<!--定义一个demotest类-->
<!--<bean id="demoTest" class="com.foo.DemoTest">-->
<!--<property name="list">-->
<!--<list merge="true" value-type="java.lang.String">-->
<!--<value>zhangsan</value>-->
<!--<value>wangwu</value>-->
<!--<value>zhaoliu</value>-->
<!--<value>zhangsan</value>-->
<!--<value>zhaoliu</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<application:dubbo name="${mysql}" age="18"></application:dubbo>
<!--初始化beanPostProcess-->
<!--该方法主要在类初始化时候调用-->
<!--<bean id="beanPostProcess" class="com.foo.BeanPostProcess"></bean>-->
<!--<bean></bean>-->
<!--<bean id="wuliang" class="com.foo.Hello">-->
<!--<property name="demo" ref="demo"></property>-->
<!--<property name="demoTest" ref="demoTest"></property>-->
<!--</bean>-->
<!--测试IOC DI-->
<!--开启aop配置 :aspectj-autoproxy-->
<!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->
<!--<bean id="daoImpl" class="com.foo.aop.impl.DaoImpl"></bean>-->
<!--<bean id="timeHander" class="com.foo.aspect.TimeHandler"></bean>-->
<!--<aop:config>-->
<!--<aop:aspect id="time" ref="timeHander">-->
<!--<aop:pointcut id="addAllMethod" expression="execution(* com.foo.aop.Dao.*(..))"></aop:pointcut>-->
<!--<aop:before method="printTime" pointcut-ref="addAllMethod"></aop:before>-->
<!--<aop:after method="printTime" pointcut-ref="addAllMethod"></aop:after>-->
<!--</aop:aspect>-->
<!--</aop:config>-->
</beans>
定义context.properties
mysql=mysql
使用
package com.foo;
import com.ext.Bean;
import com.foo.aop.Dao;
import com.foo.namespace.entity.Hero;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.adapter.AdvisorAdapter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class TestDemo {
public static void main(String[] args) {
//根据 Xml 配置文件创建 Resource 资源对象,该对象中包含了 BeanDefinition 的信息
// ClassPathResource resource =new ClassPathResource("application-context.xml");
// //FileSystemXmlApplicationContext
// //创建 DefaultListableBeanFactory
// DefaultListableBeanFactory factory =new DefaultListableBeanFactory();
// //创建 XmlBeanDefinitionReader 读取器,用于载入 BeanDefinition。之所以需要 BeanFactory 作为参数,是因为会
//
// // 注册事件
// // 可以注册多个beanpostprocessor事件,他们的顺序是根据bean的注册顺序进行调用。
// // factory.addBeanPostProcessor(new BeanPostProcess());
// // factory.addBeanPostProcessor(new BeanPostProcessDemo());
// // 将读取的信息回调配置给 factory
// XmlBeanDefinitionReader reader =new XmlBeanDefinitionReader(factory);
// //XmlBeanDefinitionReader 执行载入 BeanDefinition 的方法,最后会完成 Bean 的载入和注册。完成后 Bean 就成功
// // 的放置到 IOC 容器当中,以后我们就可以从中取得 Bean 来使用
//
// reader.loadBeanDefinitions(resource);
// AdvisorAdapter
// Hello hello = (Hello) factory.getBean("wuliang");
// hello.get();
// AdvisedSupport
ApplicationContext ac = new ClassPathXmlApplicationContext("application-context.xml");
Bean bean = (Bean) ac.getBean(Bean.class.getName());
System.out.println("name: " + bean.getName() + " age: " + bean.getAge());
// DemoTest demoTest = (DemoTest) ac.getBean("demoTest");
// System.out.println(demoTest.getList().size());
// System.getProperties();
// try{
// Thread.sleep(10000000);
// }catch (Exception e) {
// e.printStackTrace();
// }
// AdvisedSupport
}
}
第九章 结合源码篇