概念

  • 依赖注入(Dependency Injection,DI)
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配

    1. 构造器注入

    我们在之前的案例已经讲过了

    2. Set方式注入【重点】

    【环境搭建】
  1. 复杂类型属性
  2. 真实测试对象

要求被注入的属性,必须有set方法,set方法的方法名有set +属性首字母大写,如果属性是boolean类型,没有set方法,是is

1)pojo包

  1. public class Address {
  2. private String address;
  3. public String getAddress() {
  4. return address;
  5. }
  6. public void setAddress(String address) {
  7. this.address = address;
  8. }
  9. }
  1. package cn.edu.jxust.pojo;
  2. import java.util.*;
  3. public class Student {
  4. private String name;
  5. private Address address;
  6. private String[] books;
  7. private List<String> hobbys;
  8. private Map<String,String> card;
  9. private Set<String> game;
  10. private String wife;
  11. private Properties info;
  12. @Override
  13. public String toString() {
  14. return "Student{" +
  15. "name='" + name + '\'' +
  16. ", address=" + address +
  17. ", books=" + Arrays.toString(books) +
  18. ", hobbys=" + hobbys +
  19. ", card=" + card +
  20. ", game=" + game +
  21. ", wife='" + wife + '\'' +
  22. ", info=" + info +
  23. '}';
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. }

2)beans.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- 第一种,普通值注入,value-->
  7. <bean id="student" class="cn.edu.jxust.pojo.Student">
  8. <property name="name" value="浔阳江头月送客,枫叶荻花秋瑟瑟" />
  9. </bean>
  10. </beans>

3)MyTest测试

  1. import cn.edu.jxust.pojo.Student;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  7. Student student = (Student) context.getBean("student");
  8. System.out.println(student.getName());
  9. }
  10. }

3. 拓展方式注入

常量注入、Bean注入、数组、List、Map、Set注入、Null注入、properties注入、p命名和c命名

1)beans.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="address" class="cn.edu.jxust.pojo.Address" />
  7. <!-- 第一种,普通值注入,value-->
  8. <bean id="student" class="cn.edu.jxust.pojo.Student">
  9. <property name="name" value="浔阳江头月送客,枫叶荻花秋瑟瑟" />
  10. <!-- 第二种,Bean注入,ref-->
  11. <property name="address" ref="address" />
  12. <!-- 数组-->
  13. <property name="books">
  14. <array>
  15. <value>红楼梦</value>
  16. <value>西游记</value>
  17. <value>三国</value>
  18. <value>三国演义</value>
  19. </array>
  20. </property>
  21. <!-- List-->
  22. <property name="hobbys">
  23. <list>
  24. <value>听歌</value>
  25. <value>敲代码</value>
  26. <value>看电影</value>
  27. </list>
  28. </property>
  29. <!-- Map-->
  30. <property name="card">
  31. <map>
  32. <entry key="身份证" value="361127200000000000"/>
  33. <entry key="银行卡" value="555555555555555555" />
  34. </map>
  35. </property>
  36. <!-- set注入-->
  37. <property name="game">
  38. <set>
  39. <value>LOL</value>
  40. <value>BOB</value>
  41. <value>COC</value>
  42. </set>
  43. </property>
  44. <!-- null注入-->
  45. <property name="wife">
  46. <null />
  47. </property>
  48. <property name="info">
  49. <props>
  50. <prop key="学号">57200181636</prop>
  51. <prop key="性别"></prop>
  52. </props>
  53. </property>
  54. </bean>
  55. </beans>

