Java加载资源文件以及对spring的父子容器简单的介绍

我们在项目中开发过程中经常遇到一个问题就是,该如何去加载资源文件。其实目前在项目中很多资源文件都不需要我们手工去编写代码来加载资源文件了,大部分都是框架已经给我们加载好,我们只需要去配置一下就可以了。今天,我就来总结一下如何从不需要要任何框架到使用框架的过程中加载资源文件的方法。

方式一:使用Java的字节码对象Class来加载资源文件

这种方式是通过Class对象来获取资源文件路径:

    @Test
    public void testLoadProperties() throws Exception{
        String path = this.getClass().getResource("/db.properties").getPath();
        FileInputStream fileInputStream = new FileInputStream(new File(path));
        Properties prop = new Properties();
        prop.load(fileInputStream);
        System.out.println(prop.getProperty("db.driver"));
        System.out.println(prop.getProperty("username"));
        fileInputStream.close();
    }

这种方式是通过Class对象来获取资源文件,并返回流的形式

    @Test
    public void test2() throws IOException{
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/db.properties");
        Properties prop = new Properties();
        prop.load(resourceAsStream);
        System.out.println(prop.getProperty("db.driver"));
        System.out.println(prop.getProperty("username"));
        resourceAsStream.close();
    }

注意:在获取资源文件的时候一定要在获取资源文件前面加上”/”,表示从根目录中寻找,如果没有加”/”会报错java.lang.NullPointerExceptioni异常,因此在项目中开发过程中也需要捕获异常哦!

方式二:利用Java中工具类ResourceBundle类

    @Test
    public void testResourceBundle(){
        ResourceBundle bundle = ResourceBundle.getBundle("db");//基名
        System.out.println(bundle.getString("db.driver"));
        System.out.println(bundle.getString("username"));
    }

注意:注意ResourceBundle工具类只能获取properties资源文件。并且在获取properties文件时不需要写全名,只需要写基名(例如:db.properties的基名就是db)

方式三:在Web项目中,通过ServletContext来加载资源文件

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = getServletContext().getRealPath("db.properties");
        FileInputStream fileInputStream = new FileInputStream(new File(path));
        Properties prop = new Properties();
        prop.load(fileInputStream);
        System.out.println(prop.getProperty("db.driver"));
        System.out.println(prop.getProperty("username"));
        fileInputStream.close();
    }

注意:ServletContext只能用于用于WEB项目中,在Java项目中找不到路径。ServletContext的getRealPath()方法选择路径是从Web项目中编译后的根目录寻找,如果资源文件没有放在WebRoot目录下,而是放在src目录中,在编译后该资源文件却没有在根目录中,而是在根目录中的WEB-INF/classes目录中,因此,在寻找在src源码路径下的资源文件,需要加个头/WEB-INF/classes(这就是我们在配置配置Spring文件时需要在路径前面加”classpath:”)。

方式四:在Struts2项目中,通过国际化加载资源文件

加载资源文件
通过在Struts2配置文件中配置加载资源文件

<constant name="struts.custom.i18n.resources" value="baseName"/><!--baseName:基名-->

struts2中的资源文件的定义有3中方式:
- aseName_language_country.properties
- baseName_language.properties
- baseName.properties
读取资源文件
获取资源文件中的内容是通过ActionSupport中的getText(key)方法,在继承了ActionSupport类中直接使用getText(key)获取资源文件中的内容。
strut2还可以在页面中通过struts便签从获取资源文件中的内容;通过:

<s:text name="key"/>

方式五:在Spring或者SpringMVC项目中,可以通过扫描方式

在Spring或者SpringMVC项目中,可以在Spring或者SpringMVC配置文件中开启扫描,这样,就可以把所有的资源文件都扫描到Spring容器中。
加载资源文件:

    <!-- 加载资源文件 -->
    <context:property-placeholder location="classpath:properties/*.properties" />

通过上面的配置文件就把properties文件扫描到spring容器中去了。后面我们需要资源文件中的内容就可从spring容器中获取。
读取资源文件中的内容:
在Spring配置中获取资源文件可以通过${key}来获取资源文件中value值。
在Java代码中,可以通过注解@Value(“key”)把资源文件中value值注入给属性keyName

@Value(key)
private String keyName;

注意:在这里存在Spring父子容器的关系
Spring容器中父子容器:
在Spring和SpringMVC父子容器中:父容器不能访问或者注入子容器的对象,子容器可以访问或者注入父容器中的对象。但是不能注入父容器的属性值;
例子:
spring配置文件:在Spring中加载资源文件。

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:properties/*.properties" />
    <!-- 包扫描器,扫描带@Service注解的类 -->
    <context:component-scan base-package="com.lc.service"></context:component-scan>

springmvc配置文件:

      <!-- 配置包扫描器 -->    
    <context:component-scan base-package="com.lc.controller"></context:component-scan>

在父(spring)容器中可以通过属性注入资源文件中值。
但是在springmvc扫描的com.lc.controller包下的所有类都不能通过@Value注解注入资源文件中的值。因为子容器获取不到父容器中的属性值,但是能获取到对象。(解释spring的父子容器可能不是很清晰,观客如果有什么不懂得,可以去看看别人的博客进一步了解下。)
如果子容器需要获取资源文件中内容,可以在springmvc配置文件中也加载资源文件。这样资源文件中的内容在父子容器中都存在了。
通过spring和springmvc父子容器的了解,我们在开发过程中,如果分模块开发,千万不能把Service层让注入到springmvc子容器中,这样的话,Service层中的事务可能就失效了,会让程序出现bug。在分模块开发中,我们一般把Controller层放到springmvc子容器中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值