• SpringIOC使用扩展


    在上篇博客中,我们使用Spring通过setter访问器实现了对属性的赋值,这种做法被称为设值注入。除此之外Spring还提供了通过构造方法赋值的能力,成为构造注入。下面我们通过一个小demo来了解如何通过构造方法来注入值(因一个类中可能包含其他自定义类型的对象,所以我们采用Student类中包含Car类的实例来演示如何通过构造来给Student类属性注入值)

    Student类:

    public class Student {
        private String name;//学生姓名
        private Integer age; //学生年龄
        private Car car;//学生的汽车
        @Override
        //重写toString()方法方便进行测试
        public String toString() {
            return "Student [name=" + name + ", age=" + age + ", car=" + car + "]";
        }
        //构造函数
        public Student(String name, Integer age, Car car) {
            System.out.println("我是带参构造");
            this.name = name;
            this.age = age;
            this.car = car;
        }
        
        public Student() {
            System.out.println("我是无参构造");
        }
        //属性访问器
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Car getCar() {
            return car;
        }
        public void setCar(Car car) {
            this.car = car;
        }
        
    }

    Car类:

    public class Car {
        //汽车颜色
        private String color;
        //属性访问器
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
        //重写toString()方法方便测试
        @Override
        public String toString() {
            return "Car [color=" + color + "]";
        }
         
        
        
    }

    applicationContext.xml配置:

    <bean id="car" class="cn.wz.entity.Car">
             <property name="color" value="白色"/>
        </bean>
                        
        <bean id="stu" class="cn.wz.entity.Student">
        <!--通过constructor-arg元素向构造方法传入参数-->
                <constructor-arg index="0" value="王哲"/>
        <constructor-arg index="1" value="18"/>
        <constructor-arg index="2" ref="car"/>                    
        </bean>

    测试代码:

    public class Test {
        public static void main(String[] args) {
            
            hasArgumentConstructor();
        }
        public static void hasArgumentConstructor(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student student = context.getBean("stu",Student.class);
            System.out.println(student);
            
        }
    }

    最终运行结果:

    image

    使用p命名空间实现属性注入

    Spring配置文件采用schema形式,使用不同的名命空间管理不同类型的配置,是的配置文件更具扩展性。而p命名空间的配置则不再使用property子元素来为对象的属性注入值,而是以Bean元素属性的方式来注入值如我们将上面demo的配置代码更改如下:

    <bean id="car" class="cn.wz.entity.Car">
             <property name="color" value="白色"/>
        </bean>
         <bean id="stu" class="cn.wz.entity.Student" p:name="王哲" p:age="18" p:car-ref="car" />

    其测试用例运行效果是一样的,但需要注意的是在使用这种方式给属性注入值时一定要先导入P命名空间如图所示:image

    从上图中我们可以发现我们再xml配置文件的头文件中不但配置了P命名空间还有c命名空间,c命名空间是在使用构造注入值时使用其用法和P命名空间一样,这里不再进行演示了。

    为集合属性注入值

    对于存在属性类型为集合的对象Spring提供了list、set、map等元素对其进行配置。具体配置如下:

    <!-- List集合 -->
        <bean    id="list" class="cn.wz.entity.Student">
            <property name="list">
                <list>
                    <value>王哲</value>
                    <value>张一铭</value>
                </list>
            </property>
        </bean>
        <!-- Set集合 -->
        <bean    id="set" class="cn.wz.entity.Student">
            <property name="set">
                <set>
                    <value>王哲</value>
                    <value>张一铭</value>
                </set>
            </property>
        </bean>
        <!-- map集合 -->
        <bean    id="map" class="cn.wz.entity.Student">
            <property name="map">
                <map>
                    <entry key="wz"><value>王哲</value></entry>
                    <entry key="zym"> <value>张一铭</value></entry>
                </map>
            </property>
        </bean>
        <!-- 数组 -->
        <bean    id="array" class="cn.wz.entity.Student">
            <property name="array">
                <list>
                <value>王哲</value>
                <value>张一铭</value>
                </list>
            </property>
        </bean>
  • 相关阅读:
    idea 出现 java.noSuchMechodFound
    IDEA maven 多项目 出现 java 程序包找不到
    Nginx中间件使用心得(一)
    IntelliJ IDEA 2017版 编译器使用学习笔记(三) (图文详尽版);IDE快捷键使用
    IntelliJ IDEA 2017版 编译器使用学习笔记(二) (图文详尽版);IDE快捷键使用
    IntelliJ IDEA 2017版 编译器使用学习笔记(一) (图文详尽版);IDE快捷键使用;IDE多行代码同时编写
    IntelliJ IDEA 2017版 导入项目项目名称为红色
    IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合
    IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)
    IntelliJ IDEA 2017版 spring-boot修改端口号配置把端口号改为8081
  • 原文地址:https://www.cnblogs.com/wangzheand/p/5929197.html
Copyright © 2020-2023  润新知