番禺核酸检测点查询哈尔滨网站seo公司

diannao/2026/1/23 9:56:13/文章来源:
番禺核酸检测点查询,哈尔滨网站seo公司,深圳世展建设有限公司,服装设计好找工作吗目录 一、前言 二、通过类型来获取Bean 0.总述#xff08;重要#xff09; : 1.基本介绍 : 2.应用实例 : 三、通过指定构造器为Bean注入属性 1.基本介绍 : 2.应用实例 : 四、通过p命名空间为Bean注入属性 1.基本介绍 : 2.应用实例 : 五、通过ref引用实现Bean的相…目录 一、前言 二、通过类型来获取Bean 0.总述重要 :  1.基本介绍 :  2.应用实例 :  三、通过指定构造器为Bean注入属性 1.基本介绍 :  2.应用实例 :  四、通过p命名空间为Bean注入属性 1.基本介绍 :  2.应用实例 :  五、通过ref引用实现Bean的相互引用 1.基本介绍 :  2.应用实例 :  六、对Bean注入属性的内容延伸 1.准备工作 :  2.注入List类型的属性 :  3.注入Set类型的属性 :  4.注入Map类型的属性 :  5.注入数组类型的属性 :  6.注入Properties类型的属性 :  7.List属性注入之通过util命名空间注入 :  8.级联属性注入 :  七、通过静态工厂获取Bean 1.基本介绍 :  2.应用实例 :  八、通过实例工厂获取Bean 1.基本介绍 :  2.应用实例 :  九、通过FactoryBean获取Bean 1.基本介绍 :  2.应用实例 :  十、关于Bean配置的更多内容和细节 十一、总结 一、前言 第二节内容up打算和大家分享一下Spring IOC——基于XML方式对Bean的配置和管理。(PS: 若对XML文件未曾了解可以去快速阅读一下up的“速通XML”一文。)注意事项——①代码中的注释也很重要②不要眼高手低自己跟着过一遍才有收获③点击文章的侧边栏目录或者文章开头的目录可以进行跳转。良工不示人以朴所有文章都会适时补充完善。大家如果有问题都可以在评论区进行交流或者私信up。感谢阅读 二、通过类型来获取Bean 0.总述重要 :  (1) Spring对Bean的管理主要包括两方面内容——创建bean对象 和 为bean注入属性。         (2) Spring对Bean的配置方式也主要是两种——基于XML文件配置的方式 和 基于注解配置的方式。         (3) 上一小节中我们已经演示过“通过id属性来获取bean对象”的方式(基于beans.xml配置文件)因此这一小节我们直接演示其他方式这一小节均是基于XML文件配置的方式。 1.基本介绍 :  (1) 通过类型来获取bean对象其实是调用了getBean(ClassT aClass)方法该方法与我们上一小节中演示的“通过id获取bean对象”的方法构成重载如下图所示 :          (2) 通过类型来获取bean时要求ioc容器中同一类型的bean对象只能有一个否则会抛出异常NoUniqueBeanDefinitionException不唯一Bean定义异常如下图所示 :          (3) “通过类型获取Bean”方式的适用场景——在一个线程中只需要一个对象实例单例的情况eg : XxxAction / Servlet / Controller / XxxService。         (4) PS : 在容器配置文件(eg : beans.xml)中给bean对象的属性赋值底层是通过setter方法完成的因此我们定义的JavaBean比如之前的Student类中必须提供相应的setter方法否则报错。 2.应用实例 :  需求 : 在beans.xml配置文件中配置一个bean对象并通过Class类型的方式来获取到该bean对象。                 首先我们需要创建一个JavaBean类以Student类为例Student类代码如下 :  package com.cyan.spring.bean;/*** author : Cyan_RA9* version : 21.0*/ public class Student {private String name;private int age;private int score;//PS : 无参构造器必须给出因为底层要通过反射来创建对象。public Student() {}public Student(String name, int age, int score) {this.name name;this.age age;this.score score;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public int getScore() {return score;}public void setScore(int score) {this.score score;}Overridepublic String toString() {return Student{ name name \ , age age , score score };} }然后在beans.xml配置文件中配置一个Student类对象beans.xml代码如下 :  ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--(1) 在根元素beans中通过bean/bean子元素来配置JavaBean对象。每配置一个bean相当于配置了一个Java对象。(2) bean子元素需要配置两个属性———class 和 id。其中class表示所要实例化类的正名(全类名);id表示该对象在Spring容器中的标识通过id可以获取到对象。(3) property子元素用于配置该对象的成员变量(对象的属性)其中name表示属性名称value表示属性的值。(4) XML内容回顾———若一个标签没有标签体以age/age为例可以简写为age/。--!-- 注意———此处我们没有为bean标签配置id属性默认的分配id为全类名#0 --bean classcom.cyan.spring.bean.Studentproperty namename valueRain/propertyproperty nameage value19/propertyproperty namescore value439/property/bean /beans 接着在测试类中调用getBean(ClassT aClass)方法以StudentBeanByXML类为测试类通过JUnit框架进行单元测试StudentBeanByXML类代码如下 :  package com.cyan.spring.test;import com.cyan.spring.bean.Student; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** author : Cyan_RA9* version : 21.0*/ public class StudentBeanByXML {//1.通过类型来获取BeanTestpublic void getBeanByClass() {//(1) 获取Spring容器对象//IOC : Inversion Of ControlApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);//(2) 通过JavaBean的类型来获取beanStudent bean ioc.getBean(Student.class);//(3) 打印出获取到的bean对象的信息System.out.println(bean);} }运行结果 :  三、通过指定构造器为Bean注入属性 1.基本介绍 :  “通过指定构造器为Bean注入属性”即在beans.xml配置文件中通过constructor-arg/标签来指定一个JavaBean中的带参构造利用该带参构造来完成对Bean的属性的初始化其配置方式本质仍然是“IOC——基于XML文件配置Bean”。如下图所示 :          上图中演示的为通过index索引来确定参数还可以通过type类型或者name属性名来确定参数本质都是根据形参来唯一确定一个带参构造。 2.应用实例 :  需求 : 在beans.xml文件中新配置一个Bean对象并通过constructor-arg/标签指定Student类的一个带参构造来初始化该Bean的属性。在StudentBeanByXML类中新定义一个单元测试方法通过id属性获取到该Bean对象并检测属性注入是否成功。                 仍然使用Student类作为JavaBean类首先我们要在beans.xml文件中配置Bean对象up为了演示一下index, type, name三种形式的constructor-arg/标签此处配置了三个Bean对象代码如下 :  bean classcom.cyan.spring.bean.Student idstu03constructor-arg valueCyan index0/constructor-argconstructor-arg value21 index1/constructor-argconstructor-arg value453 index2/constructor-arg/beanbean classcom.cyan.spring.bean.Student idstu04constructor-arg valueEisen typejava.lang.String/constructor-argconstructor-arg value21 typeint/constructor-argconstructor-arg value442 typeint/constructor-arg/beanbean classcom.cyan.spring.bean.Student idstu05constructor-arg valueFive namename/constructor-argconstructor-arg value20 nameage/constructor-argconstructor-arg value460 namescore/constructor-arg/bean 接着在StudentBeanByXML类中新定义一个方法分别获取到id stu03, id stu04, id stu05的对象insertPropertiesByConstructor()方法代码如下 :  //2.通过指定构造器为Bean注入属性//回顾————//对Bean的管理包括两方面 (1) 创建bean对象(2) 为bean对象注入属性。Testpublic void insertPropertiesByConstructor() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Student stu03 ioc.getBean(stu03, Student.class);Student stu04 ioc.getBean(stu04, Student.class);Student stu05 ioc.getBean(stu05, Student.class);System.out.println(stu03 stu03);System.out.println(stu04 stu04);System.out.println(stu05 stu05);} 运行结果 :  可以看到打印出的属性值与我们配置的一致说明带参构造成功初始化该bean对象。 四、通过p命名空间为Bean注入属性 1.基本介绍 :  前文中用bean/bean标签配置Bean对象时我们用到了class属性(JavaBean的全类名)id属性(对象在容器中的标识)。现在我们可以用p:property_name property_value(注意冒号)的形式直接在bean标签内部为Bean注入属性但直接使用会报错如下图所示 :          报错提示 : Namespace p is not bound命名空间p未绑定。          解决方法 : 将鼠标悬停在报错处按下Alt Enter选择Create namespace declaration创建命名空间声明如下图所示 :         创建命名空间声明后 可以看到beans.xml根元素中已经自动加入了p命名空间的声明如下图所示 :  2.应用实例 :  需求 : 在beans.xml文件中新配置一个Bean对象并通过p命名空间为该对象注入属性。在StudentBeanByXML类中新定义一个单元测试方法打印该对象信息检测属性注入是否成功。                 仍然使用Student类作为JavaBean类首先我们要在beans.xml文件中配置Bean对象代码如下 :  bean classcom.cyan.spring.bean.Student idstu06 p:namePeter p:age33 p:score1024/bean 接着在StudentBeanByXML测试类中新定义一个方法获取到id stu06的对象injectPropertiesByP()方法代码如下 :  //3.通过p命名空间为Bean注入属性Testpublic void injectPropertiesByP() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Student stu06 ioc.getBean(stu06, Student.class);System.out.println(stu06 stu06);} 运行结果 :  五、通过ref引用实现Bean的相互引用 1.基本介绍 :  (1) 在JavaWeb中比方说我们之前的Web项目——尘歌壶摆设购买系统我们利用“分层设计”的思想会分别建立DAO层Service层Web层并根据“Web层调用Service层Service层调用DAO层”的思想在XxxServlet类中new一个XxxService对象在XxxService类中new一个XxxDao对象。如下图所示 :          (2) 而有了Spring的加持后我们可以通过ref引用来实现IOC容器中bean对象的相互引用其本质是通过ref引用来为一个对象的引用类型的属性进行初始化即属于“Bean管理——为bean注入属性”的范畴。         (3) ref在使用时是作为property标签的一个属性我们之前已经多次用到了property标签格式如下 :          property nameproperty_name refotherBeans id/property更多说明见beans.xml中的注释 2.应用实例 :  需求 : 参照之前JavaWeb中“分层设计”的思想在Service层中调用DAO层但是不直接new出实例而是在beans.xml文件中通过ref进行配置试着根据输出语句测试注入属性是否成功。                 首先我们需要创建用于测试的Service层和DAO层的类以我们之前的尘歌壶摆设购买系统为参考如下图所示 :  FurnishingDAOImpl类代码如下 : 定义了一个用于添加摆设的addFurnishing方法 package com.cyan.spring.dao;/*** author : Cyan_RA9* version : 21.0*/ public class FurnishingDAOImpl {public FurnishingDAOImpl() {System.out.println(FurnishingDAOImpl的无参构造被调用~);}public void addFurnishing() {System.out.println(FurnishingDAOImpl的addFurnishing方法被调用~);} }FurnishingServiceImpl类代码如下 : 定义了一个FurnishingDAOImpl类型的属性我们将在beans.xml文件中对其进行初始化此外定义addFurnishing()方法实现Service层调用DAO层 package com.cyan.spring.service;import com.cyan.spring.dao.FurnishingDAOImpl;/*** author : Cyan_RA9* version : 21.0*/ public class FurnishingServiceImpl {//Service层调用DAO层但通过beans.xml文件进行配置private FurnishingDAOImpl furnishingDAO;public FurnishingServiceImpl() {}//About DAOs setter,getterpublic FurnishingDAOImpl getFurnishingDAO() {return furnishingDAO;}public void setFurnishingDAO(FurnishingDAOImpl furnishingDAO) {this.furnishingDAO furnishingDAO;}//Add Furnishingpublic void addFurnishing() {System.out.println(FurnishingServiceImpl的addFurnishing()方法被调用~);furnishingDAO.addFurnishing();} }在beans.xml中配置这两个对象代码如下 : 注意看注释 !--(1) 配置一个FurnishingDAOImpl对象;(2) 由于该类暂时没有设置属性所以此处不需要注入属性(下面furnishingService01同理)--bean classcom.cyan.spring.dao.FurnishingDAOImpl idfurnishingDAO01/!--(1) 配置一个FurnishingServiceImpl对象;(2) 仍然是通过property子元素来为id furnishingService01的对象注入属性,注意此处的ref 表示————id furnishingService01的对象的furnishingDAO属性引用了上面配置的id furnishingDAO01的对象PS : ref体现了Spring的依赖注入(即一个对象的属性引用了另一个对象)--bean classcom.cyan.spring.service.FurnishingServiceImpl idfurnishingService01property namefurnishingDAO reffurnishingDAO01/property/bean 最后仍是在StudentBeanByXML测试类中新定义一个方法测试FurnishingServiceImpl的FurnishingDAOImpl属性是否被初始化。                 injectPropertiesByRef()方法代码如下 :  //4.通过ref引用实现Bean的相互引用Testpublic void injectPropertiesByRef() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);FurnishingServiceImpl furnishingService01 ioc.getBean(furnishingService01, FurnishingServiceImpl.class);furnishingService01.addFurnishing();} 运行结果 :  可以看到FurnishingDAOImpl类的无参构造成功被调用说明它被成功初始化。                 其实除了使用ref属性外还可以直接通过配置内部bean来完成对属性的初始化如下所示 :  bean classcom.cyan.spring.service.FurnishingServiceImpl idfurnishingService02property namefurnishingDAObean classcom.cyan.spring.dao.FurnishingDAOImpl//property/bean 这种方法的意思是对引用类型属性的初始化不再使用Spring容器中已经配置好的对象而是自己重新配置一个bean。经过测试配置内部bean的方法也可以成功注入大家有兴趣可以自己去试试。 六、对Bean注入属性的内容延伸 1.准备工作 :  上文中我们已经演示过多种为bean注入属性的方式比如“通过指定构造器注入”“通过p命名空间注入”“通过ref引用实现Bean的相互引用”等。现在让我们来讨论一下如果bean对象的属性是数组或者集合类型我们又该怎样去注入呢 为了实现“注入数组 or 集合类型的属性”我们先来创建一个类去维护这些属性School类代码如下 :  package com.cyan.spring.bean;import java.util.*;/*** author : Cyan_RA9* version : 21.0*/ public class School {private ListStudent studentList;private SetStudent studentSet;private MapString, Student studentMap;private String[] studentNames;private Properties pros;public School() {}public School(ListStudent studentList, SetStudent studentSet, MapString, Student studentMap, String[] studentNames, Properties pros) {this.studentList studentList;this.studentSet studentSet;this.studentMap studentMap;this.studentNames studentNames;this.pros pros;}public ListStudent getStudentList() {return studentList;}public void setStudentList(ListStudent studentList) {this.studentList studentList;}public SetStudent getStudentSet() {return studentSet;}public void setStudentSet(SetStudent studentSet) {this.studentSet studentSet;}public MapString, Student getStudentMap() {return studentMap;}public void setStudentMap(MapString, Student studentMap) {this.studentMap studentMap;}public String[] getStudentNames() {return studentNames;}public void setStudentNames(String[] studentNames) {this.studentNames studentNames;}public Properties getPros() {return pros;}public void setPros(Properties pros) {this.pros pros;}Overridepublic String toString() {return School{ studentList studentList , studentSet studentSet , studentMap studentMap , studentNames Arrays.toString(studentNames) , pros pros };} }接着我们在beans.xml中配置一个School类型的Bean对象代码如下 :  bean classcom.cyan.spring.bean.School idschool01!-- 暂未给属性赋值 --/bean 2.注入List类型的属性 :  在刚刚配置的id school01的bean对象中通过property标签为List类型的属性初始化代码如下 :  property namestudentListlistref beanstu03/refref beanstu04/ref/list/property 注入除了通过ref元素配置的形式外也可以在List元素中直接配置内部Bean。仍然是在StudentBeanByXML测试类中我们新定义一个方法测试List属性是否注入成功injectList()方法代码如下 :  //5.为Bean注入集合 or 数组类型的属性//5.1 注入List类型的属性Testpublic void injectList() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);School school01 ioc.getBean(school01, School.class);System.out.println(School中的studentList属性如下 : );System.out.println(school01.getStudentList());} 运行结果 :  3.注入Set类型的属性 :  在“准备工作”中配置的id school01的bean对象中通过property标签为Set类型的属性初始化代码如下 :  property namestudentSetsetref beanstu03/refref beanstu06/ref/set/property 可以看到Set类型属性的配置和List类型属性的配置非常类似只不过前者是放在set元素里了。仍然是在StudentBeanByXML测试类中我们新定义一个方法测试Set属性是否注入成功injectSet()方法代码如下 :  //5.2 注入Set类型的属性Testpublic void injectSet() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);School school01 ioc.getBean(school01, School.class);System.out.println(School中的studentSet属性如下 : );System.out.println(school01.getStudentSet());} 运行结果 :  4.注入Map类型的属性 :  在“准备工作”中配置的id school01的bean对象中通过property标签为Map类型的属性初始化代码如下 :  property namestudentMapmap!-- entry表示一个键值对 --entry!-- key元素用于配置key注意此处的value是指key的值字面意思 --keyvaluekeyStu05/value/key!-- 紧跟key元素其后的ref元素才是真正的当前键值对的value --ref beanstu05/ref/entryentrykeyvaluekeyStu06/value/keyref beanstu06/ref/entry/map/property 仍然是在StudentBeanByXML测试类中我们新定义一个方法测试Map属性是否注入成功injectMap()方法代码如下 :  //5.3 注入Map类型的属性Testpublic void injectMap() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);School school01 ioc.getBean(school01, School.class);System.out.println(School中的studentMap属性如下 : );System.out.println(school01.getStudentMap());} 运行结果 :  5.注入数组类型的属性 :  在“准备工作”中配置的id school01的bean对象中通过property标签为String[]类型的属性初始化代码如下 :  property namestudentNamesarray!-- 由于School类中维护的数组为String类型所以此处直接以value元素配置 --valueCyan/valuevalueRain/valuevalueFive/valuevalueIce/value/array/property 仍然是在StudentBeanByXML测试类中我们新定义一个方法测试String[]属性是否注入成功injectArray()方法代码如下 :  //5.4 注入数组类型的属性Testpublic void injectArray() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);School school01 ioc.getBean(school01, School.class);System.out.println(School中的studentNames属性如下 : );System.out.println(Arrays.toString(school01.getStudentNames()));} 运行结果 :  6.注入Properties类型的属性 :  在“准备工作”中配置的id school01的bean对象中通过property标签为Properties类型的属性初始化代码如下 :  property nameprospropsprop keyusernameCyan_RA9/propprop keypassword55555233/propprop keyip127.0.0.1/prop/props/property 仍然是在StudentBeanByXML测试类中我们新定义一个方法测试Properties属性是否注入成功injectProperties()方法代码如下 :  //5.5 注入Properties类型的属性Testpublic void injectProperties() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);School school01 ioc.getBean(school01, School.class);System.out.println(School中的pros属性如下 : );System.out.println(school01.getPros());} 运行结果 :  7.List属性注入之通过util命名空间注入 :  方才我们已经见过了如何通过property元素注入List类型的属性其实是通过list子元素来实现的。                 那么假设现在给出一个需求已知成华大道到二仙桥的路上开着两家书店它们都卖《生死疲劳》《镜花缘》《湘行散记》《明朝那些事儿》《三体》这几本书让你在beans.xml中配置这俩个书店对象你能吗                 你可能会说哟瞧你这话说的这不是张飞吃豆芽——小菜一碟么看我一波张飞穿针——粗中有细给你整得明明白白服服帖帖。 于是你就开整了先新定义一个BookStore的JavaBean类代码如下 :  package com.cyan.spring.bean;import java.util.List;public class BookStore {private ListString bookList;public BookStore() {}public BookStore(ListString bookList) {this.bookList bookList;}public ListString getBookList() {return bookList;}public void setBookList(ListString bookList) {this.bookList bookList;} }再去beans.xml文件中配置一波代码如下 :  bean classcom.cyan.spring.bean.BookStore idbookStore01property namebookListlistvalue生死疲劳/valuevalue镜花缘/valuevalue湘行散记/valuevalue明朝那些事儿/valuevalue三体/value/list/property/beanbean classcom.cyan.spring.bean.BookStore idbookStore02property namebookListlistvalue生死疲劳/valuevalue镜花缘/valuevalue湘行散记/valuevalue明朝那些事儿/valuevalue三体/value/list/property/bean 你还不尽兴 继续去测试类中定义了一个单元测试的方法设法输出bookList属性进行检验testMySB()方法代码如下 :  Testpublic void testMySB() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);BookStore bookStore01 ioc.getBean(bookStore01, BookStore.class);BookStore bookStore02 ioc.getBean(bookStore02, BookStore.class);System.out.println(bookStore01s bookList bookStore01.getBookList());System.out.println(bookStore02s bookList bookStore02.getBookList());} 运行结果 :  对此我只能说“你™是真B呀”。是的坦白说你做的针不戳儿。但是呢假如现在成华大道到二仙桥的路上又开了5家书店阁下又如何应对呢                 你可能会想那我再配5个Bean不就完事儿了么这up怎么磨磨唧唧的阴阳怪气你到底想说啥你说呗整这么绕一大圈子。对此我想说“你再配5个Bean也确实能成事儿但我说你这么配就慢了我们不仅要配得对而且要配得快。 于是便要引出util命名空间了。直接上代码 : (当你使用util:list时IDEA会自动帮你引入util命名空间) util:list idcommonBookListvalue生死疲劳/valuevalue镜花缘/valuevalue湘行散记/valuevalue明朝那些事儿/valuevalue三体/value/util:listbean classcom.cyan.spring.bean.BookStore idbookStore01property namebookList refcommonBookList/property/beanbean classcom.cyan.spring.bean.BookStore idbookStore02property namebookList refcommonBookList/property/bean 可以看到其实就是将大家都有的放到了util:list/util:list元素中然后在每个BookStore类型的Bean对象中利用ref引用到util:list中。经测试输出结果是一样的这里就不再放图了。 8.级联属性注入 :  所谓“级联属性注入”其实指的是Spring的IOC容器可以直接为“属性的属性”赋值即当类中的某个属性有自己的属性时我们希望在配置该类Bean对象时将对象属性和对象属性的属性都注入。 需求 : 定义一个员工类维护员工id员工姓名员工的部门名称三个属性其中员工的部门名称通过部门类的deptName属性表示。要求设法实现“级联属性注入”。                 首先我们需要定义员工类和部门类如下 :                  Employee类代码如下 :  package com.cyan.spring.bean;public class Employee {private Integer id;private String name;private Department department;public Employee() {}public Employee(Integer id, String name, Department department) {this.id id;this.name name;this.department department;}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department department;}Overridepublic String toString() {return Employee{ id id , name name \ , department department };} }Department类代码如下:  package com.cyan.spring.bean;public class Department {private String deptName;public Department() {}public Department(String deptName) {this.deptName deptName;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName deptName;}Overridepublic String toString() {return Department{ deptName deptName \ };} }接着在beans.xml文件中配置Employee对象和Department对象。代码如下 :  !-- 配置Employee类对象 --bean classcom.cyan.spring.bean.Employee idemployee01property nameid value1/propertyproperty namename valueCyan/propertyproperty namedepartment refdepartment01/property!-- [级联属性注入] --!-- 其实是通过对象名.属性名的形式对属性的属性进行注入操作 --property namedepartment.deptName valueBack-End/property!-- 此处底层实际调用了setDeptName方法 --/bean!-- 配置Department类对象 --bean classcom.cyan.spring.bean.Department iddepartment01/ 最后在测试类StudentBeanByXML中新定义一个单元测试方法输出配置的Employee对象查看级联属性注入是否成功。testCascade()方法代码如下 :  //PS : 测试级联属性赋值Testpublic void testCascade() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Employee employee01 ioc.getBean(employee01, Employee.class);System.out.println(employee01 \n employee01);} 运行结果 :  可以看到employee对象的属性department的属性deptName被成功初始化说明级联属性注入成功。 七、通过静态工厂获取Bean 1.基本介绍 :  “通过静态工厂获取Bean”本质上还是“基于XML方式配置Bean”只不过我们并不直接在配置Bean时就为Bean对象注入属性而是事先在静态工厂类的static代码块中完成了初始化用静态的Map容器来存储Student类对象并提供了一个静态方法用于获取已经初始化好的Student对象然后在beans.xml配置文件中我们只需要给定一个key并指定调用静态工厂提供的用于获取Student对象的方法Spring容器便可以根据该key获取到对应的Student类对象。         注意 :          (1) “通过静态工厂获取Bean”在配置bean时class不再是Student类的全路径而是静态工厂类的全路径.         (2) 除了id和class外还需要一个属性factory-method表示指定一个静态工厂类的用于返回Bean对象的方法.         (3) 至于bean元素内部则需要使用construcotr-arg valuekey/标签说明要获取的对象在静态工厂类中对应的key。 2.应用实例 :  上面说了一堆只是看肯定多少觉得一头雾水下面我们来个实例感受一下。                 首先up定义一个自己的静态工厂类CyanStaticFactory类代码如下 :  package com.cyan.spring.factory;import com.cyan.spring.bean.Student;import java.util.HashMap; import java.util.Map;public class CyanStaticFactory {//维护一个静态Map集合用于保存Student对象private static MapString, Student studentMap;//在静态代码块中初始化Student对象static {studentMap new HashMap();studentMap.put(student01, new Student(Cyan, 21, 450));studentMap.put(student02, new Student(Rain, 19, 460));}//提供一个获取Student对象的静态方法public static Student getStudent(String key) {return studentMap.get(key);} }然后在beans.xml文件中完成配置代码如下 : 注意看up配置的id表明最终返回的其实是一个Student类对象 bean classcom.cyan.spring.factory.CyanStaticFactory idstu07factory-methodgetStudentconstructor-arg valuestudent02//bean 最后仍然是在测试类StudentBeanByXML中定义一个单元测试方法获取到Student对象                 getBeanByStaticFactory()方法代码如下 : 注意此处getBean方法得到的是Student类型的对象 //6.通过静态工厂获取BeanTestpublic void getBeanByStaticFactory() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Student stu07 ioc.getBean(stu07, Student.class);System.out.println(stu07);} 运行结果 :  八、通过实例工厂获取Bean 1.基本介绍 :  “通过实例工厂获取Bean”和通过静态工厂获取Bean类似只不过见名知意我们在自定义实例工厂类中通过非静态Map容器来保存Student类对象并在非静态代码块中对Map容器进行初始化并提供一个用于获取Student对象的非静态方法。然后在beans.xml文件中除了指定一个用于获取Bean对象的方法以及给出key外必须先指定一个实例工厂对象。         回顾一下代码块——         静态代码块随着类的加载而被隐式地调用最多只能执行一次而对于非静态代码块每实例化一次包含该非静态代码块的类都会执行一次该类中的非静态代码块。         结合代码块的内容回顾我们可以猜到必须先实例化“实例工厂对象”以执行其非静态代码块中的内容完成对非静态Map集合的初始化然后才能获取到其保存的学生对象。         注意 :          (1) “通过实例工厂获取Bean”在配置bean时需要同时配置实例工厂对象和学生对象.         (2) 属性factory-method表示指定一个实例工厂类的用于返回Bean对象的方法属性factory-bean表示指定使用一个特定的实例工厂对象返回Bean。         (3) bean元素内部仍需要使用construcotr-arg valuekey/标签说明要获取的对象在实例工厂类中对应的key。 2.应用实例 :  首先up定义一个自己的实例工厂类CyanInstanceFactory类代码如下 :  package com.cyan.spring.factory;import com.cyan.spring.bean.Student;import java.util.HashMap; import java.util.Map;public class CyanInstanceFactory {private MapString, Student studentMap;{studentMap new HashMap();studentMap.put(student03, new Student(Eisen, 22, 437));studentMap.put(student04, new Student(Five, 20, 429));}public Student getStudent(String key) {return studentMap.get(key);} }然后在beans.xml文件中完成配置代码如下 : !-- 配置实例工厂对象 --bean classcom.cyan.spring.factory.CyanInstanceFactory idcyanInstanceFactory01/!-- 配置学生对象 --bean idstu08 factory-beancyanInstanceFactory01 factory-methodgetStudentconstructor-arg valuestudent03//bean 最后仍然是在测试类StudentBeanByXML中定义一个单元测试方法获取到Student对象                 getBeanByInstanceFactory()方法代码如下 : //7.通过实例工厂获取BeanTestpublic void getBeanByInstanceFactory() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Student stu08 ioc.getBean(stu08, Student.class);System.out.println(stu08);} 运行结果 :  九、通过FactoryBean获取Bean 1.基本介绍 :  “通过FactoryBean获取Bean”又和上文中通过实例工厂获取Bean类似都是维护了一个非静态Map容器来保存Bean对象并在非静态代码块中对Map容器进行初始化此外还需要单独维护一个String类型的key属性。并且我们需要去实现FactoryBean接口并重写接口中的方法。         注意 :          (1) “通过FactoryBean获取Bean”在配置bean时class为FactoryBean的全类名.         (2) 通过property子元素为key属性初始化。 2.应用实例 :  首先我们需要定义一个自己的FactoryBean类并实现FactoryBean接口CyanFactoryBean类代码如下 :  package com.cyan.spring.factory;import com.cyan.spring.bean.Student; import org.springframework.beans.factory.FactoryBean;import java.util.HashMap; import java.util.Map;public class CyanFactoryBean implements FactoryBeanStudent {private String key;private MapString, Student studentMap;{studentMap new HashMap();studentMap.put(student05, new Student(Irving, 32, 427));studentMap.put(student06, new Student(Rose, 23, 431));}public void setKey(String key) {this.key key;}Overridepublic Student getObject() throws Exception {return studentMap.get(key);}Overridepublic Class? getObjectType() {return Student.class;}Overridepublic boolean isSingleton() {return FactoryBean.super.isSingleton();} }接着在beans.xml中配置bean对象代码如下 :  !-- 配置Student对象通过FactoryBean获取 --bean classcom.cyan.spring.factory.CyanFactoryBean idstu09property namekey valuestudent05//bean 最后在StudentBeanByXML测试类中定义一个单元测试方法测试是否配置成功。getBeanByFactoryBean()方法代码如下 :  //8.通过实例工厂获取BeanTestpublic void getBeanByFactoryBean() {ApplicationContext ioc new ClassPathXmlApplicationContext(beans.xml);Student stu09 ioc.getBean(stu09, Student.class);System.out.println(stu09);} 运行结果 :  十、关于Bean配置的更多内容和细节 由于“Spring IOC—基于XML配置和管理Bean”内容较多而up写到这里时编辑器已经很卡了。故打算将Bean配置信息重用Bean生命周期以及Bean后置处理器等内容单独放一篇文章中。         链接如下 :          待更新--- 十一、总结 以上就是Spring系列博文第二小节的全部内容了。总的来看Spring 基于XML配置和管理Bean内容很多我们可以通过多种方式获取Bean或者为Bean注入属性足以感受到Spring配置和管理Bean的灵活性。再来简单回顾一下上文的总述如下图所示 : 下一节内容——Spring IOC——基于注解配置和管理Bean。感谢阅读 System.out.println(END-------------------------------------------------);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/88969.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dw怎么切片做网站龙岗网站