2)pojo包

  1. package cn.edu.jxust.pojo;
  2. public class Address {
  3. private String address;
  4. public String getAddress() {
  5. return address;
  6. }
  7. public void setAddress(String address) {
  8. this.address = address;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Address{" +
  13. "address='" + address + '\'' +
  14. '}';
  15. }
  16. }
  1. package cn.edu.jxust.pojo;
  2. import java.util.*;
  3. public class Student {
  4. private String name;
  5. private Address address;
  6. private String[] books;
  7. private List<String> hobbys;
  8. private Map<String,String> card;
  9. private Set<String> game;
  10. private String wife;
  11. private Properties info;
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public Address getAddress() {
  19. return address;
  20. }
  21. public void setAddress(Address address) {
  22. this.address = address;
  23. }
  24. public String[] getBooks() {
  25. return books;
  26. }
  27. public void setBooks(String[] books) {
  28. this.books = books;
  29. }
  30. public List<String> getHobbys() {
  31. return hobbys;
  32. }
  33. public void setHobbys(List<String> hobbys) {
  34. this.hobbys = hobbys;
  35. }
  36. public Map<String, String> getCard() {
  37. return card;
  38. }
  39. public void setCard(Map<String, String> card) {
  40. this.card = card;
  41. }
  42. public Set<String> getGame() {
  43. return game;
  44. }
  45. public void setGame(Set<String> game) {
  46. this.game = game;
  47. }
  48. public String getWife() {
  49. return wife;
  50. }
  51. public void setWife(String wife) {
  52. this.wife = wife;
  53. }
  54. public Properties getInfo() {
  55. return info;
  56. }
  57. public void setInfo(Properties info) {
  58. this.info = info;
  59. }
  60. @Override
  61. public String toString() {
  62. return "Student{" +
  63. "name='" + name + '\'' +
  64. ", address=" + address.toString() +
  65. ", books=" + Arrays.toString(books) +
  66. ", hobbys=" + hobbys +
  67. ", card=" + card +
  68. ", game=" + game +
  69. ", wife='" + wife + '\'' +
  70. ", info=" + info +
  71. '}';
  72. }
  73. }

3)测试

  1. import cn.edu.jxust.pojo.Student;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  7. Student student = (Student) context.getBean("student");
  8. System.out.println(student.toString());
  9. /*
  10. Student{
  11. name='浔阳江头月送客,枫叶荻花秋瑟瑟',
  12. address=Address{address='null'},
  13. books=[红楼梦, 西游记, 三国, 三国演义],
  14. hobbys=[听歌, 敲代码, 看电影],
  15. card={
  16. 身份证=361127200000000000,
  17. 银行卡=555555555555555555
  18. },
  19. game=[LOL, BOB, COC],
  20. wife='null',
  21. info={学号=57200181636,
  22. 性别=男}
  23. }
  24. */
  25. }
  26. }

4)p命名和c命名注入

4.1 p命名(运用无参)

P命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:p=”http://www.springframework.org/schema/p”

User类
  1. package cn.edu.jxust.pojo;
  2. public class User {
  3. private String name;
  4. private int age;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. public void setAge(int age) {
  15. this.age = age;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User{" +
  20. "name='" + name + '\'' +
  21. ", age=" + age +
  22. '}';
  23. }
  24. }

userbeans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
  8. <bean id="user" class="cn.edu.jxust.pojo.User" p:name="杨辉" p:age="18" />
  9. </beans>

4.2 c命名(运用有参)

c 命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:c=”http://www.springframework.org/schema/c”

User类
  1. package cn.edu.jxust.pojo;
  2. public class User {
  3. private String name;
  4. private int age;
  5. public User(){
  6. }
  7. //与p不同,多了有参构造函数
  8. public User(String name, int age){
  9. this.name = name;
  10. this.age = age;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. @Override
  25. public String toString() {
  26. return "User{" +
  27. "name='" + name + '\'' +
  28. ", age=" + age +
  29. '}';
  30. }
  31. }

userbeans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd">
  8. <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
  9. <bean id="user" class="cn.edu.jxust.pojo.User" p:name="杨辉" p:age="18" />
  10. <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
  11. <bean id="user2" class="cn.edu.jxust.pojo.User" c:name="杨杨" c:age="19" />
  12. </beans>

4.3 MyTest测试

  1. import cn.edu.jxust.pojo.Student;
  2. import cn.edu.jxust.pojo.User;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class MyTest {
  7. @Test
  8. public void Test(){
  9. ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
  10. //p命名操作
  11. User user = (User) context.getBean("user");
  12. //c命名操作
  13. User user = (User) context.getBean("user2");
  14. System.out.println(user.toString());
  15. }
  16. }

5)Bean的作用域

image.png

5.1 单例模式(spring默认机制)

  1. <bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">

5.2 原型模式

每次从容器中get的时候,都会产生一个新对象!

  1. <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"

5.3 其余的request、session、application,这些个只能在web开发中使用到!