`
xxp3369
  • 浏览: 151310 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring_injection

阅读更多
1、spring的普通属性注入
参见:spring文档3.3章节

什么是属性编辑器,作用?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器

* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
* 将属性编辑器注册到spring中,参见:applicationContext-editor.xml

依赖对象的注入方式,可以采用:
* ref属性
* <ref>标签
* 内部<bean>来定义

如何将公共的注入定义描述出来?
* 通过<bean>标签定义公共的属性,指定abstract=true
* 具有相同属性的类在<bean>标签中指定其parent属性

参见:applicationContext-other.xml



applicationContext-other.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"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  
   <bean id="beanAbstract" abstract="true">
   <property name="id" value="1000"/>
   <property name="name" value="Jack"/>
   </bean>        
  
   <bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
   <property name="name" value="Tom"/>
   <property name="password" value="123"/>
   </bean>       
  
   <bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>

applicationContext-beans.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"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="bean1" class="com.bjsxt.spring.Bean1">
<property name="strValue" value="Hello"/>
<!--
<property name="intValue" value="123"/>
-->
<property name="intValue">
<value>123</value>
</property>
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</map>
</property>
<property name="dateValue">
<value>2008-08-15</value>
</property>
</bean>

<bean id="bean2" class="com.bjsxt.spring.Bean2">
<property name="bean3" ref="bean3"/>
<property name="bean4">
<ref bean="bean4"/>
</property>
<property name="bean5" ref="bean5"/>
</bean>

<!--
<bean id="bean3" class="com.bjsxt.spring.Bean3">
<property name="id" value="1000"/>
<property name="name">
<value>Jack</value>
</property>
<property name="password" value="123"/>
</bean>

<bean id="bean4" class="com.bjsxt.spring.Bean4">
<property name="id" value="1000"/>
<property name="name" value="Jack"/>
</bean>
-->

<bean id="bean5" class="com.bjsxt.spring.Bean5">
<property name="age" value="20"/>
</bean>
</beans>


applicationContext-editor.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"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <!-- 定义属性编辑器 -->     
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.bjsxt.spring.UtilDatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>

<!--
<bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean>
-->
</beans>


log4j.properties


# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n


UtilDatePropertyEditor.java


package com.bjsxt.spring;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * java.util.Date属性编辑器 
 * @author Administrator
 *
 */
public class UtilDatePropertyEditor extends PropertyEditorSupport {

	private String format="yyyy-MM-dd";
	
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date d = sdf.parse(text);
			this.setValue(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setFormat(String format) {
		this.format = format;
	}

}


Bean1.java

package com.bjsxt.spring;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Bean1 {
	
	private String strValue;
	
	private int intValue;
	
	private List listValue;
	
	private Set setValue;
	
	private String[] arrayValue;
	
	private Map mapValue;
	
	private Date dateValue;

	public String getStrValue() {
		return strValue;
	}

	public void setStrValue(String strValue) {
		this.strValue = strValue;
	}

	public int getIntValue() {
		return intValue;
	}

	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}

	public List getListValue() {
		return listValue;
	}

	public void setListValue(List listValue) {
		this.listValue = listValue;
	}

	public Set getSetValue() {
		return setValue;
	}

	public void setSetValue(Set setValue) {
		this.setValue = setValue;
	}

	public String[] getArrayValue() {
		return arrayValue;
	}

	public void setArrayValue(String[] arrayValue) {
		this.arrayValue = arrayValue;
	}

	public Map getMapValue() {
		return mapValue;
	}

	public void setMapValue(Map mapValue) {
		this.mapValue = mapValue;
	}

	public Date getDateValue() {
		return dateValue;
	}

	public void setDateValue(Date dateValue) {
		this.dateValue = dateValue;
	}
}


Bean2.java

package com.bjsxt.spring;

public class Bean2 {

	private Bean3 bean3;
	
	private Bean4 bean4;
	
	private Bean5 bean5;

	public Bean3 getBean3() {
		return bean3;
	}

	public void setBean3(Bean3 bean3) {
		this.bean3 = bean3;
	}

	public Bean4 getBean4() {
		return bean4;
	}

	public void setBean4(Bean4 bean4) {
		this.bean4 = bean4;
	}

	public Bean5 getBean5() {
		return bean5;
	}

	public void setBean5(Bean5 bean5) {
		this.bean5 = bean5;
	}
}


Bean3.java

package com.bjsxt.spring;

public class Bean3 {
	
	private int id;
	
	private String name;
	
	private String password;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}


Bean4.java

package com.bjsxt.spring;

public class Bean4 {

	private int id;
	
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}


Bean5.java

package com.bjsxt.spring;

public class Bean5 {
	
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}


InjectionTest.java

package com.bjsxt.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class InjectionTest extends TestCase {
	
	private BeanFactory factory;
	
	@Override
	protected void setUp() throws Exception {
		factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");	
	}

	public void testInjection1() {
		Bean1 bean1 = (Bean1)factory.getBean("bean1");
		
		System.out.println("bean1.strValue=" + bean1.getStrValue());
		System.out.println("bean1.intValue=" + bean1.getIntValue());
		System.out.println("bean1.listValue=" + bean1.getListValue());
		System.out.println("bean1.setValue=" + bean1.getSetValue());
		System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
		System.out.println("bean1.mapValue=" + bean1.getMapValue());
		System.out.println("bean1.dateValue=" + bean1.getDateValue());
	}
	
	public void testInjection2() {
		Bean2 bean2 = (Bean2)factory.getBean("bean2");
		
		System.out.println("bean2.bean3.id=" + bean2.getBean3().getId());
		System.out.println("bean2.bean3.name=" + bean2.getBean3().getName());
		System.out.println("bean2.bean3.password=" + bean2.getBean3().getPassword());
		System.out.println("bean2.bean4.id=" + bean2.getBean4().getId());
		System.out.println("bean2.bean4.name=" + bean2.getBean4().getName());
		System.out.println("bean2.bean5.age=" + bean2.getBean5().getAge());
	}
	
}
分享到:
评论

相关推荐

    spring_injection1

    标题“spring_injection1”可能指的是Spring框架中的依赖注入(Dependency Injection,简称DI)概念的讲解,这在Spring框架中是核心特性之一。这个压缩包可能包含了一些关于Spring DI的源代码示例或者教程文档。 ...

    3. spring_injection

    spring_injection" 暗示了我们即将探讨的是Spring框架中的依赖注入(Dependency Injection,简称DI)机制。依赖注入是Spring的核心特性之一,它极大地简化了Java应用程序的构建,使得组件之间松耦合,提高了代码的...

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] ...injection1] [spring_injection2] [spring_scope] [spring_struts_1] [spring_struts_2] [spring_struts_hibernate] [spring_whyspring] [ssh_training_itemmgr]

    尚学堂_Spring_0300_IOC_Injection_Type

    标题中的"Spring_0300_IOC_Injection_Type"指的是Spring框架中的依赖注入(Dependency Injection,简称DI)机制,特别是关于类型注入(Type Injection)的知识点。在Spring框架中,依赖注入是核心特性之一,它使得...

    Spring_0300_IOC_Injection_Type 构造器注入

    依赖注入(Dependency Injection,DI)是实现IOC的一种方式,它允许组件通过构造器、setter方法或接口来接收它们的依赖项,而不是自己去寻找或创建。 二、构造器注入 构造器注入是在创建对象时,通过构造函数传递...

    spring_day01

    1. **IoC(Inversion of Control,控制反转)**:Spring框架的核心特性之一,它通过依赖注入(Dependency Injection, DI)来管理对象的生命周期和依赖关系。IoC使得开发者无需在代码中手动创建和管理对象,而是由...

    spring_mybatis框架myeclipse8.5版本

    在Spring中,依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)是两大核心特性。依赖注入帮助我们解耦组件,提高代码的可测试性和可维护性。而AOP则允许我们在不修改原有...

    Spring_Struts_Ibatis_Mysql Demo

    **Spring框架**是Java企业级应用开发的核心框架,提供了依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)等特性。在本示例中,Spring主要负责管理各个组件的生命周期,以及...

    spring_day05_spring_

    1. **依赖注入(Dependency Injection, DI)**:这是Spring的核心特性,允许对象之间的依赖关系通过外部容器进行管理,而不是在代码内部硬编码。这提高了代码的可测试性和可维护性。 2. **IoC容器**:IoC(Inversion ...

    spring_day06_spring_

    在IT行业中,Spring是一个极其重要的Java应用程序开发框架,它以其依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)特性著称。Spring框架的核心包括容器、数据访问/集成、...

    Spring_Dev_Guide

    Spring框架以其依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)为核心,简化了Java应用的开发。它提供了一个全面的编程和配置模型,可以用于各种应用类型,包括Web应用和...

    spring_api+spring_reference中文版

    1. **依赖注入(Dependency Injection,DI)**:Spring的核心特性之一,它允许开发者在运行时通过外部容器来管理对象及其依赖关系,降低了代码间的耦合度,提高了可测试性和可维护性。 2. **面向切面编程(Aspect-...

    SpringTest_springtest_spring_java_Framework_

    Spring框架是Java开发中最常用的轻量级开源框架之一,它以其强大的依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)能力而著名。"SpringTest_springtest_spring_java_...

    spring_day02_spring_

    1. **依赖注入(Dependency Injection, DI)**:理解IoC的概念,以及Spring如何通过DI管理对象的生命周期和它们之间的依赖关系。 2. **注解驱动的IoC**:学习Spring提供的各种注解,如@Component、@Service、@...

    Spring+Hibernate+Struts

    Spring的IoC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)特性是其核心,"spring_injection"可能详细阐述了如何通过XML配置或注解实现依赖注入,以及如何自定义Bean工厂和实现Bean的...

    尚学堂_Spring_0100_模拟Spring

    它的核心特性是依赖注入(Dependency Injection,DI),这使得应用程序的组件可以松散耦合,从而提高代码的可测试性和可维护性。 在这个《尚学堂_Spring_0100_模拟Spring》的学习资源中,我们可能会关注Spring框架...

    Spring笔记

    Spring框架以其依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)的核心特性而闻名,极大地提高了代码的可测试性和模块化。在描述中提到的“博文链接:...

    spring_01.zip

    Spring框架是Java开发中的核心组件,以其优秀的依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)特性,极大地简化了企业级应用的开发工作。在本篇文章中,我们将深入探讨...

Global site tag (gtag.js) - Google Analytics