学习笔记(07):Spring Boot实战开发-@ImportResource、配置类、占位符表达式

立即学习:https://edu.csdn.net/course/play/25330/298883?utm_source=blogtoedu

1. @ImportResource

    Spring Boot自动装配/自动配置

    Spring等配置文件  默认会被Spring Boot自动给配置好

    如果要自己编写Spring等配置文件,SPring Boot能否识别?手写一个spring.xml试验一下。结果:默认不识别,如需识别需要使用该注解指定;用法:@ImportResource(locations={"class:spring.xml"});放在启动类前使用;但不推荐手写spring配置文件。通过注解配置,这也是SpringBoot的核心思想;大致的配置方向:

    写配置类:

建一个config包:@Configuration    @Bean

示例:

     //配置类(等价于spring.xml)

@Configuration

public class AppConfig{//<bean if="xxxxxxx">

    @Bean

    public StudentService stuService(){

        StudentService stuService = new StudentService();

        StudentDao stuDao = new StudentDao();

        stuService.setStudentDao(stuDao);

 

        return stuService;//返回值<bean cleass="xxxxxx">

    }

}

2. SpringBoot全局配置文件中的  占位符表达式

a.随机数  ${random.uuid/lon/int}等

b.引用变量值

    yml中:

        student: 

                        uname: x

                        name: ${student.user.name}

         实际引用的是properties中的student.user.name的值

        student: 

                        uname: x

                        name: ${student.user.name2:defaultvalue}

        如果roperties中的student.user.name2不存在则使用默认值

c.配置与注解的体现

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${jdbc.url}"/>
</bean>
等价于
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
    @Value("${jdbc.url}")
    private String url;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值