您的位置:首页 > 编程语言 > Java开发

根据Spring上下文对象xxxContext获得*.properties属性文件中的配置属性值

2018-01-12 11:17 573 查看
       最近写了一个文件上传和下载的简单测试demo,对于这种需要部署在服务器上的,而不是随便在本机上跑跑的那种,对于上传和下载的目录路径的选择和配置尤为重要,这个不能在代码里面写死,必须是灵活可配置的。

一、目录结构图



(1)说一下这个项目中的applicationContext.xml到底有什么作用

       以前不懂这个配置xml文件是干什么用的,直到在项目中的web.xml中没有添加节点<context-param>后,项目报错,大致意思就是说,Spring的Context【上下文】对象没有指定加载applicationContext.xml,当然,默认的param-value是这个文件名,我们也可以改成其它名称。



     

      我们一直听说Spring容器,但是这个Spring容器是个什么鬼,我们好像初学项目的时候都不会太在意和细细品味,相比较Spring-Boot来说,Spring要求的配置文件要多一些,那Spring-Boot作为Spring的孩子,为什么它的配置文件就要少很多呢,因为,Spring-Boot将一些配置(xml)都改为注解方式的Java类来实现了,而在项目启动的时候,Spring-Boot全局唯一application入口main方法启动的时候会自动加载这些配置类(当然,这里我们仍然需要借助一些注解来实现自动化),这种加载的原理其实,还是向Spring这个父容器里面加Bean,什么又是Bean呢?
通俗点说,就是一个Java应用,你可以理解为一个工具类,一个普通业务类,一个接口,或者一个XXX..etc,抽象点,就是,它就是Java对象。而在我们的Spring里面,虽然我们也可以手动的去创建一个Bean,比如在类上加@Service注解或者@Component注解,将其声明为Spring bean,但是,只这样做的话,我们的Spring识别不了,为什么呢,回到我们一开始说的,Spring容器!对了,不要忘了它,此时Spring容器里面并没有我们刚才创建的Bean,我们的Spring又不像其孩子Spring-Boot那样可以自动加载配置类并注入Bean,怎么办,是不是替它有点着急,是不是对我啰嗦的文字也有一点着急,哈哈,莫慌,如果你读懂了上面我所说的(前提是你也使用过Spring-Boot做过项目),下面,你会对applicationContext.xml的内容重新审视和重新认识:

针对本次演示的项目,其applicationContext.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 包扫描器,扫描带@Component注解的类 -->
<context:component-scan base-package="onegis.files.utils"></context:component-scan>
</beans>


      这个xml就是复制来复制去(网红脸),变的就是<beans...></beans>节点里面的内容,因此,我们很容易的理解,其内容是什么,没错,就是由一个个bean组成的beans,显然,这时候,我们似乎知道了,这个applicationContext.xml其实就是Spirng上下文容器的心脏,有了心脏,Spring容器才能接收(存放)更多的bean,容器里面放的当然就是各种bean了,Spring上下文对象初始化(创建)的时候,也就是项目启动的时候,我们就可以在代码中使用我们在Spring容器中注入的bean了



      Spring这个大容器里面,不单单存放Bean,还存放属性文件,因此,我们在Spring上下文对象初始化的时候,将属性文件properties中配置的信息也加载进去了



我们看下,整个项目的web入口,也就是web.xml里面,是怎么去初始化Spring上下文对象的

A.先来看一下,目录结构



B.再来看一下web.xml里面的内容

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>onegis.files</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC/spring-MVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>onegis.files.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ListFileServlet</servlet-name>
<servlet-class>onegis.files.servlet.ListFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListFileServlet</servlet-name>
<url-pattern>/listFile</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>onegis.files.servlet.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


注意两个关于Spring初始化很重要的事情:

1.Spring上下文对象容器的加载(初始化),没有这个,我们的Spring就炸了,项目就挂掉了



2.Spring(DispatcherServlet)前端控制器配置加载,没有这个,你就无法享用MVC的模式



我们看下这个spring-MVC.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<!-- 配置包扫描器 -->
<context:component-scan base-package="onegis.files.controller"></context:component-scan>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解析器  定义跳转的文件的前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 添加静态资源css、js映射文件 -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

<!-- 配置多媒体解析器  文件上传的时候 会用到-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值 5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
</beans>


内容这里就不多说了,很简单,分五块:

1.扫描Controller层,注入对应bean

2.注解驱动配置

3.视图解析器配置

4.静态资源映射

5.多媒体解析器配置,比如上传文件的时候会用到

至此,我们关于Spring配置文件的说明就不再阐述了,下面我们直接来看本篇的两个工具类

二、工具类

(1)根据属性key获取属性文件properties中的属性value

PropertiesUtils.java

import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
* 属性properties值获取工具
* @author Appleyk
* @date   2018年1月12日:上午9:12:29
*/
@Component //@Component注解声明为Spring bean
public class PropertiesUtils implements EmbeddedValueResolverAware {

private  StringValueResolver stringValueResolver;

public void setEmbeddedValueResolver(StringValueResolver resolver) {
stringValueResolver = resolver;
}

public  String getPropertiesValue(String name){
return stringValueResolver.resolveStringValue("${"+name+"}");

}
}


说明:当然我们也可以使用@Value注解的形式,获取对应的属性值

// xxx.properties配置项:
// server.ip=192.168.1.1
// server.port=8080

@Value("${server.ip}")
private String ip;


(2)获得Spring上下文对象,并取得属性key对应的value

SpringUtils.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Spring上下文 工具类
* @author Appleyk
* @date   2018年1月12日:上午9:37:58
*/
public class SpringUtils {

/**
* 拿到上下文对象,applicationContext.xml 注入bean【手动注入或者自动扫描注解注入bean】
* @return
*/
public static ApplicationContext getSpringContext(){
return new ClassPathXmlApplicationContext("classpath:springMVC/applicationContext.xml");
}

/**
* 根据.properties文件中的属性名称,拿到属性值
* @param proName
* @return
*/
public static String getPropertiesValue(String proName){
return getSpringContext().getBean(PropertiesUtils.class).getPropertiesValue(proName);
}
}


三、工具类测试使用

走一波Junit测试

import org.junit.Test;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;

import onegis.files.utils.SpringUtils;

@EnableSpringConfigured
public class PropertiesTest {

@Test
public void A() throws Exception{
System.out.println(SpringUtils.getPropertiesValue("LOG_SAVE_PATH"));
}
}




执行单元测试,查看输出结果



这样一来,我们就可以在项目中的servlet中,大胆放心的使用这种方式了



OK,项目跑一波



浏览器中输入地址: http://localhost:8082



最后附上整个demo的src源码:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