底层注解@Configuration
springboot底层注解
@Configuration
@Configuration //告诉springboot这是一个配置类 默认是单实例 配置类也是组件
public class myConfig {
//@Bean 给容器中添加组件 以方法名作为组件的id 返回值类型就是组件的类型 返回的值就是组件的实例
//默认@Bean是以方法名作为组件id 如需添加自定义组件名称 就需要给@bean添加名称 例:@bean("Cat") 其中Cat就是组件的名字
@Bean("Cat")
public Pet getPet(){
return new Pet("cat","eight");
}
@Bean("Xm")
public User getUser(){
return new User("小明");
}
}
//Pet类
public class Pet {
private String name;
private String age;
public Pet() {
}
public Pet(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
//User类
public class User {
private String name;
public User() {
}
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
//检测组件是否成功注入
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
boolean containsCat = applicationContext.containsBean("Cat");
System.out.println("是否包含Cat:"+containsCat);
boolean containsXm = applicationContext.containsBean("xm");
System.out.println("是否包含User:"+containsXm);
}
}
/**运行结果
是否包含Cat:true
是否包含User:true
*/
@import
@Import({User.class,String.class})//给组件中自动添加{}中类型的组件 {}为数组类型
@conditional
条件装配:满足conditional指定的条件 则进行组件注入
例子:
@Configuration()
public class myConfig {
@Bean("Cat")
public Pet getPet(){
return new Pet("cat","eight");
}
@ConditionalOnBean(name = "Cat") //如果容器中有Cat组件 将注入此组件 否则将不创建 此时容器中有Cat组件将会被注入
@Bean("xm")
public User getUser(){
return new User("小明");
}
}
//测试
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
boolean containsCat = applicationContext.containsBean("Cat");
System.out.println("是否包含Cat:"+containsCat);
boolean containsXm = applicationContext.containsBean("xm");
System.out.println("是否包含User:"+containsXm);
}
}
/**运行结果
是否包含Cat:true
是否包含User:true
*/
//下例为不满足条件
@Configuration()
public class myConfig {
//@Bean("Cat") 将bean注释 此方法变为普通方法 容器中将不存在Cat这个组件
public Pet getPet(){
return new Pet("cat","eight");
}
@ConditionalOnBean(name = "Cat") //如果容器中有Cat组件 将注入此组件 否则将不创建 此时容器没有Cat组件将不会被注入
@Bean("xm")
public User getUser(){
return new User("小明");
}
}
//测试
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
boolean containsCat = applicationContext.containsBean("Cat");
System.out.println("是否包含Cat:"+containsCat);
boolean containsXm = applicationContext.containsBean("xm");
System.out.println("是否包含User:"+containsXm);
}
}
/**运行结果
是否包含Cat:false
是否包含User:false
*/
@ImportResource
@ImportResource("classpath:beans.xml") //将传统的xml配置导入到配置类中 并自动注入
@ConfigurationProperties---配置绑定
第一种方式:@component+@ConfigurationProperties
//Pet类
@Component
@ConfigurationProperties(prefix = "mypet") //注:名称不能使用驼峰命名法
public class Pet {
private String name;
private String age;
public Pet() {
}
public Pet(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
//配置文件
mypet.name = dog
mypet.age = 1
//测试
@RestController
public class Controller1 {
@Autowired
Pet pet;
@RequestMapping("/pet")
Pet hasPet(){
return pet;
}
}
结果:
第二种方式:@EnableConfigurationProperties+@ConfigurationProperties
//配置类 @EnableConfigurationProperties()需要在配置类中使用
@Configuration()
@EnableConfigurationProperties(Pet.class)//为Pet类开启配置绑定 并自动注入到容器中
public class myConfig {
}
//配置文件 properties
mypet.name = dog
mypet.age = 1
//测试
@RestController
public class Controller1 {
@Autowired
Pet pet;
@RequestMapping("/pet")
Pet hasPet(){
return pet;
}
}
结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?