1.复制xml到container/basic包下
2.ExampleBean中设置属性,包括基本类型(String和age)和集合类型(List,Set,Map),Properties类型
3.添加无参构造器,toString方法,get/set方法
4.使用value进行基本值注入,主要基本类型和集合类型的区别
5.test1测试:对ExampleBean进行测试

ExampleBean.java:

package container.basic;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

//演示注入基本类型的值和集合类型的值
public class ExampleBean {

private String name;
private int age;
private List<String> interest;
private Set<String> cities;
private Map<String,Double> scores;
private Properties db;

@Override
public String toString() {

return "ExampleBean [age="+age+",interest="+interest+
",cities="+cities+",scores="+scores+",Properties="+db+
",name="+name+"]";

}

public ExampleBean(){
System.out.println("ExampleBean的无参构造器");
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public List<String> getInterest() {
return interest;
}

public void setInterest(List<String> interest) {
this.interest = interest;
}

public Set<String> getCities() {
return cities;
}

public void setCities(Set<String> cities) {
this.cities = cities;
}

public Map<String, Double> getScores() {
return scores;
}

public void setScores(Map<String, Double> scores) {
this.scores = scores;
}

public Properties getDb() {
return db;
}

public void setDb(Properties db) {
this.db = db;
}
}

xml:

<bean id="eb1" class="container.basic.ExampleBean">
<property name="name" value="黛玉"></property>
<property name="age" value="16"></property>
<property name="interest">
<list>
<value>snooker</value>
<value>football</value>
<value>fishing</value>
</list>
</property>
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
<value>岳阳</value>
</set>
</property>
<property name="scores">
<map>
<entry key="english" value="100"/>
<entry key="math" value="90"/>
<entry key="music" value="100"/>
</map>
</property>
<property name="db" >
<props>
<prop key="username">Tom</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>

TestCase:

@Test
public void test1(){
String cfg = "container/basic/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
ExampleBean eb1 = ac.getBean("eb1",ExampleBean.class);
System.out.println(eb1.getAge());
System.out.println(eb1.getName());
System.out.println(eb1);

}

运行结果如下:

6.同理,添加一个OtherBean类,
7.在src下面新建一个config.properties文件,当然,也可以存在别的地方
8.再修改xml文件,此时不采用上述方法配置xml,而是采用将集合当作一个bean来配置
9.test2测试OtherBean这个类,test3测试读取配置文件dbInfo

10.新建InfoBean类,实现无参构造器,toString方法,get/set方法
11.使用spring表达式来读取其他bean属性的值。即将当前infoBean属性的值设置为别的bean属性的值。
12.test4测试:对infoBean进行测试

OtherBean.java:

package container.basic;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class OtherBean {

private List<String> interest;
private Set<String> cities;
private Map<String,Double> scores;
private Properties db;
private String pageSize;

@Override
public String toString() {
return "OtherBean [interest=" + interest + ", cities=" + cities
+ ", scores=" + scores + ", db=" + db + ", pageSize="
+ pageSize + "]";
}
public OtherBean() {
System.out.println("other的无参构造器");
}
public List<String> getInterest() {
return interest;
}
public void setInterest(List<String> interest) {
this.interest = interest;
}
public Set<String> getCities() {
return cities;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public Map<String, Double> getScores() {
return scores;
}
public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}

public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}

}

config.properties:

pageSize=10

InfoBean.java:

package container.basic;

//使用spring表达式来读取bean属性值
public class InfoBean {

private String name;
private String interest;
private double score;
private String pageSize;

public InfoBean() {
System.out.println("InfoBean的无参构造器");
}

@Override
public String toString() {
return "InfoBean [name=" + name + ", interest=" + interest + ", score="
+ score + ", pageSize=" + pageSize + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}

}

修改xml,添加代码如下:

<!-- 将集合当做一个bean来配置
命名空间:为了区分同名的元素而在元素之前添加一个前缀
<%@taglib uri="" prefix=""%>
<c:if></c:if>
-->
<util:list id="interestBean">
<value>snooker</value>
<value>football</value>
<value>fishing</value>
</util:list>
<util:set id="citiesBean">
<value>北京</value>
<value>上海</value>
<value>岳阳</value>
</util:set>
<util:map id="scoresBean">
<entry key="english" value="100"/>
<entry key="math" value="90"/>
<entry key="music" value="100"/>
</util:map>
<util:properties id="dbBean">
<prop key="username">Tom</prop>
<prop key="password">1234</prop>
</util:properties>
<!-- 将指定位置的properties文件的内容读入到id为dbInfo的bean对象 -->
<util:properties id="dbInfo"
location="classpath:config.properties">

