SpringDI

作者: _FireFly_ | 来源:发表于2020-11-16 09:24 被阅读0次

    Computer

    package domain;
    
    public class Computer {
    
        private String brand;//电脑品牌   ThinkPad
        private String name;//电脑型号    S5
        private Float price;//电脑价格    8000
    
        public Computer() {
        }
    
        public Computer(String brand, String name, Float price) {
            this.brand = brand;
            this.name = name;
            this.price = price;
        }
    
    
    //    @Override
    //    public String toString() {
    //        return "Computer{" +
    //                "brand='" + brand + '\'' +
    //                ", name='" + name + '\'' +
    //                ", price=" + price +
    //                '}';
    //    }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setPrice(Float price) {
            this.price = price;
        }
    
        public String getBrand() {
            return brand;
        }
        public String getName() {
            return name;
        }
        public Float getPrice() {
            return price;
        }
    }
    

    Room

    public class Room {
    
        private String name;//机房房间号 属性
        private Computer computer;//机房中有一台电脑 属性 (has a) 聚合 组合 包含
    
        public Room() {}
    
        public Room(String name, Computer computer) {
            this.name = name;
            this.computer = computer;
        }
    
        @Override
        public String toString() {
            return "Room{" +
                    "name='" + name + '\'' +
                    ", computer=" + computer +
                    '}';
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Computer getComputer() {
            return computer;
        }
        public void setComputer(Computer computer) {
            this.computer = computer;
        }
    }
    

    Student

    package domain;
    
    public class Student {
    
        private Integer sid;
        private String sname;
        private String ssex;
        private Integer sage;
    
        public Student() {
            System.out.println("Student无参数构造方法");
        }
    
        public Student(Integer sid, String sname, String ssex, Integer sage) {
            System.out.println("Student带参数构造方法");
            this.sid = sid;
            this.sname = sname;
            this.ssex = ssex;
            this.sage = sage;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "sid=" + sid +
                    ", sname='" + sname + '\'' +
                    ", ssex='" + ssex + '\'' +
                    ", sage=" + sage +
                    '}';
        }
    
        public Integer getSid() {
            return sid;
        }
    
        public void setSid(Integer sid) {
            System.out.println("setSid方法执行啦");
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public String getSsex() {
            return ssex;
        }
    
        public void setSsex(String ssex) {
            this.ssex = ssex;
        }
    
        public Integer getSage() {
            return sage;
        }
    
        public void setSage(Integer sage) {
            this.sage = sage;
        }
    }
    

    TestBean

    package domain;
    
    import java.util.Map;
    
    public class TestBean {
    
        private String name;
        private Computer computer;
        private Map<String,Computer> map;//内部类  Entry(key,value)
        //private List<Map<String,String>> list;
    
        public TestBean() {
        }
    
        public TestBean(String name, Computer computer, Map<String, Computer> map) {
            this.name = name;
            this.computer = computer;
            this.map = map;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Computer getComputer() {
            return computer;
        }
    
        public void setComputer(Computer computer) {
            this.computer = computer;
        }
    
        public Map<String, Computer> getMap() {
            return map;
        }
    
        public void setMap(Map<String, Computer> map) {
            this.map = map;
        }
    }
    

    TestProperties

    /**
     * 这个类对象是我要管理的bean
     * bean中有一个properties属性
     */
    public class TestProperties {
    
        private Properties properties;
    
        public TestProperties(Properties properties) {
            this.properties = properties;
        }
        public TestProperties() {
        }
        public Properties getProperties() {
            return properties;
        }
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    }
    

    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"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    
        <!--通过属性对应的set方法做自动DI-->
        <!--<bean name="student" class="domain.Student">-->
            <!--<property name="sid" value="1"></property>-->
            <!--<property name="sname">-->
                <!--<value type="java.lang.String">zzt</value>-->
            <!--</property>-->
            <!--<property name="ssex" value="18"></property>-->
            <!--<property name="sage" value="18"></property>-->
        <!--</bean>-->
    
        <!--通过带参数的构造方法 给属性赋值-->
        <!--<bean name="student" class="domain.Student">-->
            <!--<constructor-arg name="sid" value="1" type="java.lang.Integer"></constructor-arg>-->
            <!--<constructor-arg name="sname" value="1" type="java.lang.String"></constructor-arg>-->
            <!--<constructor-arg name="ssex" type="java.lang.String">-->
                <!--<value>男</value>-->
            <!--</constructor-arg>-->
            <!--<constructor-arg name="sage" value="18" ></constructor-arg>-->
        <!--</bean>-->
    
        <!--==================================================================-->
    
        <!--以下讲了一些特殊情况 比如null 比如特殊符号 等-->
        <!--<bean name="student" class="domain.Student">-->
            <!--<constructor-arg name="sid" value="1"></constructor-arg>-->
            <!--<constructor-arg name="sname">-->
                <!--<value><![CDATA[<zzt~>]]></value>-->
            <!--</constructor-arg>-->
            <!--<constructor-arg name="ssex" value="男"></constructor-arg>-->
            <!--<constructor-arg name="sage" value="18"></constructor-arg>-->
        <!--</bean>-->
    
        <!--<bean name="student" class="domain.Student">-->
            <!--<property name="sid" value="1"></property>-->
            <!--<property name="sname">-->
                <!--<value><![CDATA[]]></value>-->
            <!--</property>-->
        <!--</bean>-->
    
        <!--=====================================================================-->
    
        <!--<bean name="room" class="domain.Room">-->
            <!--<property name="name" value="001"></property>-->
            <!--<property name="computer"><bean name="computer" class="domain.Computer"></bean></property>-->
            <!--<property name="computer.brand" value="联想"></property>-->
            <!--<property name="computer.name" value="S5"></property>-->
            <!--<property name="computer.price" value="7000"></property>-->
        <!--</bean>-->
    
        <!--===============================================================================-->
    
        <!--<bean name="controller" class="controller.StudentController" autowire="constructor"></bean>-->
        <!--<bean name="service" class="service.StudentService" autowire="constructor"></bean>-->
        <!--<bean name="dao" class="dao.StudentDao"></bean>-->
    
        <!-- 某一个bean中的属性名 对应 另一个bean的name或者id名 -->
        <!--<bean name="dao" class="dao.StudentDao"></bean>-->
        <!--<bean name="service" class="service.StudentService" autowire="byType"></bean>-->
        <!--<bean name="controller" class="controller.StudentController" autowire="byType"></bean>-->
    
    
        <!--接口的bean属性-->
        <!--<bean name="ti" class="testinterface.ImplA"></bean>-->
        <!--<bean name="tiB" class="testinterface.ImplB"></bean>-->
        <!--<bean name="testService" class="testinterface.TestService" autowire="byName"></bean>-->
    
    
        <!-- ====================================================  -->
        <!--对象中存储数组类型-->
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<property name="name" value="zzt"></property>-->
            <!--<property name="age" value="18"></property>-->
            <!--<property name="computer">-->
                <!--<bean class="domain.Computer"></bean>-->
            <!--</property>-->
            <!--<property name="array">-->
                <!--<array value-type="java.lang.String">-->
                    <!--<value>aaa</value>-->
                    <!--<value>bbb</value>-->
                    <!--<value>ccc</value>-->
                    <!--<value>ddd</value>-->
                <!--</array>-->
            <!--</property>-->
        <!--</bean>-->
    
        <!--<bean id="c1" class="domain.Computer"></bean>-->
        <!--<bean id="c2" class="domain.Computer"></bean>-->
        <!--<bean id="c3" class="domain.Computer"></bean>-->
        <!--<bean id="c4" class="domain.Computer"></bean>-->
    
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<property name="name" value="zzt"></property>-->
            <!--<property name="age" value="18"></property>-->
            <!--<property name="computers">-->
                <!--<array value-type="domain.Computer">-->
                    <!--<ref bean="c1"></ref>-->
                    <!--<ref bean="c2"></ref>-->
                    <!--<ref bean="c3"></ref>-->
                    <!--<ref bean="c4"></ref>-->
                <!--</array>-->
            <!--</property>-->
        <!--</bean>-->
    
    
        <!--对象中存储一个list集合-->
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<constructor-arg name="name" value="zzt" type="java.lang.String"></constructor-arg>-->
            <!--<constructor-arg name="age" value="18" type="java.lang.Integer"></constructor-arg>-->
            <!--<constructor-arg name="list" type="java.util.List">-->
                <!--<list value-type="java.lang.String">-->
                    <!--<value>aaa</value>-->
                    <!--<value>bbb</value>-->
                    <!--<value>ccc</value>-->
                    <!--<value>ddd</value>-->
                <!--</list>-->
            <!--</constructor-arg>-->
        <!--</bean>-->
    
    
        <!--<bean id="c1" class="domain.Computer"></bean>-->
        <!--<bean id="c2" class="domain.Computer"></bean>-->
        <!--<bean id="c3" class="domain.Computer"></bean>-->
        <!--<bean id="c4" class="domain.Computer"></bean>-->
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<constructor-arg name="name" value="zzt"></constructor-arg>-->
            <!--<constructor-arg name="age" value="18"></constructor-arg>-->
            <!--<constructor-arg name="list" type="java.util.List">-->
                <!--<list value-type="domain.Computer">-->
                    <!--<ref bean="c1"></ref>-->
                    <!--<ref bean="c2"></ref>-->
                    <!--<ref bean="c3"></ref>-->
                    <!--<ref bean="c4"></ref>-->
                <!--</list>-->
            <!--</constructor-arg>-->
        <!--</bean>-->
    
    
        <!--存储set集合-->
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<constructor-arg name="name" type="java.lang.String">-->
                <!--<value>zzt</value>-->
            <!--</constructor-arg>-->
            <!--<constructor-arg name="computer" type="domain.Computer">-->
                <!--<bean class="domain.Computer"></bean>-->
            <!--</constructor-arg>-->
            <!--<constructor-arg name="set" type="java.util.Set">-->
                <!--<set value-type="domain.Computer">-->
                    <!--<bean class="domain.Computer"></bean>-->
                    <!--<bean class="domain.Computer"></bean>-->
                    <!--<bean class="domain.Computer"></bean>-->
                    <!--<bean class="domain.Computer"></bean>-->
                <!--</set>-->
            <!--</constructor-arg>-->
        <!--</bean>-->
    
    
        <!--对象中存储一个map集合-->
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<property name="name" value="zzt"></property>-->
            <!--<property name="computer">-->
                <!--<bean class="domain.Computer"></bean>-->
            <!--</property>-->
            <!--<property name="map">-->
                <!--<map key-type="java.lang.Integer" value-type="java.lang.String">-->
                    <!--<entry key="1" value="aaa"></entry>-->
                    <!--<entry key="2" value="bbb"></entry>-->
                    <!--<entry key="3" value="ccc"></entry>-->
                    <!--<entry key="4" value="ddd"></entry>-->
                <!--</map>-->
            <!--</property>-->
        <!--</bean>-->
    
    
        <!--<bean id="c1" class="domain.Computer"></bean>-->
        <!--<bean id="c2" class="domain.Computer"></bean>-->
        <!--<bean id="c3" class="domain.Computer"></bean>-->
        <!--<bean id="c4" class="domain.Computer"></bean>-->
    
        <!--<bean id="testBean" class="domain.TestBean">-->
            <!--<property name="name" value="zzt"></property>-->
            <!--<property name="computer">-->
                <!--<bean class="domain.Computer"></bean>-->
            <!--</property>-->
            <!--<property name="map">-->
                <!--<map key-type="java.lang.String" value-type="domain.Computer">-->
                    <!--<entry key="a" value-ref="c1"></entry>-->
                    <!--<entry key="b" value-ref="c2"></entry>-->
                    <!--<entry key="c" value-ref="c3"></entry>-->
                    <!--<entry key="d" value-ref="c4"></entry>-->
                <!--</map>-->
            <!--</property>-->
        <!--</bean>-->
    
        <!--===============================================================================-->
    
        <!--<bean id="testProperties" class="domain.TestProperties">-->
            <!--<constructor-arg name="properties">-->
                <!--<props>-->
                    <!--<prop key="1">aaa</prop>-->
                    <!--<prop key="2">bbb</prop>-->
                    <!--<prop key="3">ccc</prop>-->
                <!--</props>-->
            <!--</constructor-arg>-->
        <!--</bean>-->
    
        <!--<bean id="testProperties" class="domain.TestProperties">-->
            <!--<property name="properties">-->
                <!--<props>-->
                    <!--<prop key="3">ccc</prop>-->
                    <!--<prop key="2">bbb</prop>-->
                    <!--<prop key="1">aaa</prop>-->
                <!--</props>-->
            <!--</property>-->
        <!--</bean>-->
    
    
        <!--====研究一下对象是否会覆盖=========================================================================-->
        <bean id="computer" class="domain.Computer"></bean>
        <util:map id="map">
            <entry key="1">
                <bean class="domain.Computer"></bean>
            </entry>
            <entry key="2">
                <bean class="domain.Computer"></bean>
            </entry>
        </util:map>
    
        <!--<bean id="room" class="domain.Room">-->
            <!--<property name="name" value="001"></property>-->
            <!--<constructor-arg name="name" value="zzt"></constructor-arg>-->
            <!--<constructor-arg name="computer" ref="computer"></constructor-arg>-->
        <!--</bean>-->
    
        <!--<bean id="room" class="domain.Room" c:name="001" c:computer-ref="computer"></bean>-->
        <!--<bean id="room" class="domain.Room" c:name="zzt" p:name="002" c:computer-ref="computer"></bean>-->
    
        <bean id="testBean" class="domain.TestBean" p:name="zzt" p:computer-ref="computer" p:map-ref="map"></bean>
    
    </beans>
    

    相关文章

      网友评论

          本文标题:SpringDI

          本文链接:https://www.haomeiwen.com/subject/dndpbktx.html