@ConfigurationProperties(pref="")加载局部配置文件
刚开始@ConfigurationProperties(文件名)直接在参数里加文件名,其实是配置前缀pref="前缀"。加载局部配置文件是@PropertySource(value = {"classpath:abc.properties"})文件是properties类型。如果是yml类型,无法赋值显示null。
@Component
@PropertySource(value = {"classpath:abc.properties"})
@ConfigurationProperties(prefix = "emp")
public class Emp {
String name ;
加载xml配置文件 实例化bean 写成stuxml.xml没有写classpath报错
@ImportResource(locations = {"classpath:stuxml.xml"})
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = {"classpath:stuxml.xml"})
@SpringBootApplication
public class Demo3Application {
public static void main(String[] args) {
SpringApplication.run(Demo3Application.class, args);
}
}
bean文件
<?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">
<!--
加载一个xml,这个作为总的xml入口
<import resource="beans2.xml"/>
-->
<bean id="stu" class="com.example.demo.Stu">
<property name="name" value="xiaochun" />
</bean>
</beans>
实体类
package com.example.demo;
public class Stu {
String name ;
@Override
public String toString() {
return "Stu [name=" + name + "]";
}
浙公网安备 33010602011771号