原文:
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页面用<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;
}
}
分享到:
相关推荐
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
一、Idea关联的maven本地仓库配置...三、3个可单独使用的,maven项目文件pom.xml自定义配置 pom-maven-springboot-CusConfigV1.xml pom-maven-springboot-CusConfigV2.xml pom-maven-spring-CusConfigV1不推荐使用.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - ...
Spring项目application.xml配置文件加解密
放在项目的resources下即可,用于配置日志打印在控制台的格式等信息,并且也可以调日志信息打印出来的颜色,还能够使日志信息记录在日志文件上并保存在项目根路径下,springboot则用此logback-spring.xml命名,若是maven...
spring pom.xml
这是一个springmvc-config.xml文件,<?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:...
config.properties:数据库配置文件 log4j.properties:mybatis日志文件 spring-mvc.xml:spring-MVC配置文件 spring-mybatis.xml:mybatis的配置文件 spring.xml
ssm配置文件spring.xml
Spring.xml模版
you will learn best practices through real-world projects and follow a simple, practical approach to developing high performance and enterprise-grade Java applications with Spring. Starting with the...
Spring Boot技术知识点:如何获取application.yml配置文件里的相关属性
初次使用ssm框架的朋友可以看一下,希望对你有所帮助
spring-service.xml
spring-rabbitMQ.xml文件
spring在web.xml中和在struts中的不同配置..pdf
最简单的springboot + mybatis +mapper.xml 配置项目 简单配置java spring boot项目