zoukankan      html  css  js  c++  java
  • spring属性注入

    1,set方法注入

    (1)对于值类型的属性:

    在对象中一定要有set方法

    package com.songyan.demo1;
    
    import com.songyan.injection.Car;
    
    
    
    /**
     * 要创建的对象类
     * @author sy
     *
     */
    public class User {
    private String name;
    private int age ;/**
     * 重写toString方法在输出对象时将属性值输出
     */
    @Override
        public String toString() {
            
            return "name:"+this.name+",age:"+this.age;
        }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    }

     

    在配置文件中进行配置

    <!--在user对象中为属性名为name的属性赋值为“tom”  -->
    <property name="name" value="tom"></property>
    <property name="age" value="18"></property>
    </bean>

     测试

     

    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
        User user=(User)applicationContext.getBean("user");
        System.out.println(user);

    (2)对于引用类型的属性

    package com.songyan.injection;
    
    public class Car {
        private String name;
        private String color;
        @Override
        public String toString() {
            
            return "【carname:"+this.name+",carcolor:"+this.color+"】";
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        
    
    }
    package com.songyan.demo1;
    
    import com.songyan.injection.Car;
    
    
    
    /**
     * 要创建的对象类
     * @author sy
     *
     */
    public class User {
    private String name;
    private int age ;
    private Car car;
    
    
    
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    /**
     * 重写toString方法在输出对象时将属性值输出
     */
    @Override
        public String toString() {
            
            return "name:"+this.name+",age:"+this.age+",car:"+this.car;
        }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    }
    package com.songyan.injection;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.songyan.demo1.User;
    
    public class Client {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
        User user=(User)applicationContext.getBean("user");
        System.out.println(user);
    }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
        <!-- 引用类型的属性注入需要先将属性配置 -->
        <bean name="car" class="com.songyan.injection.Car">
            <property name="name" value="宝马"></property>
            <property name="color" value="red"></property>
        </bean>
        <!--set 方法注入值类型 -->
        <bean name="user" class="com.songyan.demo1.User">
            <!--在user对象中为属性名为name的属性赋值为“tom” -->
            <property name="name" value="tom"></property>
            <property name="age" value="18"></property>
            <!--引用类型的赋值使用ref属性 -->
            <property name="car" ref="car"></property>
        </bean>
    
    </beans>

     

    2,构造方法注入

     

    package com.songyan.injection;
    
    public class Car {
        private String name;
        private String color;
    
        // 通过构造函数对属性赋值
        public Car(String name, String color) {
            super();
            this.name = name;
            this.color = color;
        }
    
        public Car() {
    
        }
    
        @Override
        public String toString() {
    
            return "【carname:" + this.name + ",carcolor:" + this.color + "】";
        }
    
    }
    package com.songyan.demo1;
    
    import com.songyan.injection.Car;
    
    /**
     * 要创建的对象类
     * @author sy
     *
     */
    public class User {
    private String name;
    private int age ;
    private Car car;
    
    /**
     * 通过构造函数对属性赋值
     * @param name
     * @param age
     * @param car
     */
    public User(String name, int age, Car car) {
        super();
        this.name = name;
        this.age = age;
        this.car = car;
    }
    public User()
    {
        
    }
    
    
    /**
     * 重写toString方法在输出对象时将属性值输出
     */
    @Override
        public String toString() {
            
            return "name:"+this.name+",age:"+this.age+",car:"+this.car;
        }
    
    
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
        <!-- 引用类型的属性注入需要先将属性配置 -->
        <bean name="car" class="com.songyan.injection.Car">
            <constructor-arg name="name" value="baoma"></constructor-arg>
            <constructor-arg name="color" value="red"></constructor-arg>
        </bean>
        <!--构造方法注入值类型 -->
        <bean name="user" class="com.songyan.demo1.User">
            <!--在user对象中为属性名为name的属性赋值为“tom” -->
            <constructor-arg name="name" value="tom"></constructor-arg>
            <constructor-arg name="age" value="18"></constructor-arg>
            <constructor-arg name="car" ref="car"></constructor-arg>
            
        </bean>
    
    </beans>
    package com.songyan.injection;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.songyan.demo1.User;
    
    public class Client {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
        User user=(User)applicationContext.getBean("user");
        System.out.println(user);
    }
    }

    3,p名称空间注入

     

    4,spel注入

     

    5,复杂类型注入

    package com.songyan.injection;
    
    public class Car {
        private String name;
        private String color;
    
        // 通过构造函数对属性赋值
        public Car(String name, String color) {
            super();
            this.name = name;
            this.color = color;
        }
    
        public Car() {
    
        }
    
        @Override
        public String toString() {
    
            return "【carname:" + this.name + ",carcolor:" + this.color + "】";
        }
    
    }
    package com.songyan.complex;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    
    
    public class Complex {
        private Object[] obj;//数组类型注入
        private List list;//list类型注入
        private Map map;//map类型注入
        private Properties properties;//文件类型注入
        
        @Override
        public String toString() {
            
            return "array:"+Arrays.toString(obj)+"    
    list:"+this.list+"  
    properties:"+this.properties+"
    map:"+this.map;
        }
        public Object[] getObj() {
            return obj;
        }
        public void setObj(Object[] obj) {
            this.obj = obj;
        }
        public List getList() {
            return list;
        }
        public void setList(List list) {
            this.list = list;
        }
        public Map getMap() {
            return map;
        }
        public void setMap(Map map) {
            this.map = map;
        }
        public Properties getProperties() {
            return properties;
        }
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
        
        
        
    
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
        <!-- 引用类型的属性注入需要先将属性配置 -->
        <bean name="car" class="com.songyan.injection.Car">
            <constructor-arg name="name" value="baoma"></constructor-arg>
            <constructor-arg name="color" value="red"></constructor-arg>
        </bean>
        
        
        <bean name="complex" class="com.songyan.complex.Complex">
        <!--数组类型-->
            <!--数组中只有一个值类型  -->
            <!-- <property name="obj" value="tom"></property> -->
            <!--数组中只有一个引用类型  -->
            <!-- <property name="obj" ref="car"></property> -->
            <!--数组中有多个值  -->
             <property name="obj" >
                 <array>
                     <value>tom</value>
                     <value>jack</value>
                     <ref bean="car" />
                 </array>
             </property>
        <!--map类型  -->
        <property name="map">
            <map>
                <entry key="name" value="zhangsan"></entry>
                <entry key="car" value-ref="car"></entry>
            </map>    
        </property>
        <!--list类型  -->
            <!--一个值  -->
            <!-- <property name="list" value="jack"></property> -->
            <!--多个值  -->
            <property name="list">
                <list>
                    <value>jack1</value>
                    <value>jack2</value>
                    <ref bean="car"/>
                </list>
            </property>
            
        <!--properties  -->
        <property name="properties">
            <props>
                <prop key="name">user</prop>
                <prop key="pass">123</prop>
                <prop key="id">192.168.1.1</prop>
            </props>
        </property>
        </bean>
        
    
    </beans>
    package com.songyan.complex;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.songyan.demo1.User;
    
    public class Cliect {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/complex/beans.xml");
        Complex complex=(Complex)applicationContext.getBean("complex");
        System.out.println(complex);
    }
    }

     

  • 相关阅读:
    Jaeger Client Go 链路追踪|入门详解
    Go 中的 gRPC 入门详解
    【五分钟】001-数据结构概论
    【重榜?】.NET 6 Preview 1 开箱上手!带你尝试新版本更新!
    分布式链路追踪框架的基本实现原理
    【网摘】SQL练习题
    【数据库】E-R图向关系模型转换的规则
    6.0 《数据库系统概论》之关系数据库的规范化理论(数据依赖对表的影响[插入-删除-修改-冗余]、1NF-2NF-3NF-BCNF-4NF、函数依赖与多值依赖)
    【数据库】入门基础概念以及部分题目 记录 +答案+个人分析
    5.0 数据库完整性详解(PRIMARY KEY、REFERENCES、CHECK、CONSTRAINT、DOMAIN、TRIGGER)
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9129578.html
Copyright © 2011-2022 走看看