@ImportResource
1.Springboot自动装配Spring等配置文件,默认会被Springboot自动给配置好;
2.如果需要自己编写Spring等配置文件,springboot默认是不能识别的;
3.如果需要springboot识别自己编写的配置文件,需要在
在主配置类中加入@ImportResource(locations={"classpath:Xxx.xml"}),但是不推荐手写spring配置文件,
因为Springboot推荐使用注解的方式进行配置;
4.如何配置?(主程序类和其他业务逻辑类不在一个包下,SpringBoot没有扫描到就会报错)
a.编写类;
b.添加@Configuration @Bean
@Configuration//配置类(等价于spring*.xml)
public class AppConfig {
@Bean
public Person person(){//<bean id="xxxxxx"></bean>
Person p=new Person();
Pet pet=new Pet();//<property name="pet" ref="pet">
p.setPet(pet);
return p;// <bean class="xxxxxx"></bean>
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
ApplicationContext context;
@Test
public void contextLoads() {
Person p=(Person)context.getBean("person");
System.out.print(p);
}
}
Springboot中全局配置文件中的占位符表达式
1.随机数
${random.uuid}:uuid
${random.value}:随机字符串;
${random.int}:随机整型数
${random.long}:随机长整型数
${random.int(10)}:10以内的整型数;
${random.int[1024,65536]}:指定随机数的范围
2.引用变量值;
yml中:
student:
name:x;
uname:${student.uname:默认值}
实际引用的是properties文件中的student.uname的值,
如果该值不存在,使用默认值,如果没有给默认值则原封不动的输出${student.uname}