目录 一、Ajax 1、简介 2、Axios (没懂 暂留) (1)请求方式别名 (2)发送get请求 (3)发送post请求 (4)案例 二、前端工程化 1、Vue项目-目录结构 2、…

举报不良网站信息怎么做网站开发 后端

本文主要介绍的是一个球形水波的百分比控件,市面上有各种形形色色的百分比控件,我一直觉得水波是最炫的,UI给了我这个机会,然而网上搜了一大堆,不是太复杂,代码太多(反正我是调不出效果来),就是…

中国网站开发哪里可以做企业网站

破解root密码(Linux 7) 1、先重启——e 2、Linux 16这一行 末尾加rd.break(不要回车)中断加载内核 3、再ctrlx启动,进入救援模式 4、mount -o remount,rw /sysroot/——(mount挂载 o——opti…

安亭网站建设建阳建设局网站

之前那篇博客是在入门网络流时写的&#xff0c;现在对网络流重新有了一定的理解。 1. 最大流 FF 增广思想 Ford–Fulkerson 增广&#xff0c;核心即不断找增广路并增广。 dfs 实现 // FF brute #include <bits/stdc.h> #define int long longusing namespace std;in…

哪个网站可以做经济模拟题前端开发培训机构tuj

文章目录 一、Ided常用工具栏显示二、更改idea主题设置三、设置代码编辑器背景颜色为豆沙绿四、设置新项目 默认Jdk配置、maven配置1、settings for new projects2、structre for new projects 五、修改代码中注释的字体颜色六、设置编辑器字体大小七、文件编码的设置(可以设置…

seo兼职网智能网站推广优化

URI Uniform Resource Identifier 同一资源标识符 以特定语法标识一个资源的字符串 绝对URI&#xff1a;URI模式模式特有部分 scheme:scheme-specific-part scheme分为&#xff1a; data file本地文件系统 ftp http telnet urn 统一资源名 scheme-specific-part为&am…

怎样做网站的当前位置栏小程序app开发多少钱

目录 一&#xff0c;针对问题二&#xff0c;下载1&#xff0c;github里面下载FastGitHub2&#xff0c;博客上传了下载资源 三&#xff0c;安装使用点击执行文件即可 一&#xff0c;针对问题 当使用github很慢&#xff0c;或者根本打不开的时候&#xff0c;就可以使用该工具 …

站长工具网站备案百度网址收录入口

http://blog.csdn.net/charlesprince/article/details/5924376 TDI FILTER 过滤驱动的功能一般用来进行整个系统中的所有网络流量的分析&#xff0c;记录和管理&#xff0c;可以实现非常强大的管理功能&#xff0c;这里就将讨论它的设计架构&#xff0c;和具体实现的方法。  …

如何对网站的图片做cdn免费软件加速器

找到一个可靠的步骤&#xff0c;点开链接&#xff1a;http://blog.csdn.net/kevindr/article/details/22154323

做封面的软件ps下载网站iis的网站登录没反应

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 目录 你是否听过Linux下一切皆文件&#xff1f; 在哪里我们体悟到了Linux下一切皆文件&#xff1f; 文件是什么&#xff1f; 在哪里体悟到的&#xff1f; 常见疑惑 怎么办到的Linux下一切皆文件&#xff1f; 我们抛出…

怎么看网站被降权征婚网站开发

算法-快速幂 时间复杂度 O(logk) //求 m^k mod p int qmul(int m,int k,int p) {int res1%p;while(k){if(k&1){res*m;res%p;}m*m;m%p;k>>1;}return res; }

用html5做网站的优点建筑工程公司有哪些岗位

1、取得控制台应用程序的根目录方法 方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录&#xff0c;它由程序集冲突解决程序用来探测程序集 2、取得Web应用程序的根目录方法 方法1、HttpRun…

西安 网站建设 培训班wordpress全站伪静态

Lua 是一个功能强大、高效、轻量级、可嵌入的脚本语言。它支持程序编程、面向对象程序设计、函数式编程、数据驱动编程和数据描述。 Lua的官方网站上只提供了源码&#xff0c;需要使用Make进行编译&#xff0c;具体的编译方法为 curl -R -O http://www.lua.org/ftp/lua-5.4.6.…

网站如何做地面推广企航网络推广

1 必须修改端口号 port 323422 2 必须设置密码&#xff0c;并且有一定的复杂度 requirepass dske#123 3 绑定bind ip bind 127.0.0.1 4 使用无登录权限的用户运行redis 配置文件路径 /www/server/redis/redis.conf 一般都是安装文件里 如果忘记密码和端口号也可以去配置文件…

做网站备案是什么意思杭州公积金网站查询系统

1.电子商务最重要的是&#xff08; &#xff09; A.商务 B.网站 C.货物 D.信息技术 正确 正确答案&#xff1a;左边查询 学生答案&#xff1a;A 2.目前菜鸟网络依赖大数据和云计算已实现了哪些功能&#xff08; &#xff09; A.自动化仓库 B.智能发货 C.物流云加速 D.以上都正确…

班级网站怎么做自己做网站怎么让字体居中

文章目录 Spring Security OAuth2如何自定义返回的 Token 信息定制不透明令牌的信息Springsecurity-oauth2之TokenEndPoint参考Spring Security OAuth2如何自定义返回的 Token 信息 Spring Boot+OAuth2,如何自定义返回的 Token 信息? 参考URL: https://www.jianshu.com/p/b7…

北京公司响应式网站建设价位北京品牌建设网站公司

作者&#xff1a;老齐对于初学Python者&#xff0c;除了看书(《跟老齐学Python&#xff1a;轻松入门》或者《Python大学实用教程》&#xff0c;均为电子工业出版社出版)、或者看视频(网易云课堂、CSDN上均有老齐的视频课程)&#xff0c;还要进行专题性总结。比如本文&#xff0…

做产品推广得网站网站建设 蔬菜配送

明理信息科技知识付费saas租户平台 在当今的知识付费时代&#xff0c;我们面临着一个重要的问题&#xff1a;如何从众多的知识付费平台中选择适合自己的平台&#xff1f;本文将为您提供一些实用的建议&#xff0c;帮助您做出正确的选择。 首先&#xff0c;我们需要了解自己的…

实施网站推广的最终目的太湖网站建设

1&#xff1a;ARPG类型游戏 2:MMORPG 3:MOBA 4:卡牌类 5&#xff1a;棋盘类 转载于:https://www.cnblogs.com/like-minded/p/8297718.html

自己做的网站加载不出验证码网站和微信同步建设

1.关于MITK&#xff1a; MITK的全称是”The Medical Imaging Interaction Toolkit”。它是一款开源的交互式医学图像处理软件开发和应用平台。MITK将ITK和VTK整合为一个应用框架。该软件由德国一个研究室开发。官网地址为&#xff1a;http://mitk.org/wiki/MITK 。 与MITK同名…