【Spring Boot】10、Spring Boot其他配置文件

其他配置文件包括

property属性文件、xml配置文件

1、@PropertySource

@PropertySource:加载指定的配置文件;

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
   	// lastName必须是邮箱格式
   	// @Email
    // @Value("${person.last-name}")
    private String lastName;
    // @Value("##{11*2}")
    private Integer age;
    // @Value("true")
    private Boolean boss;

请注意 [字串,bool,时间,列表可以注入],map不能注入,

简单的数据类型Spring注入数据后进行了类型转换

断点查看可以知道注入的数据

person.lastName=hello@accp.site
person.age=18
person.boss=false
person.birth=2017/12/12
person.lists=lisi,zhaoliu

People(lastName=hello@accp.site, age=18, pets=null, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, lists=[[lisi, zhaoliu]])

IDEA设置 *.properties文件的默认编码

Settings` –> `Editor` –> `File Encoding` –> `Properties Files(*.properties)` –> `Default encoding for properties files:` `UTF-8` –> `OK

2、@ImportResource

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

想让Spring的配置文件生效,@ImportResource标注在一个配置类上

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

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}
  • Spring的配置文件
<?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="p123" class="site.accp.demo7.pojo.People">
        <property name="lastName" value="123"/>
    </bean>
</beans>
  • 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo7ApplicationTests {

    @Autowired
    ApplicationContext ioc;

    @Test
    public void contextLoads() {
        boolean p123 = ioc.containsBean("p123");
        System.out.println(p123);
    }

}

3、@Bean

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

1、配置类**@Configuration** --> Spring配置文件

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

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

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

测试:

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

输出:配置类@Bean给容器中添加组件了… 同时返回true

4、配置文件占位符

  • 随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
  • 占位符获取之前配置的值

如果没有可以使用的:指定默认值

person.last-name=张三${random.uuid}
person.age=${random.int}
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.hello:hello}_dog
person.dog.age=15
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值