java文件读取web-inf下的spring的xml配置文件

最近在学习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");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值