`

Spring依赖注入方式---- 构造方法注入

 
阅读更多

Spring通过构造方法依赖注入

1.  创建一个Person

 

package com.spring.bean;
public class Person {
	private String name;
	private String address;
	private double height;
	private int weight;
	
	public Person(String name, String address, double height) {
		super();
		this.name = name;
		this.address = address;
		this.height = height;
	}
	
	public Person(String name, String address, int weight) {
		super();
		this.name = name;
		this.address = address;
		this.weight = weight;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", address=" + address + ", height=" + height + ", weight=" + weight + "]";
	}
	
	
}

 

 

2. 创建spring 配制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">

	
	<!-- 通过构造器注入属性-->
	<!-- 1. 通过index属性指定要注入的属性值,默认会选择靠前的构造器注入 -->
	<bean id="person1" name="person" class="com.spring.bean.Person">
		<constructor-arg value="matt" index="0"></constructor-arg>
		<constructor-arg value="上海" index="1"></constructor-arg>
		<constructor-arg value="172" index="2"></constructor-arg>
	</bean>
	<!-- 2. 通过name属性指定要注入的属性值-->
	<bean id="person2" class="com.spring.bean.Person">
		<constructor-arg value="joy" index="0"></constructor-arg>
		<constructor-arg value="上海" index="1"></constructor-arg>
		<constructor-arg value="59" name="weight"></constructor-arg>
	</bean>
	
	<!-- 2. 通过type属性指定要注入的属性值-->
	<bean id="person3" class="com.spring.bean.Person">
		<constructor-arg value="joy" index="0"></constructor-arg>
		<constructor-arg value="上海" index="1"></constructor-arg>
		<constructor-arg value="59" type="int"></constructor-arg>
	</bean>
</beans>

 

 

3. 创建运行类

 

package com.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PersonMain {

	public static void main(String[] args) {
		ApplicationContext ctx  = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Person person1 = (Person) ctx.getBean("person1");
		System.out.println(person1);
		
		Person person2 = (Person) ctx.getBean("person2");
		System.out.println(person2);
		
		Person person3 = (Person) ctx.getBean("person3");
		System.out.println(person3);

	}

}

 

运行结果:

Person [name=matt, address=上海, height=172.0, weight=0]
Person [name=joy, address=上海, height=0.0, weight=59]
Person [name=joy, address=上海, height=0.0, weight=59]

 

注, 可以通过spring配制文件中不同的属性指定选择相应的构造方法。

         

 

 

分享到:
评论

相关推荐

    Spring 依赖注入 构造方法注入

    NULL 博文链接:https://zhangyulong.iteye.com/blog/856986

    Spring系列之依赖注入的三种方式.docx

    如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的 依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程(将依赖关系注入到对象中) ,spring的依赖注入有3种方式: ...

    SpringSet方法注入

    我们学习了spring框架spring框架里面有3个优势第一个是轻量级的IOC也叫控制反转后来改名为DI也叫依赖注入,依赖注入里面有3中注入方法分别是set注入,构造器注入,注解注入,我传的是set注入的视频

    Spring依赖注入的三种方式实例详解

    Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的。 首先我们需要以下几个类: 接口 Logic.java 接口实现类 ...

    Java框架篇?spring 依赖注入

    spring依赖注入的方式有4种  构造方法注入  属性注入  工厂注入  注解注入  下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class User { private String username; private...

    java7源码翻译-recap-spring:回顾春天

    依赖注入 依赖项是类的属性(实例变量)(最终成为对对象的引用) 使用 setter 方法或使用构造函数参数注入依赖项。 我们使用定义要注入的依赖项的 xml 配置或注释来定义元数据。 注入依赖的过程称为连接,它将所有...

    深入解析Java的Spring框架中bean的依赖注入

    主要介绍了Java的Spring框架中bean的依赖注入,讲解了以构造函数为基础的依赖注入和基于setter方法的依赖注入的方式,需要的朋友可以参考下

    99%的人答不对Spring推断构造方法!打破你三观!!!(范例→源码)

    Spring 推荐对于那些必须依赖注入的属性,使用构造方法注入; 而那些不一定非要注入的属性,Spring 则推荐使用 setter 注入。 所以,既然 Spring 官网都那么说了,你要是连构造方法注入都不好好学习,那可就有点...

    详解Spring的核心机制依赖注入

    如A组件调用B组件的方法,可称A组件依赖于B组件,依赖注入让Spring的Bean以配置文件组织在一起,而不是以硬编码的方式耦合在一起 一、理解依赖注入 依赖注入(Dependency Injection) = 控制反转(Inversion ofControl,...

    Spring5参考指南:依赖注入

    依赖注入就是在Spring创建Bean的时候,去实例化该Bean构造函数所需的参数,或者通过Setter方法去设置该Bean的属性。Spring的依赖注入有两种基于构造函数的依赖注入和基于setter的依赖注入。构造函数的注入是通过构造...

    spring-tutorial-elo-rating-system-corvinus

    技术领域Maven-依赖关系管理和构建JUni5和Jupiter-测试Lombok-带注释的构造函数和getter / setter生成SLF4J-测井,注入LombokHibernate-数据访问和对象关系映射Spring Boot-依赖注入和Web控制器建筑学可以通过HTTP...

    Spring攻略PDF版

     1.3 应用控制反转和依赖注入   1.3.1 问题描述   1.3.2 解决方案   1.3.3 实现方法   1.4 理解不同类型的依赖注入   1.4.1 问题描述   1.4.2 解决方案   1.4.3 实现方法   1.5 ...

    25个经典的Spring面试问答

    什么是Spring框架Spring框架有哪些主要模块 ...构造方法注入和设值注入有什么区别 Spring框架中有哪些不同类型的事件 FileSystemResource和ClassPathResource有何区别 Spring 框架中都用到了哪些设计模式

    Spring攻略中文版PDF

     1.3 应用控制反转和依赖注入   1.3.1 问题描述   1.3.2 解决方案   1.3.3 实现方法   1.4 理解不同类型的依赖注入   1.4.1 问题描述   1.4.2 解决方案   1.4.3 实现方法   1.5 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    springday02

    DI依赖注入,通过set方法注入和通过构造方法注入的练习

    Spring攻略英文版(附带源码)

    Spring专家力作 理论与实践完美结合 问题描述→解决方案→实现方法 第一部分 核心概念  第1章 控制反转和容器   1.1 使用容器管理组件   1.1.1 问题描述   1.1.2 解决方案   1.1.3 实现方法   1.2...

    CH02-IOC依赖注入.pptx

    理解什么是IoC和DI。 理解构造注入 理解不同数据类型的注入方法 掌握p命名空间注入 Bean自动装配

    Spring.html

    构造器注入&lt;construct-arg&gt; set注入 生命周期 scope:prototype/singleton init-method destroy-method API BeanFactory:使用这个工厂创建对象的方式都是懒加载,在调用的时候再创建 ...

    Spring 3 Reference中文

    4.4.1.1 基于构造方法的依赖注入 39 4.4.1.2 基于setter 方法的依赖注入 41 4.4.1.3 解决依赖过程.. 42 4.4.1.4 依赖注入示例.. 43 4.4.2 深入依赖和配置.. 45 4.4.2.1 直接值(原生类型...

Global site tag (gtag.js) - Google Analytics