来源:juejin.cn/post/6844903813753602056通过注解注入bean背景说起Spring,肯定会提到IOC容器和DI依赖注入。Spring通过将每个类标记为一个bean注入到IOC容器中,达到控制反转的效果。那么我们在刚开始接触Bean的时候,一定要用xml文件来一个一个的注入,比如下面这个。如果我们的项目一般很大,需要几千个或者上百个Bean来使用,写起来很繁琐。那么Spring帮我们实现了一个通过注解注入的方法。只要在需要注入的类前面加上相应的注解,Spring就会帮我们扫描注入。xml扫描包的方法注解注入的一般形式总的来说bean注入有最直接易懂的方式,废话不多说下面,先贴出代码。BeanclasspublicclassMyBean{}Configurationclass//创建类配置文件@ConfigurationpublicclassMyConfiguration{//将一个Bean交给Spring管理@BeanpublicMyBeanmyBean(){returnnewMyBean();}}测试类和xml有点不同。这里在Test中,不再实例化ClassPathXmlApplicationContext,而是获取的AnnotationConfigApplicationContext实例。ApplicationContextcontext=newAnnotationConfigApplicationContext(MyConfiguration.class);MyBeanmyBean=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实现。直接看代码Beanclass@ComponentpublicclassMyBeanConstructor{privateAnotherBeananotherBeanConstructor;@AutowiredpublicMyBeanConstructor(AnotherBeananotherBeanConstructor){this.anotherBeanConstructor=anotherBeanConstructor;}@OverridepublicStringtoString(){return"MyBeanConstructor="Constructor"Constructor"+str+'}';}}AnotherBeanclass@Component(value="Bean的id,默认是类名小驼峰")publicclassAnotherBean{}Configurationclass@Configuration@ComponentScan("com.company.annotationbean")publicclassMyConfiguration{}这里我们可以发现和一般方式注入的代码不一样。看看新的注解是什么意思:@AutoWired简单粗暴,直译就是自动组装的意思。我还是不明白为什么叫自动装配。?看下一个笔记的解释就知道了。如果这里注入的时候指定了一个Beanid,需要用@Qualifier注解@Component(默认单例模式)么??这转化为零件,为什么有种修车的感觉??是的,Spring管理bean的方式就是修车的方式。当我们需要将一个类变成一个可以被Spring注入的Bean时,我们添加注解部分@Conmonent,然后我们就可以在加载Bean的时候像IOC车上的一个部分一样组装起来。这里我们再多几个注解也可以实现这个功能,具体就是@Component:@Controller在Controller层标注@Service在Service层标注@Repository在dao层标注@ComponentScan("")还是翻译,零件扫描,我们去看看括号里的“零件仓库”,需要加载哪些“零件”(类),Spring会扫描包,把里面所有标有@Component的类都注入进去。这里通过构造方法注入很容易理解。当我们组装MyBean部分时,突然发现必须在AnotherBean的基础上安装才能在IOC中安装。然后我们每次组装都会自动组装一个MyBean。又一个Bean进去,我给大家举个例子:还是拿汽车来说,是不是一定要先发动汽车,再踩油门??这里AutoWired的内容就像启动汽车一样。不发动车子,你踩油门也没用,他也不走。通过set方法注入Bean。我们可以在属性的set方法中注入Bean。请参阅代码。MyBean类@Componentpublic类MyBeanSet{privateAnotherBeananotherBeanSet;@AutowiredpublicvoidsetAnotherBeanSet(AnotherBeananotherBeanSet){this.anotherBeanSet=anotherBeanSet;}@OverridepublicStringtoString(){return"MyBeanSet{"+"anotherBeanSet="+anotherBeanSet+'}';}}Configuration类和Test类和上一个一样,这里就不贴了。我们发现我们有一个setter方法@AutoWired,和上面不同的是,我们在实例化类的时候不会自动组装这个对象,而是在我们显式调用setter的时候。通过properties来注入Bean,我们之前的两种注入方式在时间上是不一样的,代码也比较多。如果是通过属性,就是@ComponentpublicclassMyBeanProperty{@AutowiredprivateAnotherBeananotherBeanProperty;@OverridepublicStringtoString(){return"MyBeanProperty{"+"anotherBeanProperty="+anotherBeanProperty+'}';}}这里可以看到我们类中需要使用到实例对象AnotherBean,可以通过@AutoWired自动装配。对于一些小伙伴询问私有属性,Spring是如何加载到IOC中的呢?建议看一下反射,通过List注入BeanMyBeanList类@ComponentpublicclassMyBeanList{privateListstringList;@AutowiredpublicvoidsetStringList(ListstringList){this.stringList=stringList;}publicListgetStringList(){returnstringList;}}MyConfigurationclass@Configuration@ComponentScan("annoBean.annotationbean")publicclassMyConfiguration{@BeanpublicListstringList(){ListstringList=newArrayList();字符串列表。添加(“列表-1”);stringList.add("List-2");返回字符串列表;}}这里我们注入了MyBeanList,List中的元素会被一个一个注入。下面介绍另一种注入ListMyConfiguration类@Bean的方式//通过这个注解设置Bean注入的优先级,不一定是连续的数字@Order(34)publicStringstring1(){return"String-1";}@Bean@Order(14)publicStringstring2(){return"String-2";}注入与List中泛型相同的类型,会自动匹配类型。这里即使没有List的感觉,只是String的类型,但是会通过List的Bean来注入。第二种方法的优先级高于第一种。当两者都存在时,如果要强制使用第一种方法,需要指定bean的id,通过Map@ComponentpublicclassMyBeanMap{privateMapintegerMap;注入Bean;publicMapgetIntegerMap(){返回integerMap;}@AutowiredpublicvoidsetIntegerMap(MapintegerMap){this.integerMap=integerMap;}}@BeanpublicMapintegerMap(){MapintegerMap=newHashMap();integerMap.put("map-1",1);integerMap.put("map-2",2);returnintegerMap;}@BeanpublicIntegerinteger1(){return1;}@BeanpublicIntegerinteger2(){return2;}Map类型bean的注入也有两种方式,第二种优先级值更高第一种及以上下面是几种通过注解注入Bean的方式。可以和xml注入的方式对比一下。近期热点文章推荐:1.1000+Java面试题及答案(2022最新版)2.厉害了!Java协程来了。..3.SpringBoot2.x教程,太全面了!4.20w程序员红包封面,快拿。..5.《Java开发手册(嵩山版)》最新发布,赶快下载吧!感觉不错,别忘了点赞+转发!