java文件读取web-inf下的spring的xml配置文件
来源:互联网 发布:nginx request body 编辑:程序博客网 时间:2024/05/14 16:22
最近在学习spring,就写了一个小程序,来验证spring的自动加载类的功能,主要测试下通过spring来自获取POJO类的实例,闲话少说,上代码:
先写一个POJO类 :
package com.jeecms.common.util;public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void test(){ System.out.println("name:"+getName()+" age:"+getAge()); } }
然后在写一个bean.xml :
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="person" class="com.jeecms.common.util.Person"> <property name="name" value="xingoo"/> <property name="age" value="12"/> </bean></beans>
最后在写一个测试类:
package com.jeecms.common.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author Tom */public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml"); Person p = app.getBean("person",Person.class);//创建bean的引用对象 p.test(); }}
这样基本完成了一个简单的装载注入,不过在使用ClassPathXmlApplicationContext时候发现, ClassPathXmlApplicationContext只能获取编译路径下的src,即你的.class文件目录下,不能获取其他路径的xml文件,不过我们可以使用FileSystemXmlApplicationContext,通过绝对的路径来获取XML文件,
ApplicationContext app = new FileSystemXmlApplicationContext("/WebContent/WEB-INF/config/bean.xml");
也或者通过配置 contextConfigLocation 来初始化所有相关的spring
<context-param> <!-- Context Configuration locations for Spring XML files --> <param-name>contextConfigLocation</param-name> <param-value> ./WEB-INF/applicationContext.xml </param-value> </context-param>
然后就可以通过实现ApplicationContextAware 接口,可获取ApplicationContext 实例
/** * 以静态变量保存Spring ApplicationContext,可在任意代码中取出ApplicaitonContext. * */public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext =applicationContext; } /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz) { checkApplicationContext(); return (T) applicationContext.getBeansOfType(clazz); } private static void checkApplicationContext() { if (applicationContext == null) throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil"); }}
0 0
- java文件读取web-inf下的spring的xml配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取Resource、WEB-INF目录下的配置文件
- Junit单元测试Spring 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Junit单元测试Spring 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- 读取web-inf目录下的spring配置文件
- Java读取WEB-INF目录下的properties配置文件
- 读取WEB-INF 下applicationContext.xml配置文件
- Junit 单元测试 Spring 读取WEB-INF下 xml 文件
- Struts 2 拦截器中得到bean以及读取WEB-INF下的spring的xml文件
- Java读取WEB-INF下XML文件并进行解析
- Java读取WEB-INF下XML文件并进行解析
- Spring+Junit 读取WEB-INF上的配置文件
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- 9-7节点属性
- yii2框架-yii2局部关闭(开启)csrf的验证(十七)
- 使用comm在java程序中管理本地端口
- java 内部类(inner class)详解
- 解决在部分手机webview字体显示过小的问题
- java文件读取web-inf下的spring的xml配置文件
- 相同HID设备的句柄获取
- 【算法设计与分析】墓地雕塑
- c#对两个txt文件的列合并
- 百度 进程调度算法
- 【杭电-oj】-1873-看病要排队(优先队列)
- PAT—1003. Emergency
- js、Jquery、AngularJs中的extend方法
- Tomcat类加载机制概述