日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Spring注入bean的常用的六種方式

作者:程序猿——小白菜 更新時間: 2022-05-20 編程語言

一.通過注解注入的一般形式

Bean類

public class TestBean{
}

Configuration類
@Configuration注解去標(biāo)記了該類,這樣標(biāo)明該類是一個Spring的一個配置類,在加載配置的時候會去加載他。

@Bean的注解,標(biāo)明這是一個注入Bean的方法,會將下面的返回的Bean注入IOC。

//創(chuàng)建一個class配置文件
@Configuration
public class TestConfiguration{
 //將一個Bean交由Spring進(jìn)行管理
    @Bean
    public TestBean myBean(){
        return new TestBean();
    }
}

測試類

ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean = cotext.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);

二.通過構(gòu)造方法注入Bean

我們在生成一個Bean實(shí)例的時候,可以使用Bean的構(gòu)造方法將Bean實(shí)現(xiàn)注入
Bean類

@Component
public class TestBeanConstructor {

 private AnotherBean anotherBeanConstructor;

 @Autowired
 public TeanBeanConstructor(AnotherBean anotherBeanConstructor){
     this.anotherBeanConstructor = anotherBeanConstructor;
 }

 @Override
 public String toString() {
     return "TeanBean{" +
         "anotherBeanConstructor=" + anotherBeanConstructor +
         '}';
 }
}

AnotherBean類

@Component(value="Bean的id,默認(rèn)為類名小駝峰")
public class AnotherBean {
}

Configuration類

@Configuration
@ComponentScan("com.company.annotationbean")
public class TestConfiguration{
}

注解解釋

@AutoWired
自動裝配??
若是在這里注入的時候指定一個Bean的id就要使用@Qualifier注解。
注釋可以在 setter 方法中被用于自動連接 bean,就像 @Autowired 注釋,容器,一個屬性或者任意命名的可能帶有多個參數(shù)的方法。
還有不解的地方可以直接百度注解有更詳細(xì)的解釋跟實(shí)例

@Component(默認(rèn)單例模式)
@Component 被稱為元注釋,它是@Repository、@Service、@Controller、@Configuration的父類,理論上可以使用@Component來注釋任何需要Spring自動裝配的類。但每個注釋都有他們自己的作用,用來區(qū)分類的作用,Spring文檔上也說明后續(xù)版本中可能為@Repository、@Service、@Controller、@Configuration這些注釋添加其他功能,所以建議大家還是少使用@Component。
  @Repository注解是任何滿足存儲庫角色或構(gòu)造型(也稱為數(shù)據(jù)訪問對象或DAO)的類的標(biāo)記。該標(biāo)記的用途之一是異常的自動翻譯,如異常翻譯中所述。
  @Service注解一般使用在Service層。
  @Controller注解一般使用在Controller層的類,@RestController繼承了@Controller。
  @Configuration注解一般用來配置類,用來項(xiàng)目啟動時加載的類。
  
@ComponentScan("")
@ComponentScan注解一般用在Spring項(xiàng)目的核心配置類,或者在Spring Boot項(xiàng)目的啟動類里使用。作用就是用來掃描@Component及其子類注釋的類,用于Spring容器自動裝配。@ComponentScan默認(rèn)是掃描的路徑是同級路徑及同級路徑的子目錄,所以把Spring Boot的啟動類放在根目錄下,@ComponentScan是可以省略不寫的。
主要屬性:

  1. value屬性、basePackages屬性
@AliasFor("basePackages")
String[] value() default {};

@AliasFor("value")
String[] basePackages() default {};

這兩個值的作用是一樣的,是相互別名的關(guān)系。內(nèi)容是填寫要掃描的路徑,如果是有一個路徑,可以不用寫value,有幾種寫法如下:

@ComponentScan("com.zh.service")

@ComponentScan(value = "com.zh.service")

