Spring依赖注入(dependency injection) 短命女 2022-06-11 09:27 118阅读 0赞 Spring的依赖注入包括两方面: 1、依赖:指bean对象创建依赖于容器;Bean对象的依赖资源。 2、注入:指bean对象依赖的资源由容器来设置和装配。 Spring的注入有两类: 1、构造器的注入:使用的是constructor-arg,如图: ![Center][] 2、setter注入: 要求被注入的属性,必须有set方法,set方法生成的方法名,由set+属性(首字母大写),如果属性是boolean类型,没有get方法,是is方法。 1)常量注入: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> </bean> </beans> 2)bean的注入: ![Center 1][] 给Address配置值之后如图: ![Center 2][] Address类代码: package com.myspring.vo; public class Address { private String address ; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } Student类代码: package com.myspring.vo; public class Student { private String name; private Address addr ; public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); } } 3)数组注入: 在Student类中加入字符串数组,代码如: package com.myspring.vo; public class Student { private String name; private Address addr ; private String[] books ; public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i]+" "); } System.out.println(); } } beans.xml代码增加了输入注入,如图: ![Center 3][] beans.xml代码: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> </bean> </beans> 测试代码Test: package com.myspring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myspring.vo.Student; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ; Student student = (Student) ac.getBean("student") ; student.show() ; } } 运行后控制台打印信息: ![Center 4][] 4)List注入: 在Student实体类中增加集合属性hobbies,代码如: package com.myspring.vo; import java.util.List; public class Student { private String name; private Address addr ; private String[] books ; private List<String> hobbies ; public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i]+" "); } System.out.println(); System.out.println("hobbies = "+ hobbies); } } 在beans.xml中配置如下: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <!-- List注入 --> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>足球</value> <value>台球</value> </list> </property> </bean> </beans> 运行test测试类代码: package com.myspring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myspring.vo.Student; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ; Student student = (Student) ac.getBean("student") ; student.show() ; } } 控制台打印如下: ![Center 5][] 5)map注入: 实体类Student中增加map属性cards,代码如: package com.myspring.vo; import java.util.List; import java.util.Map; public class Student { private String name; private Address addr ; private String[] books ; private List<String> hobbies ; private Map<String , String> cards ; public void setCards(Map<String, String> cards) { this.cards = cards; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i]+" "); } System.out.println(); System.out.println("hobbies = "+ hobbies); System.out.println("cards = "+ cards); } } beans.xml中的配置文件如下: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <!-- List注入 --> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>足球</value> <value>台球</value> </list> </property> <!-- map注入 --> <property name="cards"> <map> <entry key="中国银行" value="11010102020202002" /> <entry> <key><value>建设银行</value></key> <value>20200300039399292992</value> </entry> </map> </property> </bean> </beans> 运行test类,代码同上面的test,控制台打印信息如下: ![Center 6][] 6)Set集合注入: 实体类Student代码如下: package com.myspring.vo; import java.util.List; import java.util.Map; import java.util.Set; public class Student { private String name; private Address addr ; private String[] books ; private List<String> hobbies ; private Map<String , String> cards ; private Set<String> games ; public void setGames(Set<String> games) { this.games = games; } public void setCards(Map<String, String> cards) { this.cards = cards; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i]+" "); } System.out.println(); System.out.println("hobbies = "+ hobbies); System.out.println("cards = "+ cards); System.out.println("games = " + games); } } beans.xml代码如下: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <!-- List注入 --> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>足球</value> <value>台球</value> </list> </property> <!-- map注入 --> <property name="cards"> <map> <entry key="中国银行" value="11010102020202002" /> <entry> <key><value>建设银行</value></key> <value>20200300039399292992</value> </entry> </map> </property> <!-- Set集合注入 --> <property name="games"> <set> <value>lol</value> <value>dota</value> <value>cs</value> <value>cf</value> </set> </property> </bean> </beans> 测试类代码test同上面,运行后控制台打印信息: ![Center 7][] 7)Null注入: 实体类Student代码: package com.myspring.vo; import java.util.List; import java.util.Map; import java.util.Set; public class Student { private String name; private Address addr ; private String[] books ; private List<String> hobbies ; private Map<String , String> cards ; private Set<String> games ; private String wife ; public void setWife(String wife) { this.wife = wife; } public void setGames(Set<String> games) { this.games = games; } public void setCards(Map<String, String> cards) { this.cards = cards; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name +"---addr="+addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i]+" "); } System.out.println(); System.out.println("hobbies = "+ hobbies); System.out.println("cards = "+ cards); System.out.println("games = " + games); System.out.println("wife = "+ wife ); } } beans.xml代码: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <!-- List注入 --> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>足球</value> <value>台球</value> </list> </property> <!-- map注入 --> <property name="cards"> <map> <entry key="中国银行" value="11010102020202002" /> <entry> <key><value>建设银行</value></key> <value>20200300039399292992</value> </entry> </map> </property> <!-- Set集合注入 --> <property name="games"> <set> <value>lol</value> <value>dota</value> <value>cs</value> <value>cf</value> </set> </property> <!-- Null 注入 --> <property name="wife"> <null></null> </property> </bean> </beans> 运行test测试类,控制台打印信息如下: ![Center 8][] 8)Properties注入 实体类Student代码: package com.myspring.vo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private String name; private Address addr; private String[] books; private List<String> hobbies; private Map<String, String> cards; private Set<String> games; private String wife; private Properties info; public void setInfo(Properties info) { this.info = info; } public void setWife(String wife) { this.wife = wife; } public void setGames(Set<String> games) { this.games = games; } public void setCards(Map<String, String> cards) { this.cards = cards; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void setBooks(String[] books) { this.books = books; } public void show() { System.out.println("name=" + name + "---addr=" + addr.getAddress()); System.out.print("books="); for (int i = 0; i < books.length; i++) { System.out.print(books[i] + " "); } System.out.println(); System.out.println("hobbies = " + hobbies); System.out.println("cards = " + cards); System.out.println("games = " + games); System.out.println("wife = " + wife); System.out.println("info = " + info); } } beans.xml配置代码: <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <bean id="addr" class="com.myspring.vo.Address"> <property name="address" value="北京市长安街1号"></property> </bean> <bean id="student" class="com.myspring.vo.Student"> <!-- 常量注入 --> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"></property> <!-- 注入数组 --> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <!-- List注入 --> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>足球</value> <value>台球</value> </list> </property> <!-- map注入 --> <property name="cards"> <map> <entry key="中国银行" value="11010102020202002" /> <entry> <key><value>建设银行</value></key> <value>20200300039399292992</value> </entry> </map> </property> <!-- Set集合注入 --> <property name="games"> <set> <value>lol</value> <value>dota</value> <value>cs</value> <value>cf</value> </set> </property> <!-- Null 注入 --> <property name="wife"> <null></null> </property> <!-- Properties注入 --> <property name="info"> <props> <prop key="学号">11020039398</prop> <prop key="sex">男</prop> <prop key="name">小明</prop> </props> </property> </bean> </beans> 运行测试类test结果如下: ![Center 9][] 9)、p命名空间注入: 我们从下面的文档截图中可以看到这是p命名空间的头文件,该文档所在路径可以从图中看到, ![Center 10][] p命名空间的头文件部分和普通的头文件部分唯一的不同就是p命名空间的头文件多了xmlns:p="" 这部分,如图: ![Center 11][] 下面来看一下p命名怎么注入: 新建一个实体类User,代码如下: package com.myspring.vo; public class User { private String name; private int age; 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } } beans.xml中的配置代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <!-- p命名空间注入 --> <bean id="user" class="com.myspring.vo.User" p:name="小明" p:age="11"></bean> </beans> 其中name和age是对应的实体类User中的name和age属性 ![Center 12][] 测试类test代码如下: package com.myspring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myspring.vo.Student; import com.myspring.vo.User; public class Test { public static void main(String[] args) { //p命名注入测试 ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ; User user = (User) ac.getBean("user") ; System.out.println(user); } } 控制台打印的运行结果如下: ![Center 13][] 3、c命名空间注入(其实就是构造方法注入) c命名空间注入需要现在beans.xml头文件部分增加 xmlns:c="http://www.springframework.org/schema/c" 实体类User代码如下: package com.myspring.vo; public class User { private String name; private int age; public User() { // TODO Auto-generated constructor stub } public User(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } } beans.xml代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 , 由spring容器创建和管理 --> <!-- c命名空间注入 --> <bean id="c_user" class="com.myspring.vo.User" c:name="小王" c:age="22"></bean> </beans> 测试类Test代码: package com.myspring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myspring.vo.Student; import com.myspring.vo.User; public class Test { public static void main(String[] args) { //p命名注入测试 ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ; User user = (User) ac.getBean("c_user") ; System.out.println(user); } } 控制台打印运行结果如下: ![Center 14][] [Center]: /images/20220611/384bae595d0246df910e2b88c5698119.png [Center 1]: /images/20220611/bcfe1512cea146dcabe9ef53df98e3f2.png [Center 2]: /images/20220611/591de1f38c4345d683af6550e242d022.png [Center 3]: /images/20220611/b6e62698e7fb443894336b9e69731dd1.png [Center 4]: /images/20220611/e43a5ba82132435aa22bb131d9f079ea.png [Center 5]: /images/20220611/9aa25d399cd840c9a9aee74583ebe038.png [Center 6]: /images/20220611/5fbaf8cbdb16475d827f7671e7354c07.png [Center 7]: /images/20220611/f05dd9bced004560835a3818d2f22293.png [Center 8]: /images/20220611/4492084db492416fb78c1ba3060068f6.png [Center 9]: /images/20220611/fa65847babf3492e9f9199ac69d5b6dd.png [Center 10]: /images/20220611/ef8804b8fb5b4499b0d02eb77b8c8e83.png [Center 11]: /images/20220611/920590f48b094e2392385ef0fd570e7c.png [Center 12]: /images/20220611/de2b682407434c35ba675ec164152ce3.png [Center 13]: /images/20220611/9452489265a84a1ba69e8ad63e1e37c8.png [Center 14]: /images/20220611/26a3da063838411da65707d90e8cc173.png
还没有评论,来说两句吧...