• spring框架 构造方法注入


    在resources里面新建applicationContext.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-4.3.xsd">
    <!--构造方法注入-->
    <bean id="book1" class="com.ssz.entity.Book">
    <constructor-arg name="name" value="张三三"/>
    </bean>
    <bean id="book2" class="com.ssz.entity.Book">
    <constructor-arg name="id" value="50"/>
    <constructor-arg name="name" value="李思思"/>
    </bean>
    <!--setter方法注入-->
    <bean id="book3" class="com.ssz.entity.Book" scope="prototype">
    <property name="id" value="100"/>
    <property name="name" value="王五五"/>
    <property name="price" value="30"/>
    </bean>
    <!--setter方法注入+ 集合setter方法注入-->
    <bean id="book4" class="com.ssz.entity.Book" scope="prototype">
    <property name="id" value="200"/>
    <property name="name" value="赵六六"/>
    <property name="price" value="40.08"/>
    <property name="address">
    <array>
    <value>郑州市</value>
    <value>开封市</value>
    <value>南阳市</value>
    <value>洛阳市</value>
    </array>
    </property>
    </bean>

    <!--User类-->
    <bean id="user" class="com.ssz.entity.User" destroy-method="close" init-method="init" >
    <property name="id" value="001"/>
    <property name="name" value="王九九"/>
    <property name="age" value="26"/>
    <property name="address" value="河南省郑州市"/>
    </bean>

    <bean id="user2" class="com.ssz.entity.User">
    <property name="id" value="002"/>
    <property name="name" value="孙十十"/>
    <property name="age" value="33"/>
    <property name="address" value="河南省开封市"/>
    </bean>

    <!--setter方法注入+ array注入-->
    <bean id="book5" class="com.ssz.entity.Book" scope="prototype">
    <property name="id" value="300"/>
    <property name="name" value="孙七七"/>
    <property name="price" value="60"/>
    <property name="address">
    <array>
    <value>郑州市</value>
    <value>开封市</value>
    <value>南阳市</value>
    <value>洛阳市</value>
    </array>
    </property>
    </bean>

    <!--setter方法注入+ array注入 + list + map + set + Properties 集合-->
    <bean id="book6" class="com.ssz.entity.Book">
    <property name="id" value="400"/>
    <property name="name" value="钱八八"/>
    <property name="price" value="70"/>
    <property name="address">
    <!--array-->
    <array>
    <value>郑州市</value>
    <value>开封市</value>
    <value>南阳市</value>
    <value>洛阳市</value>
    </array>
    </property>

    <!--list-->
    <property name="list">
    <list>
    <value>10</value>
    <value>100</value>
    <value>1000</value>
    </list>
    </property>
    <property name="user" ref="user2">
    </property>

    <!--map-->
    <property name="map">
    <map>
    <entry key="u1" value-ref="user"/>
    <entry key="u2" value-ref="user"/>
    </map>
    </property>

    <!--set-->
    <property name="set">
    <set>
    <value>java</value>
    <value>C语言</value>
    <value>C++</value>
    <value>PHP</value>
    </set>
    </property>

    <!--Properties 集合-->
    <property name="pro">
    <props>
    <prop key="k1">pt1</prop>
    <prop key="k2">pt1</prop>
    <prop key="k3">pt1</prop>
    <prop key="k4">pt1</prop>
    </props>
    </property>
    </bean>

    </beans>

    lombok依赖

    <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.16.18</version>
         <scope>provided</scope>
    </dependency>

    创建测试类

    package com.ssz;

    import com.ssz.entity.Book;
    import org.junit.Test;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import java.util.List;

    public class Demo {
    @Test
    public void tt(){
    BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");//建立bean工厂
    //Book book1=bf.getBean("book2",Book.class);
    // Book book1=bf.getBean(Book.class);
    //Book book2=bf.getBean("book3",Book.class);
    //Book book2=bf.getBean(Book.class);
    // Book b=bf.getBean("book",Book.class);
    // book.setId(100);
    // book.setName("C语言入门");
    //System.out.println(book1.getPrice());
    //System.out.println(book2);
    //System.out.println(book1==book2);
    // System.out.println(book1);
    // Book bk1=bf.getBean("book4",Book.class);
    // Book bk2=bf.getBean("book4",Book.class);
    // Book bk3=bf.getBean("book4",Book.class);
    // System.out.println(bk1==bk2);
    // System.out.println(bk1==bk3);
    // System.out.println(bk2==bk3);
    // Book book1=bf.getBean("book6",Book.class);
    // System.out.println(book1);
    // for (String addr:book1.getAddress()){
    // System.out.println(addr);
    // }
    Book book=bf.getBean("book5",Book.class);
    System.out.println(book);
    }
    @Test
    public void bb(){
    // BeanFactory bf1=new ClassPathXmlApplicationContext("applicationContext.xml");
    ClassPathXmlApplicationContext bf1=new ClassPathXmlApplicationContext("applicationContext.xml");
    Book bk=bf1.getBean("book6",Book.class);
    System.out.println(bk.getMap());
    System.out.println(bk.getPro().getProperty("k1"));
    for (Integer i: bk.getList()){
    System.out.println(i);
    }
    bf1.close();
    }
    }
  • 相关阅读:
    请求的详细资料级别没有事实表
    BIEE汇总数据如何放在后面
    Biee仪表盘中提示空值如何去掉
    UFT测试本地应用程序登陆小实例(描述性编程)
    Mysql找回丢失密码
    linux下Mysql多实例实现
    如何从零安装Mysql
    Linux系统下yum源配置(Centos 6)
    Linux系统管理常用命令用法总结(2)
    Linux系统管理常用命令用法总结(1)
  • 原文地址:https://www.cnblogs.com/fqszywz/p/7498052.html
Copyright © 2020-2023  润新知