欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring框架依赖注入方法示例

程序员文章站 2024-04-01 18:07:52
在阅读这篇文章之前,大家可以先参阅《理解spring中的依赖注入和控制反转》一文,了解下依赖注入和控制反转的相关内容。 三种依赖注入的方式 属性注入,通过setter方...

在阅读这篇文章之前,大家可以先参阅理解spring中的依赖注入和控制反转一文,了解下依赖注入和控制反转的相关内容。

三种依赖注入的方式

属性注入,通过setter方法注入bean的属性值或依赖的对象 构造注入 工厂方法注入(很少使用)
例子

这里我们使用了spring-4.3.2,maven配置文件

<dependency>
 <groupid>org.springframework</groupid>
 spring-core</artifactid>
 <version>${org.springframework-version}</version>
 <exclusions>
  <exclusion>
   <groupid>commons-logging</groupid>
   commons-logging</artifactid>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-beans</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-aop</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-context</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>commons-logging</groupid>
 commons-logging</artifactid>
 <version>1.2</version>
</dependency>
<!-- junit -->
<dependency>
 <groupid>junit</groupid>
 junit</artifactid>
 <version>3.8.1</version>
 <scope>test</scope>
</dependency>

applicationcontext.xml配置文件

<!--?xml version="1.0" encoding="utf-8"?-->
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:p="https://www.springframework.org/schema/p" xmlns:util="https://www.springframework.org/schema/util" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  https://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
 
 <!-- 
  配置bean
  id:标识容器中bean对象
  class:bean的全类名,通过反射的方式在ioc容器中创建bean,所以要求bean类必须有无参构造器
  -->
 <bean class="com.spring.test.helloworld" id="helloworld">
  <property name="name" value="crystal"></property>
 </bean>
 
 <!-- 通过构造方法配置bean,可以指定参数的位置和类型,以区分重载的构造函数 -->
 <bean class="com.spring.test.car" id="car">
  <constructor-arg index="0" value="benchi"></constructor-arg>
  <constructor-arg index="1" type="double" value="200000.0"></constructor-arg>
 </bean>
 <bean class="com.spring.test.car" id="car1">
  <!-- 如果字面值包含特殊字符,使用<![cdata[]]>包裹起来
   属性值可以使用value子节点来配置
   -->
  <constructor-arg type="java.lang.string">
   <value><!--[cdata[<shanghai-->]]></value>
  </constructor-arg>
  <constructor-arg index="1" type="int" value="200"></constructor-arg>
 </bean>
 
 <bean class="com.spring.test.person" id="person">
  <property name="name" value="crystal"></property>
  <property name="age" value="20"></property>
  <!-- 可以使用ref建立引用之间的关系 -->
  <!-- 
  <property name="car" ref="car"></property>
   -->
  <!-- 
   <property name="car">
    <ref bean="car2"/>
   </property>
   -->
   <!-- 
   <property name="car">
   <bean class="com.spring.test.car">
    <constructor-arg value="changanfute"></constructor-arg>
    <constructor-arg value="3000000"></constructor-arg>
    <constructor-arg value="240"></constructor-arg>
   </bean>
   </property> 
   -->
   <!-- 测试赋值null -->
   <!-- <property name="car"><null/></property> -->
   <property name="car" ref="car1"></property>
   <!-- 为级联属性赋值,注意:属性需要先初始化后才能为级联属性赋值,和structs2不同 -->
   <property name="car.price" value="400000"></property>
 </bean>
 
 <!-- 测试配置集合属性 -->
 <bean class="com.spring.test.collections.person" id="person3">
  <property name="name" value="barry"></property>
  <property name="age" value="21"></property>
  <property name="cars">
   <list>
    <ref bean="car">
    <ref bean="car1">
    <bean class="com.spring.test.car">
     <constructor-arg value="changanfute"></constructor-arg>
     <constructor-arg value="3000000"></constructor-arg>
     <constructor-arg value="240"></constructor-arg>
    </bean>
   </ref></ref></list>
  </property>
 </bean>
 
 <!-- 配置map的属性值 -->
 <bean class="com.spring.test.collections.newperson" id="newperson">
  <property name="name" value="lina"></property>
  <property name="age" value="22"></property>
  <property name="cars">
   <!-- 使用map节点及map的entry子节点配置map类型的成员变量 --><map>
    <entry key="aa" value-ref="car"></entry>
    <entry key="bb" value-ref="car1"></entry></map>
  </property>
 </bean>
 
 
 <!-- 配置properties属性值 -->
 <bean class="com.spring.test.collections.datasource" id="datasource">
  <property name="properties">
   <!-- 使用props和prop子节点来为properties属性赋值 -->
   <props>
    <prop key="user">root</prop>
    <prop key="password">1234</prop>
    <prop key="jdbcurl">jdbc:mysql://test</prop>
    <prop key="jdbcdriver">com.mysql.jdbc.driver</prop>
   </props>
  </property>
 </bean>
 
 <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
 <util:list id="cars">
  <ref bean="car">
  <ref bean="car1">
 </ref></ref></util:list>
 
 <bean class="com.spring.test.collections.person" id="person4">
  <property name="name" value="jackie"></property>
  <property name="age" value="30"></property>
  <property name="cars" ref="cars"></property>
 </bean>
 
 <!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统配置方式更为简洁 -->
 <bean class="com.spring.test.collections.person" id="person5" p:age="32" p:cars-ref="cars" p:name="queue"></bean>
