• DI依赖注入-P8,P9,P10,P11


    1.构造器注入

    之前写过了~~~~

    2.Set方式注入【重点】

    3.拓展方式注入


    2.Set方式注入【重点】 

    【环境搭建】

    1.复杂类型

    2.真实测试对象

    四个文件


    Student实体类的创建:

     主要是依据官方文档来建立。那个Address也是为了测试不同的类型,而创建的引入类。主体是这个Student!!!

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

    Address实体类的创建:

    1. package com.Li.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. }

    beans.xml(最基础的)

    1. "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="student" class="com.Li.pojo.Student">
    7. <property name="name" value="李"/>
    8. bean>
    9. beans>

    MyTest

    1. import com.Li.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. }

    这些配置都没什么好说的,之前都配置过,学过。
    测试

     输出配置注入的名字


    八种注入方式:

    1. "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="com.Li.pojo.Address"/>
    7. <bean id="student" class="com.Li.pojo.Student">
    8. <property name="name" value="李"/>
    9. <property name="address" ref="address"/>
    10. <property name="books">
    11. <array>
    12. <value>红楼梦value>
    13. <value>水浒传value>
    14. <value>三国演义value>
    15. <value>西游记value>
    16. array>
    17. property>
    18. <property name="hobbys">
    19. <list>
    20. <value>听歌value>
    21. <value>敲代码value>
    22. <value>打球value>
    23. list>
    24. property>
    25. <property name="card">
    26. <map>
    27. <entry key="身份证" value="0000000"/>
    28. <entry key="银行卡" value="1111111"/>
    29. map>
    30. property>
    31. <property name="games">
    32. <set>
    33. <value>LOLvalue>
    34. <value>COCvalue>
    35. <value>BOBvalue>
    36. set>
    37. property>
    38. <property name="wife">
    39. <null>null>
    40. property>
    41. <property name="info">
    42. <props>
    43. <prop key="driver">20220000prop>
    44. <prop key="url">xxxprop>
    45. <prop key="password">1111prop>
    46. <prop key="username">rootprop>
    47. props>
    48. property>
    49. bean>
    50. beans>

    测试一下。

    发现address有问题,所以给Address类里面增加一个toString方法

    测试修改之后:

     成功!(因为再bean里面没有设置adress的值,所以为null)


    拓展方式注入(了解即可)

    User实体类:

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

    userBeans.xml:

    导入xml约束:

    1. xmlns:p="http://www.springframework.org/schema/p"
    2. xmlns:c="http://www.springframework.org/schema/c"

    测试p与c (p对应无参构造,c对应有参构造

     

     测试:

    1. @Test
    2. public void test2(){
    3. ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
    4. User user = context.getBean("user",User.class);//和强转本质一样,只不过换了一种形式
    5. System.out.println(user);
    6. User user2 = context.getBean("user2",User.class);
    7. System.out.println(user2);
    8. }


    Bean的作用域

     六种作用域:

     默认单例:singleton(每一次创建的都是一个相同的bean对象)


    prototype:(原型模式,每一次都是不同的bean对象)

     

     测试之后不相等了。

  • 相关阅读:
    Searching for MobileNetV3翻译
    51单片机直流电机PID速度控制正反转控制(红外光电测速)LCD1602 L298N
    js ai 天花板 TensorFlow.js
    使用Python对数据的操作转换
    GB28181学习(十三)——订阅与通知
    一、stable diffusion的发展史
    SpringBoot+Redis+Lua
    解析分布式系统的缓存设计
    bat批处理---实现输入指定拷贝文件
    springboot中使用poi导出excel文件,下载简易模板(根据对象实体类生成表头)
  • 原文地址:https://blog.csdn.net/m0_54842832/article/details/127943710