day5-@ConfigurationProperties@Autowired...

1.1

@ImportResource("beans.xml")

也可以@ImportResource("classpath:beans.xml")代替@bean,但过时了,不常用

bean.xml文件

    <bean id="haha" class="com.pianxian1.bean.User">
        <property name="name" value="Tom"></property>
        <property name="age" value="20"></property>
    </bean>

    <bean id="hehe" class="com.pianxian1.bean.Pet">
        <property name="name" value="diaomao"></property>
    </bean>

 扫包就存在了

1.2 .properties文件联动项目包里的普通类

(Ps:resources里面的.properties后缀是固定,改了就变类型)

俩注解要一起,第一个把它加到容器,才能用第二个调用配置文件设置功能 

@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;
    public  String getBrand(){
        return  brand;
    }
    public Integer getPrice(){
        return  price;
    }
    public void setBrand(String brand){
        this.brand=brand;
    }
    public void setPrice(Integer price){
        this.price=price;
    }
    public Car(){
    }
    public Car(String brand,Integer price) {
        this.price = price;
        this.brand=brand;
    }

}

1.3配置文件Application.properties属性,但是给组件默认

最后controller检验

@RestController
public class Controller {
    @Autowired
    Car car;
    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }

    @RequestMapping("/car")

    public Car car(){
        return car;
    }
}

@Autowired自动装入容器有的car

配置

@Component
@ConfigurationProperties(prefix = "mycar")

加在Car的类文件开头,properties文件相对应属性直接加

mycar.price=10000
mycar.brand="lll"

就会给所有Car一个默认属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值