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
相关推荐
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-英文书签文字版 全面讲解Spring中的依赖注入,是英文的文字版,带书签
<dependency> <groupId>org.springframework.boot <artifactId>spring-boot-configuration-processor <version>2.2.5.RELEASE </dependency>
Spring学习 关于dependency-check示例代码,需要自行下载Spring3相关jar包
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.js是一个好用的东西。
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 ...
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 presents core DI patterns in plain C# so you'll fully understand
This book is for general principle of dependency injection in software architecturing
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 ...
它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...
Dependency Injection in EJB 3
Dependency Injection in .NET 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
(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:...
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
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 ...
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应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...