pring 加载多个applicationContext
大家可以简单的看一下下面的代码,顺着看一遍,就一目了然了。这是我以前记录的跟大家分享一下。
// java 类 MyDataSource 是 .DAO 类
public static void util(){
ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{"applicationContext-database.xml","applicationContext-ibatis.xml","applicationContext-beans.xml"});
BeanFactory factory = resource;
MyDataSource myData=(MyDataSource)factory.getBean("myDataSource");
}
// 三个bean文件配置
applicationContext-database.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@172.16.3.67:1521:orcl</value></property>
<property name="username"><value>zfoa</value></property>
<property name="password"><value>zfoa</value></property>
</bean>
</beans>
applicationContext-ibatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "spring-beans.dtd">
<beans>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml"/>
<property name="dataSource"ref="dataSource"/>
</bean>
</beans>
applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd">
<beans>
<bean id="myDataSource" class="com.boco.dao.MyDataSource">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
|
|