springMVC子容器使用spring父容器配置文件资源

来源:互联网 发布:网络斗牛赌博作弊器 编辑:程序博客网 时间:2024/06/08 04:57

知识点:

1.子容器可以访问父容器的资源bean,父容器不可以访问子容器资源bean

2.spring配置读取配置文件属性例如jdbc.properties,父容器中可以使用@value("${key}") 对properties中的key获取其value值赋值给成员变量

@value注解 是在所有bean创建成功之后才执行,且只能在当前容器的查找数据


主要过程:

1.添加properties属性文件

2.spring配置文件配置读取该peoperties属性文件

3.在spring 中创建一个bean,使用@value读取并复制要使用的 key-value

4.将spring中的bean注入到springMVC容器中使用


详细过程:


子容器中使用父容器的properties配置文件变量:
设置配置文件  evn.properties
内容:PATH=D:\mysetup\images
URL=image.test.com
spring父容器配置文件加载applicationscontext.xml中读取该配置文件,配置如下
<!-- 使用spring自带的占位符替换功能 --><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!-- 允许JVM参数覆盖 --><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><!-- 忽略没有找到的资源文件 --><property name="ignoreResourceNotFound" value="true" /><!-- 配置资源文件 --><property name="locations"><list><value>classpath:env.properties</value></list></property></bean>

 使用 @value注解 读取属性值 @value是在所有bean创建成功后再执行   @value注解只会在当前容器中查询数据

在spring父容器中读取属性文件中的值
@Servicepublic class PropertiesService {@Value("${URL}")public String PIC_MANAGE_URL;@Value("${PATH}")public String PIC_MANAGE_PATH;}

在将父容器中的bean注入到子容器中,子容器即可使用bean的属性值
@Autowiredprivate PropertiesService propertiesService ;
String fileFolder = this.propertiesService.PATH + File.separator + "images" ;



子容器中使用父容器的properties配置文件变量:
设置配置文件  evn.properties
内容:PATH=D:\mysetup\images
URL=image.test.com
spring父容器配置文件加载applications.xml中读取该配置文件,配置如下
<!-- 使用spring自带的占位符替换功能 --><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!-- 允许JVM参数覆盖 --><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><!-- 忽略没有找到的资源文件 --><property name="ignoreResourceNotFound" value="true" /><!-- 配置资源文件 --><property name="locations"><list><value>classpath:env.properties</value></list></property></bean>

1.1.1.  @value注解

1.     @value是在所有bean创建成功后再执行

2.     @value注解只会在当前容器中查询数据


0 0
原创粉丝点击