-
其他配置注解
- @ConditionalOnBean(name = “tom”) // 条件装配 ,存在tom容器时,配置类中所有容器才会生效。
- @ConditionalOnMissingBean(name = “tom”) // 和上个注解相反
- @ImportResource(“classpath:bean.xml”) // 导入xml容器配置资源
package com.toec.boot.config; /** * @ProjectName: www-ES-Api * @Package: com.toec.boot.config * @ClassName: Config * @Author: 125827 * @Description: bean配置 * @Date: 1/9/2022 10:07 PM * @Version: 1.0 */ @Import({ DBHelper.class,User.class}) //可以导入全类名组件 @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类==配置文件 @ConditionalOnBean(name = "tom") //条件装配 ,存在tom容器时,配置类中所有容器都不会生效。 // @ConditionalOnMissingBean(name = "tom") //和上个注解相反 @ImportResource("classpath:bean.xml") //导入xml容器配置资源 public class MyConfig { @Bean public User user(){ return new User("toec",20); } @Bean("tom") public Pet pet(){ return new Pet("dog"); } }
2、配置绑定
- @EnableConfigurationProperties(Car.class)
// 1、开启car配置绑定功能
// 2、把这个car这个组件自动注册到容器中 - @ConfigurationProperties(prefix=“mycar”)
package com.toec.boot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Package: com.toec.boot.bean
* @ClassName: Car
* @Description: 汽车
* @Date: 1/10/2022 12:35 AM
* @Version: 1.0
* ConfigurationProperties只有在容器中的组件,才会拥有SpringBoot提供的强大功能
*/
// @Component //加入到容器中
@ConfigurationProperties(prefix="mycar")
public class Car {
private String brand;
private Integer price;
public Car() {
}
public Car(String brand, Integer price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
package com.toec.boot.config;
import ch.qos.logback.core.db.DBHelper;
import com.toec.boot.bean.Car;
import com.toec.boot.bean.Pet;
import com.toec.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
/**
* @Package: com.toec.boot.config
* @ClassName: Config
* @Description: bean配置
* @Date: 1/9/2022 10:07 PM
* @Version: 1.0
*/
@Import({
DBHelper.class,User.class}) //可以导入全类名组件
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类==配置文件
//@ConditionalOnBean(name = "tom") //条件装配
@ConditionalOnMissingBean(name = "tom") //和上个注解相反
//@ImportResource("classpath:bean.xml")
@EnableConfigurationProperties(Car.class)
//1、开启car配置绑定功能
//2、把这个car这个组件自动注册到容器中
public class MyConfig {
@Bean
public User user(){
return new User("toec",20);
}
@Bean("tom")
public Pet pet(){
return new Pet("dog");
}
}
mycar.brand=BYD
mycar.price=10000