Spring 依赖注入(注入参数)

Posted Claricre

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 依赖注入(注入参数)相关的知识,希望对你有一定的参考价值。

注入参数基本分7类:

1.基本类型值

2.注入bean

3.内部bean

4.注入null值

5.级联属性

6.List,Set,Map集合的注入

7.properties文件的注入(和集合注入基本是一样的)

下面就是实体类People2,beans.xml的源码

技术分享
package com.maya.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class People2 {
    private int id;
    private String name;
    private int age;
    private Dog dog;//注入bean
    private List<String> myList=new ArrayList<String>();//List集合注入    
    private Set<String> mySet=new HashSet<String>();//Set集合注入
    private Map<String,String> myMap=new HashMap<String,String>();//Map集合注入
    private Properties jdbc=new Properties();//properties文件注入,和map差不多都是以key、value的形式
    public Map<String, String> getMyMap() {
        return myMap;
    }
    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
    public Set<String> getMySet() {
        return mySet;
    }
    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }
    public List<String> getMyList() {
        return myList;
    }
    public void setMyList(List<String> myList) {
        this.myList = myList;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
    public People2() {
        super();
    }
    public People2(int id, String name, int age, Dog dog) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.dog = dog;
    }
    
    public Properties getJdbc() {
        return jdbc;
    }
    public void setJdbc(Properties jdbc) {
        this.jdbc = jdbc;
    }
    
    @Override
    public String toString() {
        return "People2 [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + ", myList=" + myList
                + ", mySet=" + mySet + ", myMap=" + myMap + ", jdbc=" + jdbc + "]";
    }
    //当要使用dog.name是就要注意beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
      
      <!-- 注入参数 -->
      <!-- 基本类型注入 -->
      <bean id="people21" class="com.maya.model.People2">
          <constructor-arg name="id" value="0"></constructor-arg>
          <constructor-arg name="name" value="张三"></constructor-arg>
          <constructor-arg name="age" value="10"></constructor-arg>
          <constructor-arg name="dog" ref="dog1"></constructor-arg>
      </bean>
      
      <!-- 注入bean -->
      <bean id="dog1" class="com.maya.model.Dog">
          <property name="name" value="阿黄"></property>
      </bean>
      
      <bean id="people22" class="com.maya.model.People2">
          <property name="id" value="1"></property>
        <property name="name" value="小明2"></property>
        <property name="age" value="22"></property>
          <property name="dog" ref="dog1"></property><!-- 注入bean时ref是要注入的bean的id -->
      </bean>
      
      <!-- 注入内部bean -->
      <bean id="people23" class="com.maya.model.People2">
          <property name="id" value="3"></property>
        <property name="name" value="小明3"></property>
        <property name="age" value="33"></property>
          <property name="dog">
              <bean class="com.maya.model.Dog">
                  <property name="name" value="UZI"></property><!-- 分开写而已 -->
              </bean>
          </property>
      </bean>
      
      <!-- 注入null -->
      <bean id="people24" class="com.maya.model.People2">
          <property name="id" value="3"></property>
        <property name="name" value="小明3"></property>
        <property name="age" value="33"></property>
          <property name="dog">
              <null></null><!-- 给dog属性注入一个null -->
          </property>
      </bean>
      
      <!-- 级联方式注入 -->
      <!-- <bean id="people25" class="com.maya.model.People2">
          <property name="id" value="3"></property>
        <property name="name" value="小明3"></property>
        <property name="age" value="33"></property>
          <property name="dog.name" value="小明的狗"></property>
          注意:以这种方式进行赋值时,因为我们没用注入Dog的实例对象,所以我们要在People2的dog属性后new一个Dog实例!这样才能成功赋值
      </bean> -->
      
      <!-- 注入List集合,Set集合,Map集合 -->
      <bean id="people26" class="com.maya.model.People2">
          <property name="id" value="3"></property>
        <property name="name" value="小明3"></property>
        <property name="age" value="33"></property>
          <property name="dog" ref="dog1"></property>
          <!-- List集合 -->
          <property name="myList">
              <list>
                  <value>a</value>
                  <value>b</value>
                  <value>c</value>
                  <value>d</value>
              </list>
          </property>
          <!-- Set集合,Set集合是自动去掉重复值得!所以输出的结果还是a,b,c -->
          <property name="mySet">
              <set>
                  <value>a</value>
                  <value>b</value>
                  <value>c</value>
                  <value>c</value>
                  <value>b</value>
                  <value>a</value>
              </set>
          </property>    
          <!-- Map集合 -->
          <property name="myMap">
              <map>
                  <entry>
                      <key><value>上午</value></key>
                      <value>写代码</value>
                  </entry>
                  <entry>
                      <key><value>下午</value></key>
                      <value>测试代码</value>
                  </entry>
              </map>
          </property>
          <!-- 配置properties文件 -->
          <property name="jdbc">
            <props>
                  <prop key="qd">com.mysql.jdbc.Driver</prop>
                  <prop key="url">jdbc:mysql://localhost:3306/spring4?characterEncoding=GBK</prop>
                  <prop key="user">root</prop>
              </props>
        </property>
      </bean>
      
      <!-- 配置properties文件 -->
      <bean id="people27" class="com.maya.model.People2">
      <!-- 在这之前没有为其他属性赋值,这是可以得。但是当使用其引用bean的某些方法时就必须给其赋值而且不能是null -->
        <property name="jdbc">
            <props>
                  <prop key="qd">com.mysql.jdbc.Driver</prop>
                  <prop key="url">jdbc:mysql://localhost:3306/spring4?characterEncoding=GBK</prop>
                  <prop key="user">root</prop>
              </props>
        </property>
      </bean>          
</beans>
技术分享

 常见错误:

Could not resolve matching constructor(构造函数问题)

Loading XML bean definitions from class path resource(实体类中 引用的其他bean类正在使用它的某些方法!去掉使用的方法就好了)

以上是关于Spring 依赖注入(注入参数)的主要内容,如果未能解决你的问题,请参考以下文章

Spring 依赖注入(注入参数)

装配SpringBean--依赖注入

常见的三种注解注入方式对比

Spring重温--Spring依赖注入(DI)

Spring属性注入构造方法注入工厂注入以及注入参数(转)

Java Spring学习笔记----Bean的依赖注入