当前位置: 首页 > 科技观察

Spring注入bean的方式有好几种,还有谁不会呢?

时间:2023-03-15 00:14:19 科技观察

BeanInjectionThroughAnnotationBackground说起Spring,肯定会提到IOC容器和DI依赖注入。Spring通过将每个类作为Bean方法注入到IOC容器中来达到控制反转的效果。那么我们在刚开始接触Bean的时候,一定要用xml文件来一个一个的注入,比如下面这个。如果我们的项目一般很大,需要几千个或者上百个Bean来使用,写起来很繁琐。那么Spring帮我们实现了一个通过注解注入的方法。只要在需要注入的类前面加上相应的注解,Spring就会帮我们扫描注入。xml扫描包的方法注解注入的一般形式总的来说bean注入有最直接易懂的方式,废话不多说下面,先贴出代码。另外,Spring系列面试题及答案已经全部整理完毕。可以微信搜索Java面试库小程序,可以在线刷题。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简单粗暴,直接翻译就是自动装配:wrench:,我还是不明白为什么叫自动装配:wrench:?看完下一篇笔记的解释你就知道了。如果在此处注入时指定了Beanid,则需要使用@Qualifier注解。@Component(默认单例模式)什么??这转化为零件,修车的感觉如何??是的,Spring管理bean的方式就是修车的方式。当我们需要将一个类变成一个可以被Spring注入的Bean时,我们添加注解部分@Conmonent,然后我们就可以像加载Bean时的一个部分一样组装它:wrench:到这个IOC车上这里我们还有其他几个注解那也可以实现这个功能,就是细化的@Component:@Controller在Controller层标注@Service在Service层标注@Repository在dao层标注@ComponentScan("")或者翻译,部分扫描,我们再看括号中的“零件仓库”,需要加载哪些“零件”(类),Spring会扫描这个包,将所有标有@Component的类注入到里面。这里通过构造方法注入很容易理解。当我们组装MyBean部分时,突然发现必须在AnotherBean的基础上安装才能在IOC中安装。然后我们每次组装MyBean都会自动组装它:wrench:一个AnotherBean进去。举个例子吧:栗子:我们以汽车为例。我们必须在踩油门之前启动汽车吗?这里AutoWired的内容就像启动汽车一样。不会去通过set方法注入Bean。我们可以在属性的set方法中注入Bean。请参阅代码。MyBean类@Componentpublic类MyBeanSet{privateAnotherBeananotherBeanSet;@AutowiredpublicvoidsetAnotherBeanSet(AnotherBeananotherBeanSet){this.anotherBeanSet=anotherBeanSet;}@OverridepublicStringtoString(){return"MyBeanSet{"+"anotherBeanSet="+anotherBeanSet+'}';}}Configuration类和Test类和上一个一样,这里就不贴了。我们发现我们有一个setter方法@AutoWired,与上面不同的是,我们不会在实例化类的时候自动组装:wrench:对象,而是在显式调用setter时组装。通过properties来注入Bean,我们之前的两种注入方式在时间上是不一样的,代码也比较多。如果是通过属性,就是@ComponentpublicclassMyBeanProperty{@AutowiredprivateAnotherBeananotherBeanProperty;@OverridepublicStringtoString(){return"MyBeanProperty{"+"anotherBeanProperty="+anotherBeanProperty+'}';}}这里可以看到我们类中需要使用到实例对象AnotherBean,可以通过@AutoWired自动装配。通过List注入BeanMyBeanList类@ComponentpublicclassMyBeanList{privateListstringList;@AutowiredpublicvoidsetStringList(ListstringList){this.stringList=stringList;}publicListgetStringList(){returnstringList;}}MyConfigurationclass@Configuration@ComponentScan("annoBean.annotationbean")publicclassMyConfiguration{@BeanpublicListstringList(){ListstringList=newArrayList();stringList.add("List-1");stringList.add("List-2");返回字符串列表;}}这里我们注入了MyBeanList,List中的元素会被一个一个注入。MyConfigurationclass@Bean//通过该注解设置Bean注入的优先级,不一定是连续的数字@Order(34)publicStringstring1(){return"String-1";}@Bean@Order(14)publicStringstring2(){return"String-2";}注入与List中泛型相同的类型,会自动匹配类型。这里就算没有List的感觉,只是String的类型,但是会通过List的Bean来注入。通过Map@Component注入BeanpublicclassMyBeanMap{privateMapintegerMap;publicMapgetIntegerMap(){返回integerMap;}@AutowiredpublicvoidsetIntegerMap(MapintegerMap){这个.integerMap=integerMap;}}@BeanpublicMapintegerMap(){MapintegerMap=newHashMap();integerMap.put("map-1",1);integerMap.put("map-2",2);returnintegerMap;}@BeanpublicIntegerinteger1(){return1;}@BeanpublicIntegerinteger2(){return2;}Map类型Bean的注入也有两种方式,第二种的优先级值要高属于第一种。以上就是通过注解注入Bean的几种方式。可以和xml注入的方式对比一下。