码迷,mamicode.com
首页 > 编程语言 > 详细

spring 使用外部属性文件

时间:2019-01-16 13:13:36      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:file   ons   enc   prope   resource   div   ica   --   默认值   

PropertyPlaceholderConfigurer

spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件。
PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessorBean接口,因而也是一个Bean工厂后处理器。


PropertyPlaceholderConfigurer的使用

xml配置及注解方式:

技术分享图片
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
pom.xml
技术分享图片
name=zhangsan
age=23
my.properties
技术分享图片
package test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Driver {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("my.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }
}
class User {
    @Value("${name}")
    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;
    }
}
java 类
技术分享图片
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- spring 引入属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:my.properties"
        p:fileEncoding="utf-8"/>
    <!-- 开启包扫描,因为User类使用到了@Value注解;包扫描不支持裸体类上的注解 -->
    <context:component-scan base-package="test"/>
    <!-- 使用外部属性文件值赋值 -->
    <bean id="user" class="test.User" p:age="${age}"/>
</beans>
spring my.xml

PropertyPlaceholderConfigurer的其他属性:
locations:可以引入多个属性文件
fileEncoding:属性文件的编码格式
order:指定多个属性的优先级
placeholderPrefix:默认值为“${”,可以修改
placeholderSuffix:默认为“}”

除了使用<bean>声明PropertyPlaceholderConfigurer引入属性文件这种方式外,还可以使用另一种简洁的方式;但如果希望自定义一些额外的高级功能,如属性加密、使用数据库表来保存配置信息时,就必须使用<bean>声明PropertyPlaceholderConfigurer的方式

技术分享图片
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- spring 引入属性文件 -->
    <context:property-placeholder location="my.properties"/>
    <!-- 开启包扫描,因为User类使用到了@Value注解;包扫描不支持裸体类上的注解 -->
    <context:component-scan base-package="test"/>
    <!-- 使用外部属性文件值赋值 -->
    <bean id="user" class="test.User" p:age="${age}"/>
</beans>
spring my.xml

Java配置及注解方式:

技术分享图片
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.16.RELEASE</version>
      </dependency>
pom.xml
技术分享图片
name=zhangsan
age=23
my.properties
技术分享图片
package test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Driver {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }
}
class User {
    @Value("${name}")
    private String name;
    @Value("${age}")
    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;
    }
}
java类
技术分享图片
package test;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@ComponentScan
public class MyConfig {
    @Bean
    public User user() {
        return new User();
    }
    @Bean
    public PropertyPlaceholderConfigurer PropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setLocation(new ClassPathResource("my.properties"));
        return configurer;
    }

}
java 配置类

 

spring 使用外部属性文件

标签:file   ons   enc   prope   resource   div   ica   --   默认值   

原文地址:https://www.cnblogs.com/Mike_Chang/p/10276303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!