Spring3.2.8 注入
1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
2.1构造方法注入
构造器注入可以根据参数索引注入、参数类型注入,使用constructor-arg
使用构造器注入通过配置构造器参数实现,构造器参数就是依赖。除了构造器方式,还有静态工厂、实例工厂方法可以进行构造器注入。
基本注入
按构造器参数类型注入
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>
2.1.1注入日期
public class Student { private int age; private Date birth; Student(int age, Date birth) { this.age = age; this.birth = birth; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } }
声明一个 CustomDateEditor 类将字符串转换成 java.util.Date。
<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> </constructor-arg> <constructor-arg value="true" /> </bean>
声明另一个“CustomEditorConfigurer”,使 Spring转换 bean 属性,其类型为java.util.Date
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <ref local="dateEditor" /> </entry> </map> </property> </bean>
注入:
<bean id="student" class="com.spring.Student"> <constructor-arg type="int" value="24"></constructor-arg> <constructor-arg index="1" value="2017-04-25"></constructor-arg> </bean>
2.2集合注入
集合元素 |
用途 |
<list> | 装配list类型的值,允许重复 |
<set> | 装配set类型的值,不允许重复 |
<map> | 装配map类型的值,名称和值可以是任意类型 |
<props> | 装配<properties>类型的值,名称和值必须是string类型 |
例用<list>来装配集合与数组
Student类
package com.spring.collection; public class Student { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
Person类中加入student的引用集合,String数组xh
package com.spring.collection; import java.util.List; public class Person { private List<Student> student; private String[] xh; public void setStudent(List<Student> student) { this.student = student; } public List<Student> getStudent() { return student; } public String[] getXh() { return xh; } public void setXh(String[] xh) { this.xh = xh; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <bean id="student1" class="com.spring.collection.Student" p:name="张三" /> <bean id="student2" class="com.spring.collection.Student" p:name="张三2" /> <bean id="student3" class="com.spring.collection.Student" p:name="张三3" /> <bean id="person" class="com.spring.collection.Person"> <property name="student"> <list> <ref bean="student1" /> <ref bean="student2" /> </list> </property> <property name="xh"> <list> <value>1001</value> <value>1002</value> </list> </property> </bean> </beans>
注入Map
Student类同上。
Person类
public class Person { private Map<String,Student> mapStudent; public Map<String, Student> getMapStudent() { return mapStudent; } public void setMapStudent(Map<String, Student> mapStudent) { this.mapStudent = mapStudent; } }
配置文件
<bean id="person" class="com.spring.collection.Person"> <property name="mapStudent"> <map> <entry key="1001" value-ref="student1"></entry> <entry key="1002" value-ref="student2"></entry> </map> </property> </bean>
测试<list><map>类
public class DICollectionTest { public static void main(String[] args) { ApplicationContext ap = new ClassPathXmlApplicationContext("com/spring/collection/bean.xml"); Person p = (Person)ap.getBean("person"); String[] xh = p.getXh(); System.out.println(xh[0] +"--"+xh[1]); List<Student> students = p.getStudent(); for(Student student:students) { System.out.println(student.getName()); } Map<String,Student> mapStudent = p.getMapStudent(); Iterator<Entry<String,Student>> it = mapStudent.entrySet().iterator(); while(it.hasNext()) { Entry<String,Student> entr= it.next(); System.out.println(entr.getKey() + "--" +entr.getValue()); } } }
相关推荐
### Spring学习笔记(精华全记录) #### Spring框架概述 Spring框架源自Rod Johnson的个人项目,最初于2002年末发布。Spring并非一开始就作为一个完整的框架出现,而是从一个项目逐步发展而来。随着项目的成熟,...
这份"Spring学习笔记+学习源码.zip"资源包含了深入学习Spring及其相关技术的知识点,以及实践代码,对提升Spring技能将大有裨益。 首先,我们来详细讨论Spring框架的主要组件和功能: 1. **依赖注入(Dependency ...
依赖注入是Spring框架的核心特性之一,它允许开发者在运行时将对象及其依赖关系解耦。通过DI,组件不再直接创建它所依赖的对象,而是由容器负责创建和管理这些对象,并将它们注入到需要的地方。这有助于提高代码的可...
本资料“Spring学习笔记&源码”是基于网易云课堂黑马程序员的Spring四天精通课程,旨在帮助学习者深入理解和实践Spring框架。 笔记部分可能会涵盖以下内容: 1. **Spring概述**:介绍Spring框架的历史、特点和主要...
本文档记录了尚学堂Spring学习笔记的重要知识点,涵盖了Spring配置文件的设置、普通属性的注入、自定义属性编辑器、公共属性的注入、Spring注解等内容。 一、Spring配置文件的设置 在MyEclipse中编写Spring配置...
这份"Spring框架学习笔记"涵盖了Spring框架的基础知识、核心组件以及高级特性,对于初学者来说是一份宝贵的资料。 一、Spring框架概述 Spring框架是为了解决企业应用开发的复杂性而设计的,它提供了一个全面的基础...
在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...
在本篇“Spring学习笔记之二‘属性注入’”中,我们将深入探讨Spring如何实现属性注入,并通过实例展示其工作原理。 1. **属性注入的概念** 属性注入是指Spring容器通过XML配置或者基于注解的方式,将外部资源(如...
### Spring学习笔记知识点详解 #### 一、Spring框架概述 **Spring** 是一个开源的、分层的企业级应用开发框架,旨在简化Java EE应用程序的开发。它的主要目标是提高开发效率,减少耦合度,并提供一种更为简洁的...
### Spring学习笔记知识点详解 #### 一、Spring框架概述 **1.1 什么是Spring** Spring框架是一个开源的轻量级应用框架,主要用于简化企业级应用程序的开发过程。它的核心特性在于提供了一种灵活的方式来组织和...
本篇学习笔记将深入剖析Spring依赖注入的原理,通过源码分析帮助我们理解这一核心机制。 首先,依赖注入允许我们解耦组件之间的关系,使得各个组件可以独立地进行开发、测试和维护。在Spring中,DI主要通过两种方式...
标题和描述均提到了“spring指南学习笔记”,这意味着文档聚焦于Spring框架的学习心得与关键概念。Spring是一个开源的Java企业级应用框架,以其强大的依赖注入(Dependency Injection, DI)和面向切面编程(Aspect ...
总结来说,Spring的依赖注入机制是其核心特性之一,它极大地提升了代码的可测试性和可维护性。通过对依赖的解耦,开发者可以更专注于业务逻辑,而不是对象的创建和管理。在实际项目中,结合使用构造器和setter注入,...
在本篇Spring学习笔记中,我们将深入探讨Spring的基本概念,包括bean的配置、依赖注入、IOC容器以及Bean的实例化方式。 首先,Spring中的核心概念是bean。Bean是Spring框架中的基本构建块,它们是被管理的对象,...
本篇学习笔记主要探讨了Spring中的属性注入方式,包括了传统的XML配置注入、注解式注入以及使用Java配置类的方式。 一、XML配置注入 在Spring早期版本中,XML配置文件是定义Bean及其依赖关系的主要方式。属性注入...
Spring学习笔记总结 Spring是一个基于Java的框架,它提供了一种简洁、灵活的方式来构建企业级应用程序。在这个笔记中,我们将总结Spring的主要概念和技术,包括IOC、AOP、MVC、Struts2和Hibernate的集成。 IOC...
这篇"Spring学习笔记之一“why spring”"可能探讨了为何开发者会选择Spring作为他们的技术栈。让我们深入了解一下Spring框架的核心优势和特性。 首先,Spring是轻量级的。尽管它提供了众多功能,但核心容器(如IoC...
Spring学习笔记2涵盖了Spring框架的核心概念和重要特性,旨在帮助开发者深入理解并熟练掌握Spring的使用。 1. **依赖注入(Dependency Injection, DI)**:这是Spring最核心的设计原则,它允许对象之间的依赖关系在...
Spring框架是Java应用开发中广泛使用的轻量级框架,它以IoC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)为核心,提供了丰富的功能,包括但不限于组件管理、AOP(Aspect Oriented ...