前面已经介绍了如何入门Spring编码和IOC的核心概念。今天,我将解释Spring-DI的另一个重点。Spring核心模块DI概念IoC其实有两种方式,一种是DI(DependencyInjection),另一种是DL(DependencyLookup),也就是依赖查找。前者是当前组件被动接受IoC容器注入的依赖组件,后者是主动去服务注册位置寻找其依赖组件的组件。我们在这里专注于DI。IoC的要点之一就是在系统运行过程中动态地向一个对象提供它所需要的其他对象。这是通过DI实现的。比如对象A需要操作数据库。以前我们总是要在A中写代码获取一个Connection对象。而有了spring,我们只需要告诉spring,A中需要一个Connection,至于如何以及何时构造这个Connection,A不需要知道。通过依赖注入机制,我们只需要通过简单的配置,无需任何代码,指定目标需要的资源,完成自己的业务逻辑,而不用关心具体的资源来自哪里,由谁来实现。当系统运行时,sp??ring会在适当的时候创建一个Connection,然后像注入一样注入到A中,从而完成对各个对象之间关系的控制。A需要依赖Connection才能正常运行,而这个Connection是由spring注入到A中的,所以依赖注入的名字就是由此而来。Spring通过反射技术实现注入,允许程序在运行时动态生成对象,执行对象方法,改变对象属性。依赖注入的简单总结:依赖:指创建依赖于容器的Bean对象。注入:指Bean对象所依赖的资源,由容器设置和组装。注入方式主要有Setter注入(重点)、构造函数注入和参数直接注入。还有注入的扩展方法,即:p命名空间注入和c命名空间注入,这里不再介绍,有兴趣的同学可以自行研究。Setter注入IoC容器使用setter方法注入依赖实例。通过调用无参构造函数或无参静态工厂方法实例化bean后,调用bean的setter方法(属性的set方法必须在类中有),基于setter的DI代码即可实现如下:util.Set;publicclassStudent{privateStringname;privateAddressaddress;privateString[]books;privateListhobbys;privateMapcard;privateSetgames;privateStringwife;privatePropertiesinfo;publicvoidsetName(Stringname){this.name=name;}publicStringgetName(){returnthis.name;}publicvoidsetAddress(Addressaddress){this.address=address;}publicvoidsetBooks(String[]books){this.books=books;}publicvoidsetHobbys(Listhobbys){this.hobbys=hobbys;}publicvoidsetCard(Mapcard){this.card=card;}publicvoidsetGames(Setgames){this.games=games;}publicvoidsetWife(Stringwife){this.wife=wife;}publicvoidsetInfo(Propertiesinfo){this.info=info;}publicvoidshow(){System.out.println("name="+name+",address="+address.getAddress()+",books=");for(Stringbook:books){System.out.print("<<"+book+">>\t");}System.out.println("\nhobbys:"+hobbys);System.out.println("card:"+card);System.out.println("games:"+games);System.out.println("wife:"+wife);System.out.println("info:"+info);}}配置文件在配置文件中,name赋值为小明,即完成了代码私有String名称的注入测试类publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("bean1.xml");Studentstudent=(Student)context.getBean("student");System.out.println(student.getName());}运行结果,输出:小明常用注入方式的xml配置如下:bean注入使用ref引入其他bean数组注入数学语言英文List注入听歌看电影玩游戏
地图注入设置注入CSgame小小乐空注入属性注入123456男小明测试方法publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("bean1.xml");Studentstudent=(Student)context.getBean("student");student.show();}运行结果,输出:name=Xiaoming,address=Beijing,books=<><><<英语>>爱好:[听音乐,看电影,打游戏]卡:{招行=123456789,工行=987654321}游戏:[CS,游戏,消消乐]wife:nullinfo:{学号=123456,gender=male,name=Xiaoming}构造函数注入是指IoC容器使用构造函数注入依赖实例基于构造函数的DI是通过调用带参数的构造函数来实现的,每个参数代表一个依赖项。代码如下:publicclassStudent2{privateStringname;publicStudent2(Stringname){this.name=name;}publicvoidsetName(Stringname){this.name=name;}publicvoidshow(){System.out.println("name="+name);}}配置文件中的设置测试代码publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("bean3.xml");Student2user=(Student2)c安大略省ext.getBean("student1")user.show();}运行结果:name=kevin1参数直接注入主要是通过注解@Autowired、@Qualifier和@Resource实现@Autowired@Autowired是按类型自动赋值的,不支持id匹配,需要导入spring-aop包配置文件设置代码:publicclassAnimal{@AutowiredprivateCatcat;//运行时spring会通过DI实例化Cat类@AutowiredprivateDogdog;//运行时spring通过DI,会实例化Dog类publicvoidprintCatshot(){cat.shout();}publicvoidprintDogshot(){dog.shout();}}@Qualifier@Autowired根据类型自动组装。如果Spring上下文中存在不同类型的bean,将抛出BeanCreationException;我们可以使用@Qualifier和@Autowired来解决这些问题。代码:@Autowired@Qualifier(value="dog1")privateDogdog1;beans.xml@Resource@Resource是J2EE提供的,需要导入Package:javax.annotation.Resource;如果@Resource有指定的name属性,先在byName方法中根据该属性进行查找组装,然后进行默认的byName组装,不成功会报异常。代码@Resource(name="dog2")privateDogdog;beans.xml最简单的解释IoC主要通过DI技术实现了以下两点:在系统运行过程中,动态地向一个对象提供它所需要的其他对象;在系统运行过程中,动态地从配置文件中读取数据,为对象的属性赋值。【编辑推荐】和姐姐聊Java16的新特性,好甜!IT项目太多,太难管理?不!因为你还没有学会这七招。学习Python五年,这些网站让我认识了最近。快来一起体验吧。Java都到了16了,你怎么还在用8?是不是越来越糟了?太奇妙了!Windows10的这些黑科技功能你都用过吗?