Spring属性依赖注入(手动装配)

Spring属性依赖注入


Spring的属性依赖注入分为两种: 手动装配 和 自动装配。

自动装配是指Spring整合Struts后,可以实现按类型/名称/构造等自动装配。

手动装配主要有两种:

  (1)基于xml装配:构造方法,setter方法

  (2)基于注解的装配。

 

今天就来学习一下手动装配的两种方式吧!

 

一、构造方法

在Spring的配置中,构造方法的注入形式为:<constructor-arg>

一般使用的方法是<constructor-arg index="?"type="?" value="?"></constructor-arg>

下面编写User类,里面有id,name,age三个属性,编写他们的构造方法,注意,并不一定要全部参数都必须在构造方法的参数列表中!也可以只写两个,但是index和type对应就好; 然后编写xml;最后编写Test类。

/**
 * 
 */
package com.Lily.SpringLearning.e_Constructor;

/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月16日
 * @time     上午8:47:01
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */
public class User {
	private Integer id;
	private String name;
	private Integer age;
	
	
	
	
	public User(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	
	@Override
	//用toString方法来打印User的信息
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	

}

<?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">

	
	<bean id="userId" class="com.Lily.SpringLearning.e_Constructor.User" >
		<constructor-arg index="0" type="java.lang.Integer" value="1"></constructor-arg>
		<constructor-arg index="1" type="java.lang.String" value="Lily"></constructor-arg>
		<constructor-arg index="2" type="java.lang.Integer" value="18"></constructor-arg>
	</bean>
	
</beans>

 
/**
 * 
 */
package com.Lily.SpringLearning.e_Constructor;

import static org.junit.Assert.*;

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

/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月16日
 * @time     上午8:50:51
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */
public class Test {

	@org.junit.Test
	public void test() {
		String xmlPath = "com/Lily/SpringLearning/e_Constructor/beans.xml";
		ApplicationContext a=new ClassPathXmlApplicationContext(xmlPath);
		User u=(User)a.getBean("userId");
		System.out.println(u);
	}

}




二、setter方法

在Spring配置中,使用setter方法注入的形式主要有两种:

(1)普通数据的注入:  <property name="" value="值">

(2)引用数据的注入: <property name="" ref="另一个bean">

下面编写Person类,其中地址addr是引用数据,因此需编写Addr类,然后编写配置bean和测试类。

【注意,要把引用写到第一个bean内部,当做属性来写,否则会因为找不到引用而报错,或者字段打印是空】

 

/**
 * 
 */
package com.Lily.SpringLearning.e_setter;

/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月16日
 * @time     上午10:21:42
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */
public class Person {
	private String name;
	private Integer age;
	private Address homeAddr;
	private Address schoolAddr;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Address getHomeAddr() {
		return homeAddr;
	}
	public void setHomeAddr(Address homeAddr) {
		this.homeAddr = homeAddr;
	}
	public Address getSchoolAddr() {
		return schoolAddr;
	}
	public void setSchoolAddr(Address schoolAddr) {
		this.schoolAddr = schoolAddr;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", homeAddr="
				+ homeAddr + ", schoolAddr=" + schoolAddr + "]";
	}
	
	
	
}

 

/**
 * 
 */
package com.Lily.SpringLearning.e_setter;

/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月16日
 * @time     上午10:21:55
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */
public class Address {
	private String addr;
	private String mail;
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	@Override
	public String toString() {
		return "Address [addr=" + addr + ", mail=" + mail + "]";
	}
	
	
}

 

<?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">

	
	
	<bean id="person" class="com.Lily.SpringLearning.e_setter.Person">
		<property name="name" value="Lily"></property>
		<property name="age" value="18"></property>
		
		<property name="homeAddr" ref="homeAddr"></property>
		<property name="schoolAddr" ref="schoolAddr"></property>	
	</bean>
		
		<bean id="homeAddr" class="com.Lily.SpringLearning.e_setter.Address">
			<property name="addr" value="西安"></property>
			<property name="mail" value="710071"></property>
			
			
		</bean>
		
		<bean id="schoolAddr" class="com.Lily.SpringLearning.e_setter.Address">
			<property name="addr" value="南二环"></property>
			<property name="mail" value="710070"></property>
		</bean>
	
	
	
	
</beans>

 

/**
 * 
 */
package com.Lily.SpringLearning.e_setter;

import static org.junit.Assert.*;

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

/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月16日
 * @time     上午10:30:08
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */
public class Test {

	@org.junit.Test
	public void test() {
		String xmlPath = "com/Lily/SpringLearning/e_setter/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		Person person = (Person) applicationContext.getBean("person");
		
		System.out.println(person);
	}

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring提供了几种依赖注入的方式。其中一种方式是自动装配,它可以通过上下文自动寻找bean并为其配置属性。这种方式可以减少手动配置的工作量,提高开发效率。另一种方式是使用反射来调用bean的默认构造函数实例化对象,并通过set方法来注入属性值。这样可以实现bean的依赖注入。此外,Spring还提供了工厂方法的功能,可以通过工厂注入的方式来进行Spring依赖注入。工厂类可以屏蔽目标类的实例化步骤,调用者甚至不需要指定具体的目标类是什么。这种方式在一些遗留系统或第三方类库中还是会使用到。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bean自动装配,注解](https://blog.csdn.net/weixin_41709536/article/details/108609172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring框架学习教程,详解Spring注入bean的几种方式](https://blog.csdn.net/Java___interview/article/details/120550945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值