一.spring Ioc容器补充(1)
Spring Ioc容器 DI(依赖注入):
注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入field(注解)/接口注入out
装配的方式:手动装配<property>,<constructor-arg>,@Autowired,@Resource/自动装配 1.装配各种类型的属性:
A、简单属性value属性或者value元素 <bean id="person" class="com.lspring.step2.Person">
<property name="name" value="liyy"></property>
<!-- property元素中的内容叫属性值,会自动把value描述的值转换成对应属性的类型 -->
<property name="age" >
<value>30</value>
</property>
<property name="tel"><value>ABCD</value></property>
<!-- 也可以使用value装配一些Spring支持的类型 -->
<property name="homePage"><value>http://google.com</value></property>
</bean> B、引用其它bean,使用ref属性或者ref标签
<!--ref引用其它的bean,local表示引用本容器中的bean,parent表示引用父容器中的某个bean,bean表示先在当前容器中找,找不到再到父容器中找 -->
<property name="parent">
<ref bean="person_c"></ref>
</property> C、使用内部bean <property name="parent">
<bean class="com.lspring.step2.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg>
<value>33</value>
</constructor-arg>
</bean>
</property> D、使用集合/装配集合
1).数组
代码:
private String[] favs; public String[] getFavs() {
return favs;
} public void setFavs(String[] favs) {
this.favs = favs;
} xml配置:
<!-- 装配数组 -->
<property name="favs">
<array>
<value>足球</value>
<value>篮球</value>
<value>排球</value>
</array>
</property> 2).装配list
private List<String> school; public List<String> getSchool() {
return school;
} public void setSchool(List<String> school) {
this.school = school;
} <!-- 装配list集合 -->
<property name="school">
<list>
<value>中华第一小学</value>
<value>中华第一高中</value>
<value>中华第一大学</value>
</list>
</property> 3).装配set 不接受重复的值,只显示一条重复的传下
private Set<String> cities; public Set<String> getCities() {
return cities;
} public void setCities(Set<String> cities) {
this.cities = cities;
}
<property name="cities">
<set>
<value>shanghai</value>
<value>shanghai</value>
<value>shanghai</value>
</set>
</property> E.装配Map private Map<String, Double> scores; public Map<String, Double> getScores() {
return scores;
} public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
<!-- 装配Map -->
<property name="scores">
<map>
<entry key="语文" value="50"></entry>
<entry key="外文" value="30"></entry>
<!-- 也可以使用key-ref,和value-ref来指向其他的bean -->
</map>
</property> F.装配properties
private Properties properties; public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} <!-- 装配属性类型 -->
<!-- <property name="properties"> <props> <prop key="qq">1213343</prop>
<prop key="msn">kkk@qqq.com</prop> </props> </property> -->
<!-- 在value中直接使用值对作为属性内容 -->
<property name="properties">
<value>
qq=133234
msn=1312@dfjk.com
</value>
</property> G.装配空值
private Integer age=25;
默认给age赋初值 <bean id="person_null" class="com.lspring.step2.Person">
<property name="name" value="小哥李"></property>
<property name="age">
<!-- 使用nul标签来指定空值。 -->
<null></null>
</property>
</bean>

代码如下:Person.java PersonDao.java

package com.lspring.step2;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector; public class Person {
private String name;
private Integer age=;
private String tel;
private Person parent;
private String[] favs;
private List<String> school = new Vector();
private Set<String> cities;
private Properties properties; public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} private Map<String, Double> scores; public Map<String, Double> getScores() {
return scores;
} public void setScores(Map<String, Double> scores) {
this.scores = scores;
} public Set<String> getCities() {
return cities;
} public void setCities(Set<String> cities) {
this.cities = cities;
} public List<String> getSchool() {
return school;
} public void setSchool(List<String> school) {
this.school = school;
} public String[] getFavs() {
return favs;
} public void setFavs(String[] favs) {
this.favs = favs;
} private URL homePage; public URL getHomePage() {
return homePage;
} public void setHomePage(URL homePage) {
this.homePage = homePage;
} public String getName() {
return name;
} public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public Person() {
super();
System.out.println("初始化person!");
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getTel() {
return tel;
} public void setTel(String tel) {
this.tel = tel;
} public Person getParent() {
return parent;
} public void setParent(Person parent) {
this.parent = parent;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", tel=" + tel + ", parent=" + parent + ", favs=" + Arrays.toString(favs) + ", school=" + school + ", cities=" + cities + ", properties=" + properties + ", scores=" + scores + ", homePage=" + homePage + "]";
}
}
package com.lspring.step2;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class PersonDao {
@Test
public void injectTest(){
@SuppressWarnings("resource")
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_step2.xml");
System.out.println(applicationContext.getBean("person"));
System.out.println(applicationContext.getBean("person_c"));
Person person = (Person) applicationContext.getBean("person");
//Spring 会把集合类型,初始化时统一使用某种类型
System.out.println(person.getSchool().getClass());
System.out.println("person_null:"+applicationContext.getBean("person_null"));
} }

  xml文件如下:beans_step2.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-3.2.xsd">