<!-- 注意,以上的Properties一种是直接设定值,一种是通过读取文件读取内容 -->
</util:properties>
<bean id="otherBean" class="container.basic.OtherBean">
<property name="interest" ref="interestBean"/>
<property name="cities" ref="citiesBean"/>
<property name="scores" ref="scoresBean"/>
<property name="db" ref="dbBean"/>
<property name="pageSize" value="#{dbInfo.pageSize}"/>
</bean>
<!-- 注意ref和value区别,若为对象,则ref,若为基本类型值,则为value -->

<!-- 使用spring表达式来读取其他bean的属性值 -->
<bean id="infoBean" class="container.basic.InfoBean">
<property name="name" value="#{eb1.name}"/>
<property name="interest" value="#{eb1.interest[1]}"/>
<property name="score" value="#{eb1.scores.english}"/>
<property name="pageSize" value="#{dbInfo.pageSize}"/>
</bean>

测试代码:

@Test
public void test1(){
String cfg = "container/basic/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
ExampleBean eb1 = ac.getBean("eb1",ExampleBean.class);
System.out.println(eb1.getAge());
System.out.println(eb1.getName());
System.out.println(eb1);

}

运行结果:

@Test
public void test2(){
String cfg = "container/basic/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
OtherBean ob = ac.getBean("otherBean",OtherBean.class);
System.out.println(ob);
}

运行结果如下:

@Test
public void test3(){
String cfg = "container/basic/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
System.out.println(ac.getBean("dbInfo"));
}

运行结果如下:

@Test
public void test4(){
String cfg = "container/basic/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
InfoBean ib = ac.getBean("infoBean",InfoBean.class);
System.out.println(ib);
}

运行结果如下:

spring day02-go3的更多相关文章

  1. Spring day02笔记

    spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...

  2. 开放源代码的设计层面框架Spring——day02

    spring第二天     一.基于注解的IOC配置         1.1写在最前             学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是 ...

  3. Spring day03笔记

    spring day02回顾 AOP :切面编程 切面:切入点 和 通知 结合 spring aop 编程 <aop:config> 方法1: <aop:pointcut expre ...

  4. Spring day01笔记

    struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解]   sprin ...

  5. spring框架入门day01

    struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少  --> [了解] spring ...

  6. Spring-Day02-依赖注入-作业

    配置beans约束自动提示 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html中打开xsd-configuratio ...

  7. [刘阳Java]_Spring常用注解介绍_第6讲

    Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置.在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为 ...

  8. [刘阳Java]_Spring相关配置介绍_第5讲

    这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...

  9. Spring Boot 配置_yaml语法介绍 day02

    一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...

  10. Spring Boot 基于Spring Initializer 的快速构建 day02

    一.基于Spring Initializr 快速构建Spring Boot项目(快速) 备注:需要联网 这是使用Intellij Idea快速构建可以为我们省去大量的pom.xml配置时间,简单操作, ...

随机推荐

  1. JVM内存状况查看方法和分析工具-jmap

    jmap -heap 27657 Attaching to process ID 27657, please wait... Debugger attached successfully. Serve ...

  2. C++ 简单 Hash容器的实现

    主要实现了以整数为关键字的hash,以key%m_nSize为哈希函数,以(hash(key)+i)%m_nSize重新寻址,并附带了elf_hash的实现,使用过程中可灵活修改. #ifndef _ ...

  3. NuGet的几个小技巧

    因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...

  4. appium testcase2

    自己跑的两个case都在盘里,可以直接解压后放到workspace,加载工程就能跑,前提是你的环境没有问题 http://pan.baidu.com/s/1bnHCyn1 eclipse-File-i ...

  5. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  6. meta的学习记录

    meta的学习记录 最近再学响应式的布局,就先来总结一下我们的viewport. 什么是viewport呢? 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(v ...

  7. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  8. 微信小程序:开发之前要知道的三件事

    前言 微信之父张小龙在年初的那次演讲中曾表示:"我自己是很多年的程序员,我觉得我们应该为开发的团队做一些事情".几个月后,微信正式推出微信应用号(即微信小程序),在互联网中掀起了又 ...

  9. Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)

    PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...

  10. 九个uname命令获取Linux系统详情的实例

    当你在控制台模式下,无法通过“鼠标右键 > 关于”获取操作系统的信息.这时,在Linux下,你可以使用uname命令,帮助你完成这些工作. Uname是unix name的缩写.在控制台中实际使 ...