spring application.xml在项目中的几种解析方式

转载自: http://blog.csdn.net/kit_lo/article/details/2230267



解析 在java项目中的spring applicationContext.xml文件的几种解析方式:
一种:
ApplicationContext cxt = new ClassPathXmlApplicationContext(“applicationContext.xml”);
cxt.getBean(“在applicationContext.xml中bean的id”);

二种:
ApplicationContext cxt =
 new FileSystemXmlApplicationContext(“applicationContext.xml的绝对路径”);
cxt.getBean(“在applicationContext.xml中bean的id”);

三种:
Resource res= new ClasspathResource(“applicationContext.xml”);
XmlBeanFactory factory = new XmlBeanFactory(res);
factory.getBean(“applicationContext.xml中的bean的id”); 

第四种:在web项目中解析applicationContext.xml,此方法本人感觉很好用,如在struts2.0+hibernate3.0+sprnig2.0中,某jsp页面须用daoImpl(业务实现类的一个方法),一般写法是在action中调用该方法,写不可以直接使用,因为所有东东都交给spring管理了,但是在原来的基础上加上以下这个类,做法就多了,我们可以在该jsp页面用 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; 
    }
}

 以下为applicationContext.xml完整配置文件,供参考

-  < 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 >
 
-  < 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 >
 
   < 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 >

你可能感兴趣的:(spring application.xml在项目中的几种解析方式)