Introduction:
In this article, we focus on the configuration file beans.xml for IoC purpose.
Focusing on initialize beans whose property are collection types.
Including <list/> <set/> <map/> and <props/> elements corrosponding to List, Set, Map and Properties in JAVA.
1. Four common collection types
1) <list>
<?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="scores"> <list> <value>123</value> <value>133</value> <value>132</value> </list> </property> <property name="addresses"> <list> <bean class="edu.xmu.domain.Address"> <property name="country" value="China"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="Canada"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="America"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="Austrilia"></property> </bean> </list> </property> </bean> </beans>
2) <map>
<?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="addressMap"> <map> <entry key="homeAddress" value-ref="homeAddress"> </entry> <entry key="workAddress" value-ref="workAddress"> </entry> </map> </property> <property name="scoreMap"> <map> <entry key="english" value="135"></entry> <entry key="math" value="145"></entry> <entry key="chinese" value="118"></entry> </map> </property> </bean> <bean id="homeAddress" class="edu.xmu.domain.Address"> <property name="country" value="china"> </property> </bean> <bean id="workAddress" class="edu.xmu.domain.Address"> <property name="country" value="america"> </property> </bean> </beans>
3) <set>
<?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="addressSet"> <set> <ref bean="homeAddress" /> <ref bean="workAddress" /> </set> </property> <property name="scoreSet"> <set> <value>122</value> <value>123</value> <value>231</value> </set> </property> </bean> <bean id="homeAddress" class="edu.xmu.domain.Address"> <property name="country" value="china"> </property> </bean> <bean id="workAddress" class="edu.xmu.domain.Address"> <property name="country" value="america"> </property> </bean> </beans>
4) <props>
Just like previous introduction. Not usually used.
2. Inner beans introduction
<bean id="student" class="edu.xmu.domain.Student"> <!-- instead of using a reference to a target bean, simply define the target bean inline --> <property name="workAddress"> <!-- This is the inner bean --> <bean class="edu.xmu.domain.Address"> <property name="country" value="China"/> <property name="province" value="Shanghai"/> </bean> </property> </bean>
Comments:
1) A <bean/>
element inside the <property/>
or <constructor-arg/>
elements defines a so-called inner beans.
2) Inner beans are always anonymous and they are always scoped as prototypes.
相关推荐
3. 接口注入:Spring提供了一个接口`ApplicationContextAware`,实现这个接口的类可以获取到Spring应用上下文,从而从中获取依赖对象。这是一种间接的注入方式,通常用于获取Spring容器本身。 Spring框架的依赖注入...
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 Injection, DI)是至关重要的。而`dependency-check`在Spring中指的是依赖检查,它是一种确保应用程序在运行时能够正确处理依赖关系的机制。本示例将围绕...
<dependency> <groupId>org.springframework.boot <artifactId>spring-boot-configuration-processor <version>2.2.5.RELEASE </dependency>
标题《Dependency injection in action》所指的知识点是依赖注入的实践应用,这一概念属于软件架构设计的基本原则。依赖注入是一种编程技术,通过这种方式,对象可以不直接创建或者查找其依赖的其他对象,而是在运行...
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 ...
Part3:Spring的高级特性和实战应用 这部分进一步探讨了Spring的高级特性,包括Spring的数据访问层支持,如JDBC抽象、ORM集成(如Hibernate、MyBatis),以及Spring Data项目,它简化了与各种数据存储的交互。此外,...
maven新建了一个工程,但总是找不到依赖,也就是依赖不能自动下载,即下面情况 idea上面配置maven有两种方法,一种是用idea自带的maven,另一种是自己下载的maven,我试了两种都行不通,依旧是上图的情况 ...
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框架中,依赖注入(Dependency Injection,简称DI)是一种重要的设计模式,它使得对象之间的耦合度降低,提高了代码的可测试性和可维护性。本文将深入探讨Spring中的依赖注入实现方式,以及如何通过样例文件...
《Spring in Action》是关于Spring框架的一本经典书籍,它深入浅出地介绍了Spring的核心概念和技术。这个"spring in action的jar包"很可能是书中提到的一些必要的库文件,用于配合书中的实例代码运行。这些jar包是...
Spring框架是Java开发中不可或缺的一部分,它以其Inversion of Control(IoC)和Dependency Injection(DI)特性而闻名。在“Spring基础:IoC容器(1)”这个主题中,我们将深入探讨Spring的核心组件——IoC容器,以及...
依赖注入(Dependency Injection, DI):Spring的核心特性之一,通过IoC(Inversion of Control)和AOP(Aspect Oriented Programming)实现对象之间的松耦合,降低组件间的复杂性和维护成本。 Spring MVC:用于...
《Spring in Action》是Spring框架领域的一本经典著作,它以深入浅出的方式介绍了Spring框架的核心概念和技术。这本书的中文版对于中国的Java开发者来说是一份非常宝贵的资源,它帮助我们理解并掌握Spring框架,从而...
Dependency Injection in .NET 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
《Spring in Action 3.0》是一本针对Spring框架详细讲解的权威指南,主要面向有经验的Java开发者。这本书深入浅出地介绍了如何利用Spring框架构建高质量的企业级应用。Spring框架是Java EE开发中的核心组件,它简化...
6. 依赖管理和配置(Dependency Management and Configuration):Spring的依赖注入是其核心特性之一,能够通过控制反转(IoC)原则减少组件之间的耦合。通过使用Spring管理依赖,开发者可以轻松地构建、测试和修改...
《Spring in Action》是关于Spring框架的一本权威指南,特别是第四版,它深入浅出地介绍了如何使用Spring框架构建高效、灵活的Java应用。这本书涵盖了从基础到高级的多个Spring核心概念和技术,包括依赖注入、AOP...