本教程源码请访问:tutorial_demojava
依赖注入:Dependency Injection ,指容器负责建立和维护对象之间的依赖关系,而不是经过对象自己负责本身的建立和解决本身的依赖。在当前类须要用到其余类的对象,由Spring为咱们提供,咱们只须要在配置中说明。git
在Idea中新建Maven工程;github
工程建立完成后添加相应的坐标。spring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.codeaction</groupId> <artifactId>di</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> </dependencies> </project>
package org.codeaction.bean; import java.util.Date; /** * 用户类 */ public class User1 { private String name; private Integer age; private String address; private Date birthday; //有参构造方法 public User(String name, Integer age, String address, Date birthday) { this.name = name; this.age = age; this.address = address; this.birthday = birthday; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", birthday=" + birthday + '}'; } }
User1类中必定要有有参的构造方法。apache
XML文件在resource目录下。数组
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="now" class="java.util.Date"></bean> <bean id="user1" class="org.codeaction.bean.User1"> <!-- 经过构造函数注入属性 --> <constructor-arg name="name" value="Tom"></constructor-arg> <constructor-arg name="age" value="10"></constructor-arg> <constructor-arg name="address" value="上海"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> </beans>
构造函数注入使用constructor-arg标签,这个标签在bean标签的内部。maven
constructor-arg标签中的属性:ide
前三个属性用于指定给构造函数中哪一个参数赋值,其中name属性最经常使用。函数
package org.codeaction.test; import org.codeaction.bean.User1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User1 user1 = (User1) context.getBean("user1"); System.out.println(user1); } }
运行main方法,控制台输出以下:测试
User1{name='Tom', age=10, address='上海', birthday=Mon May 25 09:01:58 CST 2020}
package org.codeaction.bean; import java.util.Date; public class User2 { private String name; private Integer age; private String address; private Date birthday; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setAddress(String address) { this.address = address; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User2{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", birthday=" + birthday + '}'; } }
User2类中必定要有set方法。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="now" class="java.util.Date"></bean> <bean id="user1" class="org.codeaction.bean.User1"> <!-- 经过构造函数注入属性 --> <constructor-arg name="name" value="Tom"></constructor-arg> <constructor-arg name="age" value="10"></constructor-arg> <constructor-arg name="address" value="上海"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <bean id="user2" class="org.codeaction.bean.User2"> <!-- 经过set方法注入 --> <property name="name" value="Bob"></property> <property name="age" value="20"></property> <property name="address" value="北京"></property> <property name="birthday" ref="now"></property> </bean> </beans>
package org.codeaction.test; import org.codeaction.bean.User1; import org.codeaction.bean.User2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //User1 user1 = (User1) context.getBean("user1"); //System.out.println(user1); User2 user2 = (User2) context.getBean("user2"); System.out.println(user2); } }
运行main方法,控制台输出以下:
User2{name='Bob', age=20, address='北京', birthday=Mon May 25 09:37:17 CST 2020}
本质上仍是给属性注入值,只是被注入的属性都是复杂类型(数组,List,Set,Map,Properties)。
package org.codeaction.bean; import java.util.*; public class User3 { private String[] hobbies1; private List<String> hobbies2; private Set<String> hobbies3; private Map<String, String> idInfo1; private Properties idInfo2; public void setHobbies1(String[] hobbies1) { this.hobbies1 = hobbies1; } public void setHobbies2(List<String> hobbies2) { this.hobbies2 = hobbies2; } public void setHobbies3(Set<String> hobbies3) { this.hobbies3 = hobbies3; } public void setIdInfo1(Map<String, String> idInfo1) { this.idInfo1 = idInfo1; } public void setIdInfo2(Properties idInfo2) { this.idInfo2 = idInfo2; } public void show() { System.out.println("array----" + Arrays.toString(hobbies1)); System.out.println("list----" + hobbies2); System.out.println("set----" + hobbies3); System.out.println("map----" + idInfo1); System.out.println("properties----" + idInfo2); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="now" class="java.util.Date"></bean> <bean id="user1" class="org.codeaction.bean.User1"> <!-- 经过构造函数注入属性 --> <constructor-arg name="name" value="Tom"></constructor-arg> <constructor-arg name="age" value="10"></constructor-arg> <constructor-arg name="address" value="上海"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <bean id="user2" class="org.codeaction.bean.User2"> <!-- 经过set方法注入 --> <property name="name" value="Bob"></property> <property name="age" value="20"></property> <property name="address" value="北京"></property> <property name="birthday" ref="now"></property> </bean> <!-- 注入复杂类型属性 --> <bean id="user3" class="org.codeaction.bean.User3"> <!-- 注入复杂类型属性 --> <property name="hobbies1"> <array> <value>篮球</value> <value>读书</value> <value>帆船</value> </array> </property> <property name="hobbies2"> <list> <value>太极</value> <value>自由搏击</value> <value>咏春</value> </list> </property> <property name="hobbies3"> <set> <value>旅游</value> <value>蹦极</value> <value>马拉松</value> </set> </property> <property name="idInfo1"> <map> <entry key="name" value="John"></entry> <entry key="age"> <value>20</value> </entry> <entry key="gender" value="男"></entry> </map> </property> <property name="idInfo2"> <props> <prop key="address">广州</prop> <prop key="height">180</prop> </props> </property> </bean> </beans>
用于为List结构注入的标签:list、array、set;
用于为Map结构注入的标签:map、props;
结构相同,标签能够互换。
package org.codeaction.test; import org.codeaction.bean.User1; import org.codeaction.bean.User2; import org.codeaction.bean.User3; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //User1 user1 = (User1) context.getBean("user1"); //System.out.println(user1); //User2 user2 = (User2) context.getBean("user2"); //System.out.println(user2); User3 user3 = (User3)context.getBean("user3"); user3.show(); } }
运行main方法,控制台输出以下:
array----[篮球, 读书, 帆船] list----[太极, 自由搏击, 咏春] set----[旅游, 蹦极, 马拉松] map----{name=John, age=20, gender=男} properties----{height=180, address=广州}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="now" class="java.util.Date"></bean> <bean id="user1" class="org.codeaction.bean.User1"> <!-- 经过构造函数注入属性 --> <constructor-arg name="name" value="Tom"></constructor-arg> <constructor-arg name="age" value="10"></constructor-arg> <constructor-arg name="address" value="上海"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <bean id="user2" class="org.codeaction.bean.User2"> <!-- 经过set方法注入 --> <property name="name" value="Bob"></property> <property name="age" value="20"></property> <property name="address" value="北京"></property> <property name="birthday" ref="now"></property> </bean> <!-- 注入复杂类型属性 --> <bean id="user3" class="org.codeaction.bean.User3"> <!-- 注入复杂类型属性 --> <property name="hobbies1"> <set> <value>旅游</value> <value>蹦极</value> <value>马拉松</value> </set> </property> <property name="hobbies2"> <array> <value>篮球</value> <value>读书</value> <value>帆船</value> </array> </property> <property name="hobbies3"> <list> <value>太极</value> <value>自由搏击</value> <value>咏春</value> </list> </property> <property name="idInfo1"> <props> <prop key="address">广州</prop> <prop key="height">180</prop> </props> </property> <property name="idInfo2"> <map> <entry key="name" value="John"></entry> <entry key="age"> <value>20</value> </entry> <entry key="gender" value="男"></entry> </map> </property> </bean> </beans>
运行main方法,控制台输出以下:
array----[旅游, 蹦极, 马拉松] list----[篮球, 读书, 帆船] set----[太极, 自由搏击, 咏春] map----{height=180, address=广州} properties----{age=20, name=John, gender=男}
经过上面的修改,说明标签结构相同,标签能够互换。