一、@Configuration
基本使用
Full模式與Lite模式
? 示例
? 最佳實戰
? 配置 類組件之間無依賴關系用Lite模式加速容器啟動過程,減少判斷
? 配置 類組件之間有依賴關系,方法會被調用得到之前單實例組件,用Full模式例子
@Configuration(proxyBeanMethods
= false)
public class MyConfig {@Bean public User
user01(){User zhangsan
= new User("zhangsan", 18);zhangsan
.setPet(tomcatPet());return zhangsan
;}@Bean("tom")public Pet
tomcatPet(){return new Pet("tomcat");}
}
################################
@Configuration測試代碼如下########################################
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
public class MainApplication {public static void main(String
[] args
) {ConfigurableApplicationContext run
= SpringApplication
.run(MainApplication
.class, args
);String
[] names
= run
.getBeanDefinitionNames();for (String name
: names
) {System
.out
.println(name
);}Pet tom01
= run
.getBean("tom", Pet
.class);Pet tom02
= run
.getBean("tom", Pet
.class);System
.out
.println("組件:"+(tom01
== tom02
));MyConfig bean
= run
.getBean(MyConfig
.class);System
.out
.println(bean
);User user
= bean
.user01();User user1
= bean
.user01();System
.out
.println(user
== user1
);User user01
= run
.getBean("user01", User
.class);Pet tom
= run
.getBean("tom", Pet
.class);System
.out
.println("用戶的寵物:"+(user01
.getPet() == tom
));}
}
二、@Import
底層是個class數組
例子
* 4、
@Import({User
.class, DBHelper
.class})* 給容器中自動創建出這兩個類型的組件、默認組件的名字就是全類名
****/@Import({User
.class, DBHelper
.class})
@Configuration(proxyBeanMethods
= false)
public class MyConfig {
}
三、@Conditional
條件裝配:滿足Conditional指定的條件,則進行組件注入
例子:
=====================測試條件裝配
==========================
@Configuration(proxyBeanMethods
= false)
@ConditionalOnMissingBean(name
= "tom")
public class MyConfig {@Bean public User
user01(){User zhangsan
= new User("zhangsan", 18);zhangsan
.setPet(tomcatPet());return zhangsan
;}@Bean("tom22")public Pet
tomcatPet(){return new Pet("tomcat");}
}public static void main(String
[] args
) {ConfigurableApplicationContext run
= SpringApplication
.run(MainApplication
.class, args
);String
[] names
= run
.getBeanDefinitionNames();for (String name
: names
) {System
.out
.println(name
);}boolean tom
= run
.containsBean("tom");System
.out
.println("容器中Tom組件:"+tom
);boolean user01
= run
.containsBean("user01");System
.out
.println("容器中user01組件:"+user01
);boolean tom22
= run
.containsBean("tom22");System
.out
.println("容器中tom22組件:"+tom22
);}
四、@ImportResoure(原生配置文件引入)
例子
======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><bean id="haha" class="com.atguigu.boot.bean.User"><property name="name" value="zhangsan"></property><property name="age" value="18"></property></bean><bean id="hehe" class="com.atguigu.boot.bean.Pet"><property name="name" value="tomcat"></property></bean>
</beans>
@ImportResource("classpath:beans.xml")
public class MyConfig {}======================測試
=================boolean haha
= run
.containsBean("haha");boolean hehe
= run
.containsBean("hehe");System
.out
.println("haha:"+haha
);System
.out
.println("hehe:"+hehe
);
總結
以上是生活随笔為你收集整理的SpringBoot @Configuration •@Import •@Conditional•@ImportResoure基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。