很难出错的Spring5(二)—— spring配置文件详解、依赖注入的几种方式 一、Spring配置xml配置文件全解前面已经讲过xml配置文件的一些内容了。再加上下面讲的就全了来看一下Spirng的所有配置无非就下面5种beansbeandescription 描述alias别名import多人开发时多个配置文件合并1.1 alias 别名就是给对象起别名getBean方法用别名获得对象和用对象名结果一样但这个注解实际上没什么用因为它可以被bean name替代1.2 bean 补充讲解!-- id :bean的唯一标识符也就是对象名 class :类的全限定名【包名路径】 name: 给对象起别名的第二种方式可以起多个空格、逗号分号都可以作为分隔符spring超级智能 -- bean idhelloJJ classHello namehi hello;hijj, hellojj property namestr value今天是1/9昨天是我的生日/property /beanid和class没有什么可讲的我们看下起别名。1.3 import 导入import什么时候用呢团队开发的时候一人写一个bean.xml最后合并起来。配置文件结构bean.xml张三写的bean2.xml李四写的bean3.xml王五写的applicationContext.xml把所有人的合并起来在程序里只导入applicationContext.xml就行了我们新建一个applicationContext.xml在IDEA配置上下文的提示里不要重新Create new application context,而是选和之前的“Bean”用同一个上下文因为是同一个项目嘛。在beans里面把团队中所有人写的配置文件import进来beans import resourcebeans.xml/import import resourcebeans2.xml/import import resourcebeans3.xml/import /beans在程序里只需要导入applicationContext.xml就可以了ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);那么有一个问题bean.xml 、bean2.xml和bean3.xml里有的对象重名怎么办我们让bean.xml 、bean2.xml和bean3.xml里对象helloJJ的对象名和别名都一模一样然后在程序中用getBean获取对象发现获取到的是最后import进来的beans.xml定义的对象前面的会被覆盖进一步测试如果bean.xml 、bean2.xml和bean3.xml里对象helloJJ的对象名都相同但是别名不同能否实现通过别名获取不同对象呢不能只能获取最后一个被import进来的同名对象前面的同名对象都被覆盖了。这里很奇怪二、依赖注入的几种方式2.1 构造器注入构造器注入要求必须有有参构造方法否则注入的时候会报错2.1.1 普通注入https://blog.csdn.net/qq_44886213/article/details/128581793也就是之前讲过的有参构造器创建对象2.1.2 C-命名空间注入Core Technologies在配置文件中必须加上这一行导入约束!--Address对象-- bean idaddress classcom.company.cj.Address property nameprovince value陕西/property /bean !--Student对象-- bean idstudent_zxf classcom.company.cj.Student c:name黑黑 c:address-refaddress /bean2.2 setter方法注入2.2.1 普通注入常用另开一个module定义Student类用来进行属性的依赖注入测试。每个属性都必须配有setter方法哦。package com.company.cj; import javafx.beans.property.Property; import java.util.*; /* * 测试多种数据类型的注入 * * */ public class Student { private Address address; //引用数据类型 private String name; //也是引用数据类型java常用类 private int age; //基本数据类型 private String []goals; //数组 private ListString friends; //List private SetString hobbies; //Set private MapString,String importantDates; //Map private String Empty1; //测试空值 private String Empty2; //测试空字符串 private Properties info; //Properties public Address getAddress() { return address; } public void setAddress(Address address) { this.address address; } 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 String[] getGoals() { return goals; } public void setGoals(String[] goals) { this.goals goals; } public ListString getFriends() { return friends; } public void setFriends(ListString friends) { this.friends friends; } public SetString getHobbies() { return hobbies; } public void setHobbies(SetString hobbies) { this.hobbies hobbies; } public MapString, String getImportantDates() { return importantDates; } public void setImportantDates(MapString, String importantDates) { this.importantDates importantDates; } public String getEmpty1() { return Empty1; } public void setEmpty1(String empty1) { Empty1 empty1; } public String getEmpty2() { return Empty2; } public void setEmpty2(String empty2) { Empty2 empty2; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info info; } Override public String toString() { return Student{ address address.getProvince() , name name \ , age age , goals Arrays.toString(goals) , friends friends , hobbies hobbies , importantDates importantDates , Empty1 Empty1 \ , Empty2 Empty2 \ , info info }; } }再定义一个Address类作为Student的一个引用属性package com.company.cj; public class Address { private String province; public String getProvince() { return province; } public void setProvince(String province) { this.province province; } }配置文件进行setter注入?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd !--Address对象-- bean idaddress classcom.company.cj.Address property nameprovince value陕西/property /bean bean idstudent_zxf classcom.company.cj.Student !--引用类型的注入-- property nameaddress refaddress/property !--String类型的注入-- property namename valuezxf/property !--int类型的注入-- property nameage value27/property !--数组的注入-- property namegoals array valuea better relationship/value valuefar far from covid-19/value valueimprovement at everywhere/value /array /property !--List的注入-- property namefriends list value杨金水/value value吕芳/value /list /property !--Set的注入-- property namehobbies set valueswim/value valuesing/value /set /property !--Map的注入-- property nameimportantDates map entry key6/5 valuego to xian/entry entry key10/28 valuego to bit/entry /map /property !--空值的注入-- property nameempty1 null/ /property !--空字符串的注入-- property nameempty2 value/property !--Properties的注入-- property nameinfo props prop key姓名zxf/prop prop key职业演员/prop prop key电话16244558866/prop /props /property /bean /beans检查是否注入成功import com.company.cj.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml); Student student_zxf (Student) context.getBean(student_zxf); System.out.println(student_zxf.toString()); /** 输出 * Student{ * address陕西, * namezxf, * age27, * goals[a better relationship, far far from covid-19, improvement at everywhere], * friends[杨金水, 吕芳], * hobbies[swim, sing], * importantDates{ * 6/5go to xian, * 10/28go to bit * }, * Empty1null, * Empty2, * info{ * 电话16244558866, * 姓名zxf, * 职业演员 * } * } */ } }2.2.1 P-命名空间注入P-命名空间注入其实也是调用的setter方法。Core Technologies要使用P-命名空间注入就得导入一行约束去上面的链接里面找在bean中输入一个p就会弹出相应的属性P-命名空间方式注入!--Address对象-- bean idaddress classcom.company.cj.Address property nameprovince value陕西/property /bean !--Student对象-- bean idstudent_zxf classcom.company.cj.Student p:namezxf p:address-refaddress /bean