<bean id="person_c" class="com.lspring.step2.Person">
<constructor-arg>
<value>Tom</value>
</constructor-arg>
<constructor-arg value="22"></constructor-arg>
<property name="parent">
<bean class="com.lspring.step2.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg>
<value>33</value>
</constructor-arg>
</bean>
</property>
</bean> <bean id="person" class="com.lspring.step2.Person">
<property name="name" value="liyy"></property>
<!-- property元素中的内容叫属性值,会自动把value描述的值转换成对应属性的类型 -->
<property name="age">
<value>30</value>
</property>
<property name="tel">
<value>ABCD</value>
</property>
<!-- 也可以使用value装配一些Spring支持的类型 -->
<property name="homePage">
<value>http://google.com</value>
</property>
<!-- 1. <property name="parent" ref="person_c"></property>=2. <property
name="parent"><ref bean="person_c"/></property> -->
<!--ref引用其它的bean,local表示引用本容器中的bean,parent表示引用父容器中的某个bean,bean表示先在当前容器中找,找不到再到父容器中找 -->
<property name="parent">
<ref bean="person_c"></ref>
</property>
<!-- 装配数组 -->
<property name="favs">
<array>
<value>足球</value>
<value>篮球</value>
<value>排球</value>
</array>
</property>
<!-- 装配list集合 -->
<property name="school">
<list>
<value>中华第一小学</value>
<value>中华第一高中</value>
<value>中华第一大学</value>
</list>
</property>
<!-- 装配set集合 -->
<property name="cities">
<set>
<value>shanghai</value>
<value>shanghai</value>
<value>shanghai</value>
</set>
</property>
<!-- 装配Map -->
<property name="scores">
<map>
<entry key="语文" value="50"></entry>
<entry key="外文" value="30"></entry>
<!-- 也可以使用key-ref,和value-ref来指向其他的bean -->
</map>
</property>
<!-- 装配属性类型 -->
<!-- <property name="properties"> <props> <prop key="qq">1213343</prop>
<prop key="msn">kkk@qqq.com</prop> </props> </property> -->
<!-- 在value中直接使用值对作为属性内容 -->
<property name="properties">
<value>
qq=133234
msn=1312@dfjk.com
</value>
</property>
</bean>
<bean id="person_null" class="com.lspring.step2.Person">
<property name="name" value="小哥李"></property>
<property name="age">
<!-- 使用nul标签来指定空值。 -->
<null></null>
</property>
</bean>
</beans>

[Spring学习笔记 2 ]装配各种类型的属性 map,list,array,null,properties的更多相关文章

  1. Spring学习笔记--自动装配Bean属性

    Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...

  2. Spring学习笔记-高级装配-03

    主要内容: ●Spring profile ●条件化的bean声明 ●自动装配与歧义性 ● Spring表达式语言 本章介绍一些高级的装配技术,可实现更为高级的装配功能. 环境与profile 软件开 ...

  3. Linux 程序设计学习笔记----Linux下文件类型和属性管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38318225 部分内容整理自网络,在此感谢各位大神. Linux文件类型和权限 数据表示 文件 ...

  4. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  5. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  6. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  7. Spring学习笔记(一)

    Spring学习笔记(一) 这是一个沉淀的过程,大概第一次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了一个很 ...

  8. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  9. 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat

    作者:ssslinppp       1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...

随机推荐

  1. R 分组计算描述性统计量

    统计学区内各个小区的房价均值 数据格式 id|community_name|house_area|house_structure|house_total|house_avg|agency_name|h ...

  2. [转]学习块格式化上下文(BlockFormattingContext)

    原文:https://www.cnblogs.com/elcarim5efil/p/4745796.html   格式化上下文 格式化上下文( formatting contexts )├── 块级格 ...

  3. installers PHPManager

    === Verbose logging started: // :: Build type: SHIP UNICODE 5.00.10011.00 Calling process: C:\Progra ...

  4. Wifidog初分析

    一.综述 wifidog是搭建无线热点认证系统的解决方案之一,他比nocat.nodog更适合互联网营销思路.常见的使用在openwrt系统上,它实现了路由器和认证服务器的数据交互,在路由器方(客户端 ...

  5. Tone Mapping算法系列一:基于Fast Bilateral Filtering 算法的 High-Dynamic Range(HDR) 图像显示技术。

    一.引言 本人初次接触HDR方面的知识,有描述不正确的地方烦请见谅. 为方便文章描述,引用部分百度中的文章对HDR图像进行简单的描述. 高动态范围图像(High-Dynamic Range,简称HDR ...

  6. NameNode的ZKFC机制

    转自: http://hackershell.cn/?p=821 NameNode的HA可以个人认为简单分为共享editLog机制和ZKFC对NameNode状态的控制 在此之前,我先提几个问题: 一 ...

  7. 微信小程序 - 深度定义骨架屏(提示)

    此举每个页面必须创建对应的css样式,比较麻烦(但非常准确),推荐使用组件化的skeleton组件 原理很简单:知晓一下this.setData原理,就OK了,可能大家会因此了解到全屏加载loadin ...

  8. 重设域管理员密码-window server 2008 R2

    How to Reset Your Forgotten Domain Admin Password on Server 2008 R2 Forgetting your password is alwa ...

  9. JAVA设计模式(全部)

    一篇一篇的重写意义不大,不如把整个PDF文档上传上来看着方便,下载链接

  10. intellij idea 插件安装、卸载

    windows 下 intellij idea 插件安装.卸载   安装(在线安装): 根据图一.图二所示(蓝色标记) 卸载: 根据图一所示(橙色标记) 启用.关闭插件: 根据图一所示(绿色标记) 安 ...