首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

【转】spring application.xml在项目中的几种解析形式

2012-09-13 
【转】spring application.xml在项目中的几种解析方式解析 在java项目中的spring applicationContext.xml文

【转】spring application.xml在项目中的几种解析方式
解析 在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页面用<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;
    }
}

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

- <bean id="config" value="${connection.driver_class}" />
- <property name="username">
  <value>${connection.username}</value>
  </property>
- <property name="password">
  <value>${connection.password}</value>
  </property>
  </bean>
- <bean id="sessionfactroy" 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 />
  </entry>
  </map>
  </property>
  </bean>
- <bean id="transactionManager" ref="sessionfactroy" />
  </bean>
- <!-- DAO对象注入  -->
- <bean id="cgdddaoimpl" ref="sessionfactroy" />
  </bean>
- <bean id="cgshddaoimpl" ref="sessionfactroy" />
  </bean>
- <bean id="gysdaoimpl" ref="sessionfactroy" />
  </bean>
- <!-- 业务层对象注入  -->
- <bean id="cgddservice" ref="cgdddaoimpl" />
  </bean>
- <bean id="cgshdservice" ref="cgshddaoimpl" />
  </bean>
- <bean id="gysservice" 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" />
- <bean id="cgddAction" ref="cgddservice" />
  </bean>
- <bean id="cgshdAction" ref="cgshdservice" />
  </bean>
- <bean id="gysAction" ref="gysservice" />
  </bean>
  </beans>



转自:http://blog.csdn.net/KOOK_OKKO/archive/2009/06/30/4310839.aspx

热点排行