解析 在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