`
DavyJones2010
  • 浏览: 154230 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring3: Dependency Configuration In Detail(Part I)

阅读更多

Introduction:

    In this article, we focus on the configuration file beans.xml for IoC purpose.

 

1. A simple bean injected with simple property

<?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-3.0.xsd">

	<bean id="student" class="edu.xmu.domain.Student">
		<!-- collaborators and configuration for this bean go here -->
		<property name="id" value="1" />
	</bean>

</beans>

    1) Here we declare a simple bean whose name is student and type is edu.xmu.domain.Student.

    2) This instance has a property named id whose value we set as 1.

 

2. A simpe bean injected with constructor.

<?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-3.0.xsd">

	<bean id="student" class="edu.xmu.domain.Student">
		<constructor-arg type="Integer" value="1" />
		<constructor-arg type="String" value="Davy" />
		<constructor-arg type="Integer" value="23" />
	</bean>

</beans>

    1) The constructor-arg has to be ordered by declaration in constructor. As there is no name to identify the arg. The only reason to assign the value to a property is its type.

 

3. A simple bean injected with complex property.

        1) By refering to another bean.

<?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-3.0.xsd">

	<bean id="student" class="edu.xmu.domain.Student">
		<property name="address" ref="address"></property>
	</bean>
	
	<bean id="address" class="edu.xmu.domain.Address">
		<property name="country" value="China"></property>
		<property name="province" value="Shanghai"></property>
		<property name="city" value="Shanghai"></property>
		<property name="avenue" value="ChenhuiRD"></property>
		<property name="number" value="1000"></property>
	</bean>
	
</beans>

4. A simple bean injected with complex constructor

<?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-3.0.xsd">
	<bean id="student" class="edu.xmu.domain.Student">
		<constructor-arg type="Integer" value="1"></constructor-arg>
		<constructor-arg type="String" value="Davy"></constructor-arg>
		<constructor-arg type="Integer" value="23"></constructor-arg>
		<constructor-arg type="edu.xmu.domain.Address" ref="address1"></constructor-arg>
		<constructor-arg type="edu.xmu.domain.Address">
			<ref bean="address2"/>
		</constructor-arg>
	</bean>
	<bean id="address1" class="edu.xmu.domain.Address">
		<property name="country" value="China"></property>
	</bean>
	<bean id="address2" class="edu.xmu.domain.Address">
		<property name="country" value="Canada"></property>
	</bean>
</beans>

    1) Pay attention to the two different approaches in constructing instance

        (1) Assign as a property "ref"

        (2) Assign as a sub-element with tag <ref>

 

5. A simple bean injected with complex constructor using Factory method

<?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-3.0.xsd">
	<bean id="student" class="edu.xmu.domain.Student" factory-method="createInstance">
		<constructor-arg type="Integer" value="1"></constructor-arg>
		<constructor-arg type="String" value="Davy"></constructor-arg>
		<constructor-arg type="Integer" value="23"></constructor-arg>
		<constructor-arg type="edu.xmu.domain.Address" ref="address1"></constructor-arg>
		<constructor-arg type="edu.xmu.domain.Address">
			<ref bean="address2"/>
		</constructor-arg>
	</bean>
	<bean id="address1" class="edu.xmu.domain.Address">
		<property name="country" value="China"></property>
	</bean>
	<bean id="address2" class="edu.xmu.domain.Address">
		<property name="country" value="Canada"></property>
	</bean>
</beans>

 

	public Student(Integer id, String name, Integer age, Address address,
			Address homeAddress)
	{
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
		this.homeAddress = homeAddress;
	}

	public static Student createInstance(Integer id, String name, Integer age,
			Address address, Address homeAddress)
	{
		System.out.println("createInstance invoked");
		return new Student(id, name, age, address, homeAddress);
	}

    1) What's the purpose of using factory-method instead of simply using constructor?

        (1) The only benefit of using factory-method is return type can be sub-class of declared return type. But what's the point?

 

6. Refer to other beans

<?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-3.0.xsd">
           
    <import resource="edu/xmu/service/service.xml"/>
    <import resource="edu/xmu/dao/dao.xml"/>
</beans>

    1) Using <ref bean="***"> tag for the scope of whole xml including parent xml and children xml

    2) Using <ref local="***"> tag for the scope of only current xml

    3) Using <ref parent="***"> tag for the scope of only parent xml

 

分享到:
评论

相关推荐

    依赖注入:Dependency injection-英文书签文字版

    3. 接口注入:Spring提供了一个接口`ApplicationContextAware`,实现这个接口的类可以获取到Spring应用上下文,从而从中获取依赖对象。这是一种间接的注入方式,通常用于获取Spring容器本身。 Spring框架的依赖注入...

    Java 9 Dependency Injection.7z

    1: WHY DEPENDENCY INJECTION?...3: DEPENDENCY INJECTION WITH SPRING 4: DEPENDENCY INJECTION WITH GOOGLE GUICE 5: SCOPES 6: ASPECT-ORIENTED PROGRAMMING AND INTERCEPTORS 7: IOC PATTERNS AND BEST PRACTICES

    Spring学习 关于dependency-check示例

    在Spring框架的学习过程中,了解和应用依赖注入(Dependency Injection, DI)是至关重要的。而`dependency-check`在Spring中指的是依赖检查,它是一种确保应用程序在运行时能够正确处理依赖关系的机制。本示例将围绕...

    spring-boot-configuration-processor.2.2.5.RELEASE.zip

    &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot &lt;artifactId&gt;spring-boot-configuration-processor &lt;version&gt;2.2.5.RELEASE &lt;/dependency&gt;

    Dependency injection in action

    标题《Dependency injection in action》所指的知识点是依赖注入的实践应用,这一概念属于软件架构设计的基本原则。依赖注入是一种编程技术,通过这种方式,对象可以不直接创建或者查找其依赖的其他对象,而是在运行...

    configuration

    configuration.js是一个好用的东西。

    Spring Dynamic Modules in Action

    You will master powerful techniques like embedding a Spring container inside an OSGi bundle, and see how Spring's dependency injection compliments OSGi. Along the way, you'll learn to handle data ...

    spring in action 第二版 part1-part3

    Part3:Spring的高级特性和实战应用 这部分进一步探讨了Spring的高级特性,包括Spring的数据访问层支持,如JDBC抽象、ORM集成(如Hibernate、MyBatis),以及Spring Data项目,它简化了与各种数据存储的交互。此外,...

    Mastering Spring 5: 2nd Edition

    You'll learn about the standard dependency injection specification for Java contexts and CDI and how the Spring Framework supports it. You'll gain an understanding of how application architectures ...

    spring学习:依赖注入的几种方式讨论

    在Spring框架中,依赖注入(Dependency Injection,简称DI)是一种重要的设计模式,它使得对象之间的耦合度降低,提高了代码的可测试性和可维护性。本文将深入探讨Spring中的依赖注入实现方式,以及如何通过样例文件...

    Dependency‘org.framework:spring-webmvc:’ not found

    maven新建了一个工程,但总是找不到依赖,也就是依赖不能自动下载,即下面情况 idea上面配置maven有两种方法,一种是用idea自带的maven,另一种是自己下载的maven,我试了两种都行不通,依旧是上图的情况 ...

    spring in action的jar包

    《Spring in Action》是关于Spring框架的一本经典书籍,它深入浅出地介绍了Spring的核心概念和技术。这个"spring in action的jar包"很可能是书中提到的一些必要的库文件,用于配合书中的实例代码运行。这些jar包是...

    Spring基础:IoC容器(1)

    Spring框架是Java开发中不可或缺的一部分,它以其Inversion of Control(IoC)和Dependency Injection(DI)特性而闻名。在“Spring基础:IoC容器(1)”这个主题中,我们将深入探讨Spring的核心组件——IoC容器,以及...

    Spring in Action 中文版 pdf

    《Spring in Action》是Spring框架领域的一本经典著作,它以深入浅出的方式介绍了Spring框架的核心概念和技术。这本书的中文版对于中国的Java开发者来说是一份非常宝贵的资源,它帮助我们理解并掌握Spring框架,从而...

    spring高手之路21

    依赖注入(Dependency Injection, DI):Spring的核心特性之一,通过IoC(Inversion of Control)和AOP(Aspect Oriented Programming)实现对象之间的松耦合,降低组件间的复杂性和维护成本。 Spring MVC:用于...

    Dependency Injection in .NET 无水印pdf

    Dependency Injection in .NET 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...

    Spring in action 3.0

    《Spring in Action 3.0》是一本针对Spring框架详细讲解的权威指南,主要面向有经验的Java开发者。这本书深入浅出地介绍了如何利用Spring框架构建高质量的企业级应用。Spring框架是Java EE开发中的核心组件,它简化...

    Spring in action中文版(第4版)

    6. 依赖管理和配置(Dependency Management and Configuration):Spring的依赖注入是其核心特性之一,能够通过控制反转(IoC)原则减少组件之间的耦合。通过使用Spring管理依赖,开发者可以轻松地构建、测试和修改...

    Spring_in_Action_4.pdf-第四版-英文原版

    《Spring in Action》是关于Spring框架的一本权威指南,特别是第四版,它深入浅出地介绍了如何使用Spring框架构建高效、灵活的Java应用。这本书涵盖了从基础到高级的多个Spring核心概念和技术,包括依赖注入、AOP...

Global site tag (gtag.js) - Google Analytics