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

二、Spring配置文件读取

时间:2017-09-07 13:35:18      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:width   version   bean   getname   group   为我   port   ack   path   

 

  在开发是时候我们使用Spring,主要是他为我们的开发提供很多便捷的方式,控制反转,依赖注入和面向切面编程是Spring的三大核心思想,配置文件读取也可以说是一种依赖注入,只不过这个是从配置文件中依赖注入进来的,那如何见配置文件中数据注入到bean 中呢?
(java学习交流③群:256909960,欢迎加入)
一、通过@Vaule来配置
  这种配置的是通过Spring将配置文件加载后,在属性上使用@Value注解将对应的值注入进来
 
1、pom文件
  需要的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>

 

(java学习交流③群:256909960,欢迎加入)
2、配置类
  在这里就直接用Spring注解进行配置了,xml配置方式后续在写
@Component    //1
@PropertySource("classpath:aaa.properties")//2
public class ValueProperties {
    @Value("${name}")//3
    private String name;
    @Value("${age}")//4
    private Integer age;
    public void hello(){
        System.out.println("my name : "+name+"| age: "+age);
    }
}

1、@component 让Spring能够扫描到这个包

2、@PropertiesSource 读取加载classpath下的配置文件(只能是properties)

3、注入配置文件中的数据name 和age,

例如配置文件中的数据是这样的host.ip,那么注入时应该这样写${host.ip}

(java学习交流③群:256909960,欢迎加入)
3、调用测试
  
@ComponentScan//1
public class AppConfiguration {
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
        applicationContext.getBean(ValueProperties.class).hello();
    }

1、扫描包,这个类要在上面ValueProperties类的同级或更上层的包中

(java学习交流③群:256909960,欢迎加入)
二、通过@ConfigurationProperties来配置
  这个是Springboot中的注解
 
1、pom文件
  
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

 

(java学习交流③群:256909960,欢迎加入)
2、配置类
  
@Component//1
@ConfigurationProperties(prefix = "server.irich")//2 
//这里不可以用@PropertySource加载配置文件
public class BootConfiguration { private String name; private Integer age; public void hello(){ System.out.println("my name : "+name+"| age: "+age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; }

配置文件

技术分享
server.irich.name=张国荣
server.irich.name=45
配置文件

 

1、向容器中加入这个bean

2、加载配置文件中,prefix是只加载文件中指定前缀的数据

一定要有setter方法

注:1.5以后@ConfigurationProperties没有了locations这个功能,但他可以和@PropertySource、@Import组合使用

(java学习交流③群:256909960,欢迎加入)
3、调用测试
  
@ComponentScan //1
@EnableConfigurationProperties //2
@PropertySource("classpath:aaa.properties") //3
//@SpringBootApplication
public class BootConfigurationApp {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BootConfigurationApp.class);
        applicationContext.getBean(BootConfiguration.class).hello();
// SpringApplication.run(BootConfigurationApp.class,args);
    }
}

1、配置扫描包

2、当@EnableConfigurationProperties注解应用到你的@Configuration时,任何被@ConfigurationProperties注解的beans将自动被Environment属性配置

3、在@ConfigurationProperties为属性赋值前加载配置文件,不可以将需要加在的配置文件写在和@ConfigurationProperties相同的位置

(java学习交流③群:256909960,欢迎加入)

二、Spring配置文件读取

标签:width   version   bean   getname   group   为我   port   ack   path   

原文地址:http://www.cnblogs.com/irich/p/7488998.html

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