Spring 注入 Bean 的七種方式

來(lái)源:juejin.cn/post/6844903813753602056
通過(guò)注解注入Bean
背景
<bean id="bean" class="beandemo.Bean" />
<context:component-scan base-package="com.company.beandemo"/>
通過(guò)注解注入的一般形式
public class MyBean{
}
//創(chuàng)建一個(gè)class配置文件
@Configuration
public class MyConfiguration{
//將一個(gè)Bean交由Spring進(jìn)行管理
@Bean
public MyBean myBean(){
return new MyBean();
}
}
ClassPathXmlApplicationContext,而是獲取的AnnotationConfigApplicationContext實(shí)例。ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
MyBean myBean = cotext.getBean("myBean",MyBean.class);
System.out.println("myBean = " + myBean);
@Configuration注解去標(biāo)記了該類,這樣標(biāo)明該類是一個(gè)Spring的一個(gè)配置類,在加載配置的時(shí)候會(huì)去加載他。@Bean的注解,標(biāo)明這是一個(gè)注入Bean的方法,會(huì)將下面的返回的Bean注入IOC。通過(guò)構(gòu)造方法注入Bean
@Component
public class MyBeanConstructor {
private AnotherBean anotherBeanConstructor;
@Autowired
public MyBeanConstructor(AnotherBean anotherBeanConstructor){
this.anotherBeanConstructor = anotherBeanConstructor;
}
@Override
public String toString() {
return "MyBean{" +
"anotherBeanConstructor=" + anotherBeanConstructor +
'}';
}
}
@Component(value="Bean的id,默認(rèn)為類名小駝峰")
public class AnotherBean {
}
@Configuration
@ComponentScan("com.company.annotationbean")
public class MyConfiguration{
}
@Qualifier注解@Conmonent,那么我們就可以在加載Bean的時(shí)候把他像零件一樣裝配:wrench:到這個(gè)IOC汽車上了@Component:@Controller 標(biāo)注在Controller層 @Service 標(biāo)注在Service層 @Repository 標(biāo)注在dao層
@Component的類進(jìn)行注入。通過(guò)set方法注入Bean
@Component
public class MyBeanSet {
private AnotherBean anotherBeanSet;
@Autowired
public void setAnotherBeanSet(AnotherBean anotherBeanSet) {
this.anotherBeanSet = anotherBeanSet;
}
@Override
public String toString() {
return "MyBeanSet{" +
"anotherBeanSet=" + anotherBeanSet +
'}';
}
}
@AutoWired,與上面不同的是,我們不會(huì)在實(shí)例化該類時(shí)就自動(dòng)裝配:wrench:這個(gè)對(duì)象,而是在顯式調(diào)用setter的時(shí)候去裝配。通過(guò)屬性去注入Bean
@Component
public class MyBeanProperty {
@Autowired
private AnotherBean anotherBeanProperty;
@Override
public String toString() {
return "MyBeanProperty{" +
"anotherBeanProperty=" + anotherBeanProperty +
'}';
}
}
對(duì)于有些小伙伴問(wèn)私有屬性,Spring怎么去加載它到IOC的?推薦去看看反射
通過(guò)List注入Bean
@Component
public class MyBeanList {
private List<String> stringList;
@Autowired
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
public List<String> getStringList() {
return stringList;
}
}
@Configuration
@ComponentScan("annoBean.annotationbean")
public class MyConfiguration {
@Bean
public List<String> stringList(){
List<String> stringList = new ArrayList<String>();
stringList.add("List-1");
stringList.add("List-2");
return stringList;
}
}
@Bean
//通過(guò)該注解設(shè)定Bean注入的優(yōu)先級(jí),不一定連續(xù)數(shù)字
@Order(34)
public String string1(){
return "String-1";
}
@Bean
@Order(14)
public String string2(){
return "String-2";
}
第二種方式的優(yōu)先級(jí)高于第一種,當(dāng)兩個(gè)都存在的時(shí)候,若要強(qiáng)制去使用第一種方式,則要去指定Bean的id即可
通過(guò)Map去注入Bean
@Component
public class MyBeanMap {
private Map<String,Integer> integerMap;
public Map<String, Integer> getIntegerMap() {
return integerMap;
}
@Autowired
public void setIntegerMap(Map<String, Integer> integerMap) {
this.integerMap = integerMap;
}
}
@Bean
public Map<String,Integer> integerMap(){
Map<String,Integer> integerMap = new HashMap<String, Integer>();
integerMap.put("map-1",1);
integerMap.put("map-2",2);
return integerMap;
}
@Bean
public Integer integer1(){
return 1;
}
@Bean
public Integer integer2(){
return 2;
}
逆鋒起筆是一個(gè)專注于程序員圈子的技術(shù)平臺(tái),你可以收獲最新技術(shù)動(dòng)態(tài)、最新內(nèi)測(cè)資格、BAT等大廠大佬的經(jīng)驗(yàn)、增長(zhǎng)自身、學(xué)習(xí)資料、職業(yè)路線、賺錢思維,微信搜索逆鋒起筆關(guān)注!
往期回顧
評(píng)論
圖片
表情
