解析 在java項目中的spring applicationContext.xml文件的幾種解析方式:
一種:
ApplicationContext cxt = new ClassPathXmlApplicationContext(「applicationContext.xml」);
cxt.getBean(「在applicationContext.xml中bean的id」); java
二種:
ApplicationContext cxt =
new FileSystemXmlApplicationContext(「applicationContext.xml的絕對路徑」);
cxt.getBean(「在applicationContext.xml中bean的id」); web
三種:
Resource res= new ClasspathResource(「applicationContext.xml」);
XmlBeanFactory factory = new XmlBeanFactory(res);
factory.getBean(「applicationContext.xml中的bean的id」); spring
第四種:在web項目中解析applicationContext.xml,此方法本人感受很好用,如在struts2.0+hibernate3.0+sprnig2.0中,某jsp頁面須用daoImpl(業務實現類的一個方法),通常寫法是在action中調用該方法,寫不能夠直接使用,由於全部東東都交給spring管理了,可是在原來的基礎上加上如下這個類,作法就多了,咱們能夠在該jsp頁面用<s:bean id="aa" name="org.xxxx.Gean"> <s:set name="bb" value="%{#aa.getBean('此處爲applicationContext.xml中所寫的daoImpl的bean的id')}"> bb就是咱們所要的方法的返回值.
public class GetBean {
private XmlBeanFactory factory;
public GetBean()
{
Resource res=new ServletContextResource(ServletActionContext.getServletContext(),"/WEB-INF/transaction.xml");
factory=new XmlBeanFactory(res);
}
public Object getBean(String beanname)
{
XmlWebApplicationContext ctx=new XmlWebApplicationContext();
ServletContext servletcontext=ServletActionContext.getServletContext();
ctx.setServletContext(servletcontext);
ctx.setConfigLocations(new String[]{"/WEB-INF/transaction.xml"});
ctx.refresh();//由servletContext初始化beanfactory須要的配置文件,進而加載該配置文件
Object obj=ctx.getBean(beanname);
return obj;
}
} sql
如下爲applicationContext.xml完整配置文件,供參考 session
- <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
- <bean id="jdbc" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="url">
<value>${connection.url}</value>
</property>
<property name="driverClassName" value="${connection.driver_class}" />
- <property name="username">
<value>${connection.username}</value>
</property>
- <property name="password">
<value>${connection.password}</value>
</property>
</bean>
- <bean id="sessionfactroy" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="jdbc" />
- <property name="hibernateProperties">
- <props>
<prop key="hibernate.dialect">${dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
- <property name="mappingDirectoryLocations">
<value>/WEB-INF/classes/org/itfuture/wuliu/hbm</value>
</property>
- <property name="eventListeners">
- <map>
- <entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionfactroy" />
</bean>
- <!-- DAO對象注入 -->
- <bean id="cgdddaoimpl" class="org.itfuture.wuliu.daoimpl.CgddDaoimpl">
<property name="sessionFactory" ref="sessionfactroy" />
</bean>
- <bean id="cgshddaoimpl" class="org.itfuture.wuliu.daoimpl.CgshdDaoimpl">
<property name="sessionFactory" ref="sessionfactroy" />
</bean>
- <bean id="gysdaoimpl" class="org.itfuture.wuliu.daoimpl.GysDaoImpl">
<property name="sessionFactory" ref="sessionfactroy" />
</bean>
- <!-- 業務層對象注入 -->
- <bean id="cgddservice" class="org.itfuture.wuliu.serviceimpl.CgddServiceImpl">
<property name="cgdddao" ref="cgdddaoimpl" />
</bean>
- <bean id="cgshdservice" class="org.itfuture.wuliu.serviceimpl.CgshdServiceImpl">
<property name="cgshddao" ref="cgshddaoimpl" />
</bean>
- <bean id="gysservice" class="org.itfuture.wuliu.serviceimpl.GysServiceImpl" scope="prototype">
<property name="gysdao" ref="gysdaoimpl" />
</bean>
- <aop:config>
<aop:advisor pointcut="execution(* *..CgddDao.*(..))" advice-ref="txAdvice" />
</aop:config>
- <tx:advice id="txAdvice">
- <tx:attributes>
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
- <!--Action-->
<bean id="baseaction" class="org.itfuture.wuliu.action.BaseAction" />
- <bean id="cgddAction" class="org.itfuture.wuliu.action.CgddAction">
<property name="cgddservice" ref="cgddservice" />
</bean>
- <bean id="cgshdAction" class="org.itfuture.wuliu.action.CgshdAction">
<property name="cgshdservice" ref="cgshdservice" />
</bean>
- <bean id="gysAction" class="org.itfuture.wuliu.action.GysAction">
<property name="gysservice" ref="gysservice" />
</bean>
</beans> app
轉自:http://blog.csdn.net/KOOK_OKKO/archive/2009/06/30/4310839.aspx jsp