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一个默认属性