码迷,mamicode.com
首页 > 编程语言 > 详细

框架学习 Spring之依赖注入DI

时间:2019-07-05 22:48:02      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:list()   class   view   文件   util   java   lis   setter   属性注入   

依赖注入的方式有四种:

1、Setter注入(属性注入)

2、构造器注入

3、P命名空间注入

4、集合类型值注入

 

 

 

1、Setter注入(属性注入)

Employee 员工实体类

技术图片
package com.spring.pojo;

public class Employee {
    private Integer id;
    private String name;
    
    private Department department;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]";
    }

    public Employee(Integer id, String name, Department department) {
        super();
        this.id = id;
        this.name = name;
        this.department = department;
    }

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    

}
View Code

 

 

Department 部门实体类

技术图片
package com.spring.pojo;

public class Department {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Department() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Department(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Department [id=" + id + ", name=" + name + "]";
    }
    

}
View Code

 

主配置文件里面

技术图片

 

 

 

2、构造器注入

技术图片

 

3、P命名空间注入

添加命名空间

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

使用P标签

技术图片

 

 

4、集合类型注入

创建collection集合实体类

 

技术图片
package com.spring.pojo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
    private List<String> list;
    private Set<String> set;
    private Map<String, Object> map;
    private Properties properties;
    private String[] array;
    public CollectionBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public CollectionBean(List<String> list, Set<String> set, Map<String, Object> map, Properties properties,
            String[] array) {
        super();
        this.list = list;
        this.set = set;
        this.map = map;
        this.properties = properties;
        this.array = array;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Set<String> getSet() {
        return set;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public String[] getArray() {
        return array;
    }
    public void setArray(String[] array) {
        this.array = array;
    }

}
View Code

 

 

主配置文件中,依赖注入

技术图片
 <bean id="collectionBean" class="com.spring.pojo.CollectionBean">
         <!--有序可重复  -->
         <property name="list">
         <list>
             <value>list1</value>
             <value>list2</value>
             <value>list3</value>
         </list>
             </property>
     
         <!--无序不可重复  -->
         <property name="set" >
             <set>
                 <value>set1</value>
                 <value>set2</value>
                 <value>set3</value>
             </set>
         </property>
         
         <property name="map">
             <map>
                 <entry key="key1" value="法海1"></entry>
                 <entry key="key2" value="法海2"></entry>
                 <entry key="key3" value="法海3"></entry>
             </map>
         </property>
         
         <property name="array">
             <array>
                 <value>String1</value>
                 <value>String2</value>
                 <value>String3</value>
             </array>
         
         </property>
         
         <!--properties是特殊的Map  -->
         <property name="properties">
             <props>
                 <prop key="prokey1">values1</prop>
                 <prop key="prokey2">values2</prop>
                 <prop key="prokey3">values3</prop>             
             </props>     
         </property>
         
     </bean>
     
View Code

 

 

 

 

框架学习 Spring之依赖注入DI

标签:list()   class   view   文件   util   java   lis   setter   属性注入   

原文地址:https://www.cnblogs.com/luojack/p/11141004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!