起源:juejin.cn/post/6844903813753602056

通过注解注入Bean

背景

咱们谈到Spring的时候肯定会提到IOC容器、DI依赖注入,Spring通过将一个个类标注为Bean的办法注入到IOC容器中,达到了管制反转的成果。那么咱们刚开始接触Bean的时候,肯定是应用xml文件,一个一个的注入,就例如上面这样。

 <bean id="bean" class="beandemo.Bean" />

咱们的我的项目个别很大的话,就须要成千上百个Bean去应用,这样写起来就很繁琐。那么Spring就帮咱们实现了一种通过注解来实现注入的办法。只须要在你须要注入的类后面加上相应的注解,Spring就会帮忙咱们扫描到他们去实现注入。

xml扫描包的形式

 <context:component-scan base-package="com.company.beandemo"/>

通过注解注入的个别模式

个别状况下,注入Bean有一个最直白,最易懂的形式去实现注入,上面废话先不多说,先贴代码。

Bean类

public class MyBean{}

Configuration类

//创立一个class配置文件@Configurationpublic class MyConfiguration{ //将一个Bean交由Spring进行治理    @Bean    public MyBean myBean(){        return new MyBean();    }}

Test类

与xml有一点不同,这里在Test中,实例化的不再是ClassPathXmlApplicationContext,而是获取的AnnotationConfigApplicationContext实例。

ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);MyBean myBean = cotext.getBean("myBean",MyBean.class);System.out.println("myBean = " + myBean);

下面的代码中MyBean也就是咱们须要Spring去治理的一个Bean,他只是一个简略的类。而MyConfiguration中,咱们首先用@Configuration注解去标记了该类,这样表明该类是一个Spring的一个配置类,在加载配置的时候会去加载他。

在MyConfiguration中咱们能够看到有一个办法返回的是一个MyBean的实例,并且该办法上标注着@Bean的注解,表明这是一个注入Bean的办法,会将上面的返回的Bean注入IOC。

通过构造方法注入Bean

咱们在生成一个Bean实例的时候,能够应用Bean的构造方法将Bean实现注入。间接看代码

Bean类

@Componentpublic class MyBeanConstructor { private AnotherBean anotherBeanConstructor; @Autowired public MyBeanConstructor(AnotherBean anotherBeanConstructor){     this.anotherBeanConstructor = anotherBeanConstructor; } @Override public String toString() {     return "MyBean{" +         "anotherBeanConstructor=" + anotherBeanConstructor +         '}'; }}

AnotherBean类

@Component(value="Bean的id,默认为类名小驼峰")public class AnotherBean {}

Configuration类

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

这里咱们能够发现,和个别形式注入的代码不一样了,咱们来看看新的注解都是什么意思:

@AutoWired

简略粗犷,间接翻译过去的意思就是主动拆卸,还不了解为什么叫主动拆卸?看了下一个注解的解释你就晓得了。若是在这里注入的时候指定一个Bean的id就要应用@Qualifier注解

@Component(默认单例模式)

什么??这翻译过去是整机,怎么感觉像是修汽车??是的,Spring治理Bean的办法就是修汽车的形式。咱们在须要将一个类变成一个Bean被Spring能够注入的时候加上注解整机@Conmonent,那么咱们就能够在加载Bean的时候把他像整机一样拆卸到这个IOC汽车上了

在这里咱们还有几个其余的注解也能够实现这个性能,也就是细化的@Component

  • @Controller 标注在Controller层
  • @Service 标注在Service层
  • @Repository 标注在dao层

@ComponentScan("")

还是翻译,整机扫描,咱们去看看括号里的“整机仓库”外面,哪些“整机”(类)须要被装载,Spring就会去扫描这个包,将外面所有标注了@Component的类进行注入。

这里的通过构造方法进行注入就很好了解了,咱们在拆卸MyBean这个整机的时候,忽然发现他必须在AnotherBean的根底上能力装置到IOC外面,那么咱们就在每次拆卸MyBean的时候主动拆卸一个AnotherBean进去。举个吧:

还是以汽车为例,咱们在踩油门登程之前,是不是必须发车??这里的AutoWired的内容就像发车,你不发车,这个油门你踩断都没有用,他都不会走。

通过set办法注入Bean

咱们能够在一个属性的set办法中去将Bean实现注入,看代码吧

MyBean类

@Componentpublic class MyBeanSet { private AnotherBean anotherBeanSet; @Autowired public void setAnotherBeanSet(AnotherBean anotherBeanSet) {     this.anotherBeanSet = anotherBeanSet; } @Override public String toString() {     return "MyBeanSet{" +         "anotherBeanSet=" + anotherBeanSet +         '}'; }}

Configuration类 和 Test类

同上一个,就不贴了

这里咱们发现在setter办法上咱们有一个@AutoWired,与下面不同的是,咱们不会在实例化该类时就主动拆卸这个对象,而是在显式调用setter的时候去拆卸。

通过属性去注入Bean

咱们后面两种注入的形式诸如工夫不同,并且代码较多,若是通过属性,即就是

@Componentpublic class MyBeanProperty { @Autowired private AnotherBean anotherBeanProperty; @Override public String toString() {     return "MyBeanProperty{" +         "anotherBeanProperty=" + anotherBeanProperty +         '}'; }}

这里咱们能够看到咱们这个类中须要应用AnotherBean这个实例对象,咱们能够通过@AutoWired去主动拆卸它。

对于有些小伙伴问公有属性,Spring怎么去加载它到IOC的?举荐去看看反射

通过List注入Bean

MyBeanList类

@Componentpublic class MyBeanList { private List<String> stringList; @Autowired public void setStringList(List<String> stringList) {     this.stringList = stringList; } public List<String> getStringList() {     return stringList; }}

MyConfiguration类

@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;    }}

这里咱们将MyBeanList进行了注入,对List中的元素会逐个注入。上面介绍另一种形式注入List

MyConfiguration类

@Bean//通过该注解设定Bean注入的优先级,不肯定间断数字@Order(34)public String string1(){    return "String-1";}@Bean@Order(14)public String string2(){    return "String-2";}

注入与List中泛型一样的类型,会主动去匹配类型,及时这里没有任何List的感觉,只是String的类型,但他会去通过List的Bean的形式去注入。

第二种形式的优先级高于第一种,当两个都存在的时候,若要强制去应用第一种形式,则要去指定Bean的id即可

通过Map去注入Bean

@Componentpublic 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; }}@Beanpublic Map<String,Integer> integerMap(){    Map<String,Integer> integerMap = new HashMap<String, Integer>();    integerMap.put("map-1",1);    integerMap.put("map-2",2);    return integerMap;}@Beanpublic Integer integer1(){    return 1;}@Beanpublic Integer integer2(){    return 2;}

同样这里也具备两种形式去注入Map类型Bean,且第二种的优先值高于第一种

以上就是Bean通过注解注入的几种形式,大家能够比照着xml注入的形式去看。

近期热文举荐:

1.1,000+ 道 Java面试题及答案整顿(2022最新版)

2.劲爆!Java 协程要来了。。。

3.Spring Boot 2.x 教程,太全了!

4.20w 程序员红包封面,快快支付。。。

5.《Java开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞+转发哦!