SpringBoot 入门之二:获取Properties中的值,通过类配置来替代原SpringXML的配值和注入方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xdy3008/article/details/84433970

•application.properties

•application.yml

person.last-name=\u674E\u56DB
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

1 通过@ConfigurationPropertie注入 :该注入适用于全局application.properties , @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定

@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    // Set Get methods 
}

2 通过@Value注入 ,该注入适用于全局application.properties

@Component
//@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
	
    @Value("${person.lastName}")
    private String lastName;
    
    @Value("${person.age}")
    private Integer age;
    
    @Value("true}")
    private Boolean boss;
    
    @Value("${person.birth}")
    private Date birth;
    
    @Value("${person.maps}")
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    // Set Get methods 
}

3 通过 @PropertySource(value = {“classpath:person.properties”}) 该注入适用于非局部自定义属性值的配置

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}

如果属性的key value配置是在spring XML配置文件中,则可以用以下方法获取

Bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>

4 通过@ImportResource(locations = {“classpath:beans.xml”})

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效; Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别; 想让Spring的配置文件生效,必须加载进来;@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBoot02ConfigApplication.class, args);
	}
}

import com.atguigu.springboot.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {

	@Autowired
	Person person;

	@Autowired
	ApplicationContext ioc;

	@Test
	public void testHelloService(){
		Object hellobean = ioc.getBean("helloService02");
		boolean b = ioc.containsBean("helloService02");
		System.out.println(hellobean);
	}
} 

5 通过注解的方式类@Configuration 获取spring配置的内容

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration 替代 Spring配置文件

2、使用@Bean给容器中添加组件

package com.atguigu.springboot.config;


import com.atguigu.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>标签添加组件
 *
 */
@Configuration
public class MyAppConfig {

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloServiceHere(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }
}

注意:要获取的bean的名字,必须跟@Configuration文件中标注为@Bean所获取的对象的方法名一致,即 ”helloServiceHere“

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {

	@Autowired
	Person person;

	@Autowired
	ApplicationContext ioc;

	@Test
	public void testHelloService(){
		Object hellobean = ioc.getBean("helloServiceHere");
		System.out.println(hellobean);
	}
}

猜你喜欢

转载自blog.csdn.net/xdy3008/article/details/84433970