Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)

Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)

一、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配置方式)

 

二、Bean的属性注入:

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

(1)使用有参构造注入

(2)使用set()方法注入

(3)使用接口注入

2、代码测试:

(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)set()方法注入;

(2)构造器注入:①通过index设置参数的位置;②通过type设置参数类型;

(3)静态工厂注入;

(4)实例工厂;

详细内容请参考这篇文章:Spring中bean的注入方式

 

四、复杂注入:数组、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配置方式)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114732.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java日志全解析(上) – 源流「建议收藏」

    作为Java程序员,幸运的是,Java拥有功能和性能都非常强大的日志库;不幸的是,这样的日志库有不止一个——相信每个人都曾经迷失在JUL(JavaUtilLog),JCL(CommonsLogging),Log4j,SLF4J,Logback,Log4j2等等的迷宫中。在我见过的绝大多数项目中,都没有能够良好的配置和使用日志库。

    2022年4月8日
    34
  • 腾讯云中ssL证书的配置安装

    腾讯云中ssL证书的配置安装

    2021年10月14日
    64
  • laravel5.6 调用第三方类库

    laravel5.6 调用第三方类库

    2021年11月5日
    43
  • Java实现AD域登录认证「建议收藏」

    Java实现AD域登录认证「建议收藏」web项目中有时候客户要求我们使用ad域进行身份确认,不再另外做一套用户管理系统。其实客户就是只要一套账号可以访问所有的OA,CRM等办公系统。这就是第三方验证。一般有AD域,Ldap,Radius,邮件服务器等。最常用的要数AD域了。因为window系统在国内占据了大量的江山。做起来也很方便。我这篇文章就是写,如何用java去实现AD域的身份验证。好了,直接看代码吧:

    2022年5月16日
    36
  • 基于RT-Thread操作系统的 基础四轮组智能车设计与实践

    基于RT-Thread操作系统的 基础四轮组智能车设计与实践这是一个很好的window,可以多少的。英俊的训练的场地的情况测window。简介:这是一个很好的window,可以多少的。英俊的训练的场地的情况测window。关键词:window

    2022年7月25日
    9
  • 光流法:Farneback

    光流法:Farneback光流法:Farnback光流法:Farnback基本假设Farneback光流法图像模型位移估计Reference现实世界中,万物都在在运动,且运动的速度和方向可能均不同,这就构成了运动场。物体的运动投影在图像上反应的是像素的移动。这种像素的瞬时移动速度就是光流。光流法是利用图像序列中的像素在时间域上的变化、相邻帧之间的相关性来找到的上一帧跟当前帧间存在的…

    2022年7月23日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号