获取spring的applicationContext.xml内的Bean实体
时间:2010-08-02 来源:fucuihong
一般情况下,我们都是使用webProject开发网站之类的web项目,一般在applicationContext.xml文件中配置的bean实体都由spring负责注入,我们只需在web.xml中加载applicatinContext.xml文件并在需要使用的地方写个此bean的getter/setter方法即可。例如:
web.xml文件中加载application.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/applicationContext-alarm.xml,
/WEB-INF/classes/applicationContext-demo.xml
</param-value>
</context-param> applicationContext.xml中注入了: <bean id="CompareChartDao"
class="pas.dao.comparechart.CompareChartDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <bean id="LayoutDao" class="pas.dao.layout.LayoutDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> ... ... 那么在需要使用以上两个bean的类中,则需要: private CompareChartDao compareChartdao;
private LayoutDao layoutDao; public CompareChartDao getCompareChartdao() {
return compareChartdao;
} public void setCompareChartdao(CompareChartDao compareChartdao) {
this.compareChartdao = compareChartdao;
} public LayoutDao getLayoutDao() {
return layoutDao;
} public void setLayoutDao(LayoutDao layoutDao) {
this.layoutDao = layoutDao;
} public useTwoBean() { //直接使用compareChartdao和layoutDao变量即可。 String configurationId=21; String widgetId=32; compareChartdao.deleteCompareChart(configurationId); Widget widget = layoutDao.getWidget(widgetId); } 以上是开发webProject项目时获取spring的applicationContext.xml内的Bean实体的方法;由于公司最近要开发一个非web项目,非web项目没有web.xml启动时加载applicationContext.xml,那么怎么样来获取applicationContext.xml中注册的实体呢? 例如我的applicationContext.xml内容如下: <!--hibernate sessionfactory manager-->
<bean id="pasThresholdMgtSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
scope="singleton"> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.proxool.xml">proxool.xml</prop>
<prop key="hibernate.proxool.pool_alias">demo</prop>
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</prop>
<prop key="hibernate.jdbc.fetch_size">20</prop>
<prop key="hibernate.default_batch_fetch_size">8</prop>
<prop key="hibernate.jdbc.batch_size">20</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_structured_entries">
true
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.connection.autocommit">true</prop> </props>
<!--
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.jdbc.fetch_size=0
hibernate.jdbc.batch_size=50
hibernate.cglib.use_reflection_optimizer=true
hibernate.hbm2ddl.auto=update
hibernate.cache.use_query_cache=true
hibernate.proxool.xml=proxool.xml
hibernate.proxool.pool_alias=ifms
hibernate.cache.use_structured_entries=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
-->
</property>
<property name="mappingLocations">
<list>
<value>src/hbm/*.hbm.xml</value>
</list>
</property>
</bean> <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
scope="singleton">
<property name="sessionFactory" ref="pasThresholdMgtSessionFactory" />
</bean> <bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<constructor-arg ref="pasThresholdMgtSessionFactory" />
</bean> 我本人采取的方式为: ApplicationContext act = new FileSystemXmlApplicationContext("src\\applicationContext.xml");//加载applicationContext内容 //获取并使用applicationContext.xml中的实体bean HibernateTemplate htemplate=(HibernateTemplate)act.getBean("hibernateTemplate");
Rule rule=(Rule)htemplate.get(Rule.class, "1");
System.out.println("id: "+rule.getID()); 以上仅为本人自己的一点研究,如有不对请大家纠正,特写在此,以备后用。
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/applicationContext-alarm.xml,
/WEB-INF/classes/applicationContext-demo.xml
</param-value>
</context-param> applicationContext.xml中注入了: <bean id="CompareChartDao"
class="pas.dao.comparechart.CompareChartDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <bean id="LayoutDao" class="pas.dao.layout.LayoutDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> ... ... 那么在需要使用以上两个bean的类中,则需要: private CompareChartDao compareChartdao;
private LayoutDao layoutDao; public CompareChartDao getCompareChartdao() {
return compareChartdao;
} public void setCompareChartdao(CompareChartDao compareChartdao) {
this.compareChartdao = compareChartdao;
} public LayoutDao getLayoutDao() {
return layoutDao;
} public void setLayoutDao(LayoutDao layoutDao) {
this.layoutDao = layoutDao;
} public useTwoBean() { //直接使用compareChartdao和layoutDao变量即可。 String configurationId=21; String widgetId=32; compareChartdao.deleteCompareChart(configurationId); Widget widget = layoutDao.getWidget(widgetId); } 以上是开发webProject项目时获取spring的applicationContext.xml内的Bean实体的方法;由于公司最近要开发一个非web项目,非web项目没有web.xml启动时加载applicationContext.xml,那么怎么样来获取applicationContext.xml中注册的实体呢? 例如我的applicationContext.xml内容如下: <!--hibernate sessionfactory manager-->
<bean id="pasThresholdMgtSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
scope="singleton"> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.proxool.xml">proxool.xml</prop>
<prop key="hibernate.proxool.pool_alias">demo</prop>
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</prop>
<prop key="hibernate.jdbc.fetch_size">20</prop>
<prop key="hibernate.default_batch_fetch_size">8</prop>
<prop key="hibernate.jdbc.batch_size">20</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_structured_entries">
true
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.connection.autocommit">true</prop> </props>
<!--
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.jdbc.fetch_size=0
hibernate.jdbc.batch_size=50
hibernate.cglib.use_reflection_optimizer=true
hibernate.hbm2ddl.auto=update
hibernate.cache.use_query_cache=true
hibernate.proxool.xml=proxool.xml
hibernate.proxool.pool_alias=ifms
hibernate.cache.use_structured_entries=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
-->
</property>
<property name="mappingLocations">
<list>
<value>src/hbm/*.hbm.xml</value>
</list>
</property>
</bean> <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
scope="singleton">
<property name="sessionFactory" ref="pasThresholdMgtSessionFactory" />
</bean> <bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<constructor-arg ref="pasThresholdMgtSessionFactory" />
</bean> 我本人采取的方式为: ApplicationContext act = new FileSystemXmlApplicationContext("src\\applicationContext.xml");//加载applicationContext内容 //获取并使用applicationContext.xml中的实体bean HibernateTemplate htemplate=(HibernateTemplate)act.getBean("hibernateTemplate");
Rule rule=(Rule)htemplate.get(Rule.class, "1");
System.out.println("id: "+rule.getID()); 以上仅为本人自己的一点研究,如有不对请大家纠正,特写在此,以备后用。
相关阅读 更多 +