框架学习 Spring之依赖注入DI

2020-12-13 05:26

阅读:200

标签: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 list;
    private Set set;
    private Map map;
    private Properties properties;
    private String[] array;
    public CollectionBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public CollectionBean(List list, Set set, Map map, Properties properties,
            String[] array) {
        super();
        this.list = list;
        this.set = set;
        this.map = map;
        this.properties = properties;
        this.array = array;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public Set getSet() {
        return set;
    }
    public void setSet(Set set) {
        this.set = set;
    }
    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;
    }
    public String[] getArray() {
        return array;
    }
    public void setArray(String[] array) {
        this.array = array;
    }

}
View Code

 

 

主配置文件中,依赖注入

技术图片技术图片
 class="com.spring.pojo.CollectionBean">
         
         list1list2list3set1set2set3String1String2String3values1values2values3
View Code

 

 

 

 

框架学习 Spring之依赖注入DI

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

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


评论


亲,登录后才可以留言!