</beans>

1. 下面是简单的属性注入、构造注入的测试类

car.java

package com.spring.test;
public class car {
	private string name;
	private int maxspeed;
	private double price;
	public car() {
	}
	public car(string name, double price) {
		this.name = name;
		this.price = price;
	}
	public car(string name, int maxspeed) {
		this.name = name;
		this.maxspeed = maxspeed;
	}
	public car(string name, double price, int maxspeed) {
		this.name = name;
		this.price = price;
		this.maxspeed = maxspeed;
	}
	public void setprice(double price) {
		this.price = price;
	}
	@override
	 public string tostring() {
		return "car [name:" + name + ", price:" + price + ", maxspeed:" + maxspeed + "]";
	}
}

helloworld.java

package com.spring.test;
public class helloworld {
	private string name;
	public helloworld() {
		system.out.println("helloworld constructor...");
	}
	public string getname() {
		return name;
	}
	public void setname(string name) {
		system.out.println("setname:" + name);
		this.name = name;
	}
	@override
	 public string tostring() {
		return "hello," + name;
	}
}

person.java

package com.spring.test;
public class person {
	private string name;
	private int age;
	private car 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;
	}
	public car getcar() {
		return car;
	}
	public void setcar(car car) {
		this.car = car;
	}
	@override
	 public string tostring() {
		return "person: [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}

main.java

package com.spring.test;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class main {
	public static void main(string[] args) {
		helloworld hello = new helloworld();
		hello.setname("barry");
		system.out.println("print:"+ hello + "\n");
		// 装入 spring 配置文件
		/**
   * 装入 spring 配置文件
   * applicationcontext是ioc容器,它有两个主要实现类(classpathxmlapplicationcontext和filesystemxmlapplicationcontext)
   * applicationcontext在初始化上下文时就实例化所有单例的bean
   */
		applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
		//helloworld hello1 = (helloworld) context.getbean("helloworld"); // 通过id获取bean对象
		helloworld hello1 = context.getbean(helloworld.class);
		// 通过类型获取bean对象(要求在ioc容器里该类型的对象只能有一个)
		system.out.println(hello1);
	}
	@test
	 public void testcontructor() {
		applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
		car car = (car) context.getbean("car");
		// 通过类型获取bean对象(要求在ioc容器里该类型的对象只能有一个)
		car car1 = (car) context.getbean("car1");
		system.out.println(car);
		system.out.println(car1);
		person person = (person) context.getbean("person");
		system.out.println(person);
	}
}

2. 下面是集合的测试类
newperson.java

package com.spring.test.collections;
import java.util.map;
import com.spring.test.car;
public class newperson {
	private string name;
	private int age;
	private map<string, car=""> cars;
	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 map<string, car=""> getcars() {
		return cars;
	}
	public void setcars(map<string, car=""> cars) {
		this.cars = cars;
	}
	@override
	 public string tostring() {
		return "person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
}

person.java

package com.spring.test.collections;
import java.util.list;
import com.spring.test.car;
public class person {
	private string name;
	private int age;
	private list<car> cars;
	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 list<car> getcars() {
		return cars;
	}
	public void setcars(list<car> cars) {
		this.cars = cars;
	}
	@override
	 public string tostring() {
		return "person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
}

datasource.java

package com.spring.test.collections;
import java.util.properties;
public class datasource {
	private properties properties;
	public properties getproperties() {
		return properties;
	}
	public void setproperties(properties properties) {
		this.properties = properties;
	}
	@override
	 public string tostring() {
		return "datasource: [properties:" + properties + "]";
	}
}

main.java

package com.spring.test.collections;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class main {
	@test
	 public void testcollections() {
		applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
		person person = (person) context.getbean("person3");
		system.out.println(person);
		newperson newperson = (newperson) context.getbean("newperson");
		system.out.println(newperson);
		datasource datasource = (datasource) context.getbean("datasource");
		system.out.println(datasource);
		person person4 = (person) context.getbean("person4");
		system.out.println(person4);
		person person5 = (person) context.getbean("person5");
		system.out.println(person5);
	}
}

总结

以上就是本文关于spring框架依赖注入方法示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!