@ImportResource,占位符表达式

@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}
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值