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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PDB 文件

    PDB 文件PDB文件什么是PDB文件PDB(ProgramDataBase)即程序的基本数据,是VS编译链接时生成的文件,每个程序集(EXE或DLL)都有一个与之对应的PDB文件。DPB文件主要存储了VS调试程序时所需要的基本信息,主要包括源文件名、变量名、函数名、对应的行号等等。因为存储的是调试信息,所以一般情况下PDB文件是在Debug模式下才会生成。…

    2022年6月2日
    94
  • 【 Linux学习】SSH连接时出现Host key verification failed的原因及解决方法以及ssh-keygen命令的用法

    【 Linux学习】SSH连接时出现Host key verification failed的原因及解决方法以及ssh-keygen命令的用法一、问题描述今天下午使用ssh连接其他服务器进行scp操作的时候,提示失败,如下所示:[root@localhostbackups]#scproot@172.xxx.xxx.xxx:/data/gitlabData/backups/1539717714_2018_10_17_9.4.3_gitlab_backup.tar.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@……

    2022年10月21日
    3
  • 视觉SLAM技术_解读团体技术

    视觉SLAM技术_解读团体技术近年来,SLAM技术取得了惊人的发展,领先一步的激光SLAM已成熟的应用于各大场景中,视觉SLAM虽在落地应用上不及激光SLAM,但也是目前研究的一大热点,今天我们就来详细聊聊视觉SLAM的那些事儿。视觉SLAM是什么?视觉SLAM主要是基于相机来完成环境的感知工作,相对而言,相机成本较低,容易放到商品硬件上,且图像信息丰富,因此视觉SLAM也备受关注。目前,视觉SLAM可…

    2022年9月27日
    4
  • Nginx命令大全[通俗易懂]

    nginx #打开nginxnginx-t   #测试配置文件是否有语法错误nginx-sreopen #重启Nginxnginx-sreload  #重新加载Nginx配置文件,然后以优雅的方式重启Nginxnginx-sstop  #强制停止Nginx服务nginx-squit  #优雅地停止Nginx服务…

    2022年4月13日
    45
  • 关于缺陷报告_登录模块缺陷报告

    关于缺陷报告_登录模块缺陷报告基本原则:尽快报告缺陷、有效描述缺陷、报告缺陷时不做任何评价、确保缺陷可以重现软件缺陷是存在于软件之中的那些不希望或不可能接受的偏差软件测试过程管理的理念:尽早测试、全面测试、全过程测试、独立迭代测试 缺陷报告的写作准则书写清晰、完整的缺陷报告是对保证缺陷正确处理的最佳手段。 它也减少了工程师以及其它质量保证人员的后续工作。为了书写更优良的缺陷报告,需要遵守“5

    2025年12月9日
    6
  • 移动端开发框架

    移动端开发框架总体概述现在比较流行的移动APP开发框架有以下六种:网页、混合、渐进、原生、桥接、自绘。前三种体验与Web的体验相似,后三种与原生APP的体验相似。这六种框架形式,都有自己适用的范围。无所谓好坏,适用就是好。网页应用适用于传统网站APP化,比如淘宝、京东,有大量WEB页面嵌入到APP中。混合应用适用于小成本应用开发,全部代码都基于Web,好处是开发快速、成本低。渐进应用适用于高机会成本的场合,边下载边使用,能快速获取,快速体验。原生应用适用于大型和高体验要求的应用,能做出让人.

    2022年6月24日
    26

发表回复

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

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