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. OC文件大小的计算方法,多用于清理缓存

    OC文件大小的计算方法,一般多用于清理缓存.下载.统计 可以使用以下方法: #pragma mark Bt转换 + (NSString *)axcFileSizeWithToString:(unsig ...

  2. sqlserver 计算 百分比

    ,),))+'%' As 百分比 NUMERIC(P,S) P的默认值是:38 S的默认值是:-84~127 numeric(a,b)函数有两个参数,前面一个为总的位数,后面一个参数是小数点后的位数, ...

  3. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9

  4. Visual Mingw

    Visual-MinGW 是一个专门为MinGW所设计的IDE.如果,你曾经用过VC++ 6.0这个IDE,你的第一感觉就是,Visual-MinGW和VC++ 6.0非常相似.所以,对于曾经用惯VC ...

  5. VB的try语句,异常处理

    Try语句格式 Try 程式语句 Catch Throw ex Finally 释放一些连接等动作,不管有没有抛异常,都会执行到这里 End try 捕捉异常的语句是很好用的,在程式发生错误时,可以利 ...

  6. 20145211 《Java程序设计》第九周学习总结——垂死病中惊坐起

    教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性 JDBC标准分为两个部分:J ...

  7. 转:[ASP.NET]重構之路系列v4 – 簡單使用interface之『你也會IoC』

    前言 上次v3版本,我們將Entity, Service, Dao, Utility都放到了類別庫裡面,讓我們可以輕鬆的在不同專案中用同一份組件.雖然文章沒有獲得太多的讚賞,不過相信那一定是太多人會這 ...

  8. 删除Checkout with Multiple Addresses

    如果选择No,您的客户在结账时只可以输入一个收货地址,订单的货物都会送到这个地址.如果您选择Yes,您的客户将可以选择发货到多个地址,在购物车中的Proceed to Checkout按钮下面将会出现 ...

  9. LoadingView 自定义加载图片

    #import <UIKit/UIKit.h> @interface LoadingView : UIView @property (nonatomic,strong) NSMutable ...

  10. Boolean强制转换

    数据类型 转换为true的值 转换为false的值 Boolean true false String 任何非空字符串 空字符串 Number 任何非0数字值(包括无穷大) 0和NaN Object ...