`
DavyJones2010
  • 浏览: 146839 次
  • 性别: 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

 

分享到:
评论

相关推荐

    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

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

    依赖注入:Dependency injection-英文书签文字版 全面讲解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;

    Spring学习 关于dependency-check示例

    Spring学习 关于dependency-check示例代码,需要自行下载Spring3相关jar包

    Pro Spring 5 An In-Depth Guide -Apress(2017)

    Covering version 5 of the Spring Framework, this book is the most comprehensive Spring reference and practical guide available for harnessing the power of this leading enterprise Java application ...

    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 ...

    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 ...

    Dependency Injection in NET

    Dependency Injection in .NET presents core DI patterns in plain C# so you'll fully understand

    Dependency injection in action

    This book is for general principle of dependency injection in software architecturing

    Dependency Injection in .NET

    Summary Dependency Injection in .NET presents core DI patterns in plain C#, so you'll fully understand how DI works, covers integration with standard Microsoft technologies like ASP.NET MVC, and ...

    SpringSecurity.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    Dependency Injection in EJB 3

    Dependency Injection in EJB 3

    Dependency Injection in .NET 无水印pdf

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

    SpringMVC3.1.2 入门级HelloWorld源码

    (maven查看的dependency) [INFO] SpringStart:SpringStart:war:V1.0.0 [INFO] +- log4j:log4j:jar:1.2.17:compile [INFO] +- org.apache.openejb:javaee-api:jar:5.0-1:provided [INFO] +- javax.faces:jsf-api:jar:...

    Dependency Injection In Action

    It explores Dependency Injection, sometimes called Inversion of Control, in fine detail with numerous practical examples. Developers will learn to apply important techniques, focusing on their ...

    Dependency Injection in .NET Core 2.0

    Dependency Injection in .NET Core 2.0

    Apache Maven Dependency Management

    Chapter 3: Dependency Designation (advanced) Chapter 4: Migration of Dependencies to Apache Maven Chapter 5: Tools within Your IDE Chapter 6: Release and Distribute Appendix: Useful Public ...

    spring framework4

    The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is ...

    Spring Security、Spring Social 、Spring Security OAuth

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

Global site tag (gtag.js) - Google Analytics