第五章:SpringBoot配置(@PropertySource @ImportResource @Bean 注解)

总目录:SpringBoot学习教程

 

(一):@PropertySource的意思是加载指定的配置文件

上一节中@ConfigurationProperties这个注解是去默认加载全局配置文件allication.properties中的属性。

但是,我们如果想加载其他配置文件中的属性呢?就需要在类中声明这个注解,并且指定他的具体位置和文件的名字.

我们创建一个名字叫做:person.properties的配置文件 。

(并把全局配置文件中的这些person的值都给注释掉,不然他回去全局文件中加载)

#person.properties
person.last-name=张三
person.age=21
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=小狗
person.dog.age=15

在类中加载这个文件

package com.example.springboot02.bean;

//加载指定的配置文件  并把值绑定到对应字段上
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person") 
public class Person2 {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map maps;
    private List lists;
    private Dog dog;
//自行补全get set toString方法  
}
 
  

我们去test中测试:

package com.example.springboot02;

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

	@Autowired
	Person person;
	@Autowired
	Person2 person2;
	@Test
	public void contextLoads() {
		//System.out.println(person);
		System.out.println(person2);
	}

}

结果就是我们配置文件中定义的值。测试成功

 

(二)@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;

以前的做法:

我们自己定义一个类  假如叫做HelloService.java

然后定义一个xml文件  就叫beans.xml




 
 

我们去test中测试ioc中有没有这个容器

package com.example.springboot02;

import com.example.springboot02.bean.Person;
import com.example.springboot02.bean.Person2;
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 Springboot02ApplicationTests {
	
	@Autowired
	ApplicationContext ioc;

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

结果显示不存在。

那么怎么办呢?--->我们把@ImportResource标注在一个配置类上

/*
入口类
*/
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class Springboot02Application {

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

这是我们再测试一下,就说ioc容器中存在。

 

以上就是以前的做法,但是SpringBoot推荐的做法是什么呢?

-----------------------------------------------------------------------------------------------------------------------------------

SpringBoot推荐给容器中添加组件的方式;

推荐使用全注解的方式

1.创建一个config包。

2.创建一个配置类MyAppConfig.java

添加一个注解

 @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件

 

package com.example.springboot02.config;
import com.example.springboot02.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAppConfig {

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

}

这时候应该是我们已经把

HelloService注册到ioc中了  而且他的id是helloService02

 

我们去测试:

test:

第五章:SpringBoot配置(@PropertySource @ImportResource @Bean 注解)_第1张图片

结果:

第五章:SpringBoot配置(@PropertySource @ImportResource @Bean 注解)_第2张图片  存在!!!ok

 

 

下一节:第六章:SpringBoot配置(配置文件占位符)

 

 

 

你可能感兴趣的:(IDEA,Maven,Springboot)