springboot@bean注解,spring使用注解注入bean

  springboot@bean注解,spring使用注解注入bean

  00-1010 1、用后台xml扫描包的方式2、通过注释注入的一般形式2.1、Bean类2.2、配置类2.3、测试类3、通过构造方法注入Bean3.1、Bean类3.2、AnotherBean类3.3、配置类4、通过set方法注入Bean4.1、MyBean类4.2、配置类和测试类5、通过属性注入Bean6、通过List注入Bean6.1、MyBeanList类6.2、MyConfiguration类6.3、

  00-1010说到Spring,一定会提到IOC容器和DI依赖注入。Spring通过将类标记为Bean的方式将类注入IOC容器,达到了控制反转的效果。所以刚开始接触Bean的时候,一定要用xml文件,一个一个的注入,比如下面。

  Bean id= bean class= com . XXX . XXX . bean /如果我们的项目一般很大,需要上百个bean来使用,写起来很繁琐。那么Spring帮助我们实现了一种通过注释注入的方法。只需在需要注入的类前面添加相应的注释,Spring会帮我们扫描实现注入。

  

目录

context : component-scan base-package= com . XXX /

 

  00-1010一般来说,注入Bean有最直白易懂的实现注入的方式。下面废话不多说,先贴代码。

  

1、背景

公共类MyBean{ }

 

  00-1010//创建类配置文件@ configuration public class my configuration {//留一个Bean给Spring for management @ Bean public my Bean my Bean(){ return new my Bean();}}

  00-1010和xml有点不同。这里,在Test中,实例化不再是ClassPathXmlApplicationContext,而是获取的AnnotationConfigApplicationContext实例。

  application context context=new AnnotationConfigApplicationContext(my configuration . class);my bean my bean=cotext . get bean( my bean ,my bean . class);system . out . println( my bean= my bean);上面代码中的MyBean是一个我们需要Spring来管理的Bean。这只是一个简单的类。在MyConfiguration中,我们首先用@Configuration标注标记该类,表示该类是Spring的一个配置类,加载配置时就会加载。

  在MyConfiguration中,我们可以看到一个方法返回了一个MyBean的实例,该方法标有@Bean的注释,表示它是一个Bean注入方法,将下面返回的Bean注入到IOC中。

  00-1010当我们生成一个Bean实例时,我们可以使用Bean构造方法来注入Bean实现。直接看代码

  

xml扫描包的方式

@Component公共类mybean constructor { private AnotherBean AnotherBean constructor;@ auto wired public mybean constructor(AnotherBean AnotherBean constructor){ this . AnotherBean constructor=AnotherBean constructor;} @将公共字符串覆盖为Stri

 

  ng() { return "MyBean{" + "anotherBeanConstructor=" + anotherBeanConstructor + }; } }

 

  

3.2、AnotherBean类

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

 

  

3.3、Configuration类

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

这里我们可以发现,和一般方式注入的代码不一样了,我们来看看新的注解都是什么意思:

 

  @AutoWired

  简单粗暴,直接翻译过来的意思就是自动装配:wrench:,还不理解为什么叫自动装配:wrench:?看了下一个注解的解释你就知道了。若是在这里注入的时候指定一个Bean的id就要使用@Qualifier注解

  @Component(默认单例模式)

  什么??这翻译过来是零件,怎么感觉像是修汽车??是的,Spring管理Bean的方法就是修汽车的方式。我们在需要将一个类变成一个Bean被Spring可以注入的时候加上注解零件@Conmonent,那么我们就可以在加载Bean的时候把他像零件一样装配:wrench:到这个IOC汽车上了

  在这里我们还有几个其他的注解也可以实现这个功能,也就是细化的@Component:

  @Controller 标注在Controller层

  @Service 标注在Service层

  @Repository 标注在dao层

  @ComponentScan("")

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

  这里的通过构造方法进行注入就很好理解了,我们在装配MyBean这个零件的时候,突然发现他必须在AnotherBean的基础上才能安装到IOC里面,那么我们就在每次装配MyBean的时候自动装配:wrench:一个AnotherBean进去。举个:chestnut:吧:

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

  

 

  

4、通过set方法注入Bean

我们可以在一个属性的set方法中去将Bean实现注入,看代码吧

 

  

 

  

4.1、MyBean类

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

 

  

4.2、Configuration类 和 Test类

同上一个,就不贴了

 

  这里我们发现在setter方法上我们有一个@AutoWired,与上面不同的是,我们不会在实例化该类时就自动装配:wrench:这个对象,而是在显式调用setter的时候去装配。

  

 

  

5、通过属性去注入Bean

我们前面两种注入的方式诸如时间不同,并且代码较多,若是通过属性,即就是

 

  

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

这里我们可以看到我们这个类中需要使用AnotherBean这个实例对象,我们可以通过@AutoWired去自动装配它。

 

  

 

  

6、通过List注入Bean

 

  

6.1、MyBeanList类

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

 

  

6.2、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

 

  

 

  

6.3、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即可

  

 

  

7、通过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; }

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

 

  以上就是Bean通过注解注入的几种方式,大家可以对比着xml注入的方式去看。

  希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: