spring day02-go3
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的更多相关文章
- Spring day02笔记
spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...
- 开放源代码的设计层面框架Spring——day02
spring第二天 一.基于注解的IOC配置 1.1写在最前 学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是 ...
- Spring day03笔记
spring day02回顾 AOP :切面编程 切面:切入点 和 通知 结合 spring aop 编程 <aop:config> 方法1: <aop:pointcut expre ...
- Spring day01笔记
struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解] sprin ...
- spring框架入门day01
struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解] spring ...
- Spring-Day02-依赖注入-作业
配置beans约束自动提示 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html中打开xsd-configuratio ...
- [刘阳Java]_Spring常用注解介绍_第6讲
Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置.在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为 ...
- [刘阳Java]_Spring相关配置介绍_第5讲
这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...
- Spring Boot 配置_yaml语法介绍 day02
一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...
- Spring Boot 基于Spring Initializer 的快速构建 day02
一.基于Spring Initializr 快速构建Spring Boot项目(快速) 备注:需要联网 这是使用Intellij Idea快速构建可以为我们省去大量的pom.xml配置时间,简单操作, ...
随机推荐
- php---实现保留小数点后两位
PHP 中的 round() 函数可以实现 round() 函数对浮点数进行四舍五入. round(x,prec) 参数说明x 可选.规定要舍入的数字.prec 可选.规定小数点后的位数. 返回将 x ...
- [Stanford 2011] 知识点小结
1.获得帮助:option+click / option+double click 2.@property里的nonatomic,表示非原子性访问,atomic是obj-c里使用的一种线程保护技术, ...
- sublime text主要快捷键列表
Ctrl+L 选择整行(按住-继续选择下行)Ctrl+KK 从光标处删除至行尾Ctrl+Shift+K 删除整行Ctrl+Shift+D 复制光标所在整行,插入在该行之前Ctrl+J 合并行(已选择需 ...
- Java学习-033-JavaWeb_002 -- 网页标记语言JSP基础知识
JSP 是 Sun 公司提倡的一门网页技术标准.在 HTML 文件中,加入 Java 代码就构成了 JSP 网页,当 Web 服务器访问 JSP 请求的时候,首先执行其中的 Java 程序源码,然后以 ...
- java中清空session
session.removeAttribute("sessionname")是清除SESSION里的某个属性. session.invalidate()是让SESSION失 ...
- 8月3日奥威Power-BI V11 视频交流开课啦!
) 进群备注:奥威Power-BI V11 在QQ群上不见不散! 主题: Power-BI V11 基于EXCEL数据源快速制作管理驾驶舱 分享交流内容: 1.产品安装与配置(超级简单,傻瓜式安装) ...
- Java Set操作
Set:无顺序,不包含重复的元素 HashSet:为快速查找设计的Set.存入HashSet的对象必须定义hashCode(). TreeSet: 保存次序的Set, 底层为树结构.使用它可以从Set ...
- 修改文件中的内容,使用fileinput模块
import fileinput filename = 'passwd' ,backup='.bak') print line.replace('root','ROOT') a = ' this is ...
- javascript设计模式学习之十三——职责链模式
一.职责链的定义和使用场景 职责链模式的定义是,职责链模式将一系列可能会处理请求的对象连接成一条链,请求在这些对象之间一次传递,直到遇到一个可以处理它的对象.从而避免请求的发送者和接收者之间的耦合关系 ...
- iOS block在两个页面间的简单传值
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...