@ComponentScan(value = {"com.zh.dao", "com.zh.service"})
  1. includeFilters屬性、excludeFilters屬性
    這兩個的作用都是過濾器,excludeFilters的作用是剔除values屬性內(nèi)的個別不需要加載的類,而includeFilters一般是和excludeFilters配合使用,就是被excludeFilters剔除的類里面,還需要其中的幾個類,就用includeFilters再加上。

舉個例子,假設(shè)送了excludeFilters剔除了所有注解是Repository的類,但其中一個Stub開頭的類,還要用到,就可以按下面的例子這樣寫。

ComponentScan.Filter[] includeFilters() default {};

    ComponentScan.Filter[] excludeFilters() default {};

Filter作為過濾器的基本對象

@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Filter {
    FilterType type() default FilterType.ANNOTATION;

    @AliasFor("classes")
    Class<?>[] value() default {};

    @AliasFor("value")
    Class<?>[] classes() default {};

    String[] pattern() default {};
}

FilterType是過濾器的類型,定義方式有幾種

public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;

    private FilterType() {
    }
}

三.通過set方法注入Bean

我們可以在一個屬性的set方法中去將Bean實(shí)現(xiàn)注入
Bean類

@Component
public class TestBeanSet {

 private AnotherBean anotherBeanSet;

 @Autowired
 public void setAnotherBeanSet(AnotherBean anotherBeanSet) {
     this.anotherBeanSet = anotherBeanSet;
 }

 @Override
 public String toString() {
     return "TestBeanSet{" +
         "anotherBeanSet=" + anotherBeanSet +
         '}';
 }
}

Configuration類

@Configuration
@ComponentScan("com.company.annotationbean")
public class TestConfiguration{
}

Test類

ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean testBean = cotext.getBean("testBean",TestBean.class);
System.out.println("testBean = " + testBean);

四.通過屬性去注入Bean

@Component
public class TestBeanProperty {

 @Autowired
 private AnotherBean anotherBeanProperty;

 @Override
 public String toString() {
     return "TestBeanProperty{" +
         "anotherBeanProperty=" + anotherBeanProperty +
         '}';
 }
}

這里可以通過@AutoWired去自動裝配它。

五.通過List注入Bean

TestBeanList類

@Component
public class TestBeanList {

 private List<String> stringList;

 @Autowired
 public void setStringList(List<String> stringList) {
     this.stringList = stringList;
 }

 public List<String> getStringList() {
     return stringList;
 }
}

TestConfiguration類()配置類

@Configuration
@ComponentScan("annoBean.annotationbean")
public class TestConfiguration {

    @Bean
    public List<String> stringList(){
       List<String> stringList = new ArrayList<String>();
       stringList.add("List-1");
       stringList.add("List-2");
       return stringList;
    }
}

這里我們將TestBeanList進(jìn)行了注入,對List中的元素會逐一注入。

下面我們換一種注入List方式
TestConfiguration類

@Bean
//通過該注解設(shè)定Bean注入的優(yōu)先級,不一定連續(xù)數(shù)字
@Order(34)
public String string1(){
    return "String-1";
}

@Bean
@Order(14)
public String string2(){
    return "String-2";
}

注入與List中泛型一樣的類型,會自動去匹配類型,及時這里沒有任何List的感覺,只是String的類型,但他會去通過List的Bean的方式去注入。
注意:
第二種方式的優(yōu)先級高于第一種,當(dāng)兩個都存在的時候,若要強(qiáng)制去使用第一種方式,則要去指定Bean的id即可。

六.通過Map去注入Bean

@Component
public class TestBeanMap {

 private Map<String,Integer> integerMap;

 public Map<String, Integer> getIntegerMap() {
     return integerMap;
 }

 @Autowired
 public void setIntegerMap(Map<String, Integer> integerMap) {
     this.integerMap = integerMap;
 }
}
/**
*第一種注入map方式
*/
@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;
}
/**
*第二種注入map方式
*/
@Bean
public Integer integer1(){
    return 1;
}

@Bean
public Integer integer2(){
    return 2;
}

這里跟List一樣,第二種優(yōu)先級大于第一種

原文鏈接:https://blog.csdn.net/A_awen/article/details/123224794

欄目分類
最近更新