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

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)

程序员文章站 2024-03-24 17:19:40
...

一、Bean实例化的三种方式:(通过配置文件创建对象)

(1)使用类的无参构造创建(最常使用)

(2)使用静态工厂创建

(3)使用实例工厂创建

代码实例:

(1)项目结构:

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)

(2)在pom.xml中导入spring的核心jar包依赖:

(3)applicationContext.xml的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 1.使用类的无参构造函数创建 -->
	<bean id="user" class="com.zwp.domain.User"></bean>

	<!-- 2.使用静态工厂进行创建 -->
	<!-- class的值不是写User对象的全路径,而是写静态工厂的全路径 -->
	<!-- factory-method的值写要调用的方法 -->
	<bean id="user2" class="com.zwp.domain.StaticFactory" factory-method="getUser"></bean>
	
	<!-- 3.使用实例工厂进行创建 -->
	<!-- 需要先创建beanFactory对象,再通过beanFactory对象进行调用 -->
	<bean id="beanFactory" class="com.zwp.domain.BeanFactory"></bean>
	<bean id="user3" factory-bean="beanFactory" factory-method="getUser"></bean>
</beans>

(4)domain类的代码:

public class User {
	public void add(){
		System.out.println("创建了一个User对象.....");
	}
}
//静态工厂调用:
public class StaticFactory {
	//静态的方法,返回User对象:
	public static User getUser(){
		return new User();
	}
}
//实例工厂
public class BeanFactory {
	//普通的方法,返回User对象
	//不能通过类名调用,需要通过对象调用。
	public User getUser(){
		return new User();
	}
}

(5)测试类:

public class Test1 {
	@Test
	public void test(){
		//1.加载spring配置文件,
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		//2.得到无参构造函数创建的对象:
		User user =(User) context.getBean("user");
		//得到静态工厂创建的对象:
		User user2 =(User) context.getBean("user2");
		//得到实例工厂创建的对象:
		User user3=(User) context.getBean("user3");
		
		System.out.println("无参构造函数创建的对象:"+user);
		System.out.println("静态工厂创建的对象:"+user2);
		System.out.println("实例工厂创建的对象:"+user3);
	}
}
(6)测试结果:每种方式都创建了一个对象

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)



二、属性注入:

1、创建对象的时候,向类里面属性设置值

2、属性注入的三种方式:(spring里面只支持前两种)

(1)使用有参构造注入

(2)使用set方法注入

(3)使用接口注入

3、代码测试:

(1)applicationContext.xml的配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 属性注入的方式:start -->
	<!-- 1.有参构造属性注入 -->
	<bean id="construct" class="com.zwp.domain.Book1">
		<constructor-arg name="bookname" value="这是Book1的name"></constructor-arg>
	</bean>
	
	<!-- 2.set方法属性注入 -->
	<bean id="setproperty" class="com.zwp.domain.Book2">
		<property name="bookname" value="这是Book2的name"></property>
	</bean>
	<!-- 属性注入的方式:end -->
</beans>

(2)domain类的代码:

//有参构造注入:
public class Book1 {
	private String bookname;
	public Book1(String bookname) {
		this.bookname = bookname;
	}
	public void text(){
		System.out.println("有参构造注入:"+bookname);
	}
}
//set方法注入属性:
public class Book2 {
	private String bookname;
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public void text(){
		System.out.println("set方法注入属性:"+bookname);
	}
}

(3)测试类:

public class Test2 {
	@Test
	public void test(){
		//1.加载spring配置文件,
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		//2.得到加载的对象
		Book1 book1 = (Book1) context.getBean("construct");
		Book2 book2 = (Book2) context.getBean("setproperty");
		book1.text();
		book2.text();
	}
}

(4)输出结果:

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)



三、对象注入:

(1)applicationContext.xml的配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 对象注入: -->
	<!-- 先创建userDao、userService对象,再在userService对象里面注入userDao属性 -->
	<!-- 在这里,不能使用value,要使用ref,因为是对象注入,不是属性注入 -->
	<!-- ref引用的值是userDao的id值;name为userService的属性 -->
	<bean id="userdao" class="com.zwp.object.userDao"></bean>
	<bean id="userservice" class="com.zwp.object.userService">
		<property name="dao" ref="userdao"></property>
	</bean>
</beans>

(2)相关类的代码:

//spring对象注入:
public class userDao {
	public void add(){
		System.out.println("dao对象注入...");
	}
}
//spring对象注入:
public class userService {
	//创建属性,set方法
	private userDao dao;
	public void setDao(userDao dao){
		this.dao=dao;
	}
	
	public void add(){
		System.out.println("service对象注入...");
		dao.add();
	}
}

(3)测试类代码:

public class Test2 {
	@Test
	public void test2(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		userService userservice = (userService) context.getBean("userservice");
		userservice.add();
	}
}

(4)运行结果:

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)



四、复杂注入:数组、list集合、map集合、properties

(1)相关类代码:

//复杂类型属性注入:
public class ComplexType {
	//第一步:
	private String[] arrs;
	private List<String> list;
	private Map<String,String> map;
	private Properties properties;
	
	//第二步:set方法
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	//展示注入的属性
	public void show(){
		System.out.println("arrs:"+arrs);
		System.out.println("list:"+list);
		System.out.println("map:"+map);
		System.out.println("properties:"+properties);
	}
}

(2)applicationContext.xml文件的配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 复杂属性的注入 -->
	<bean id="complextype" class="com.zwp.object.ComplexType">
		<!-- 1.数组 -->
		<property name="arrs">
			<list>
				<value>arr1</value>
				<value>arr2</value>
				<value>arr3</value>
			</list>
		</property>
		<!-- 2.list集合 -->
		<property name="list">
			<list>
				<value>list1</value>
				<value>list2</value>
				<value>list3</value>
			</list>
		</property>
		<!-- 3.map集合 -->
		<property name="map">
			<map>
				<entry key="1" value="map1"></entry>
				<entry key="2" value="map2"></entry>
				<entry key="3" value="map3"></entry>
			</map>		
		</property>
		<!-- 4.properties -->
		<property name="properties">
			<props>
				<prop key="a">properties-a</prop>
				<prop key="b">properties-b</prop>
				<prop key="c">properties-c</prop>
			</props>
		</property>
	</bean>
</beans>

(3)测试类

public class Test2 {
	@Test
	public void test3(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		ComplexType complexType = (ComplexType) context.getBean("complextype");
		complexType.show();
	}
}

(4)运行结果:

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)


附:项目结构

Spring的Bean实例化的三种方式、属性注入、对象注入、复杂注入(xml文件配置方式)