一个完成的spring 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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="duke" class="com.springinaction.springidol.Juggler" >
<!-- 通过构造方法设置属性值 -->
<constructor-arg value="15"></constructor-arg>
</bean> <bean id="sonnect29" class="com.springinaction.springidol.Sonnet29"></bean> <bean id="poeticPoem" class="com.springinaction.springidol.PoeticJuggler">
<constructor-arg value="15"></constructor-arg>
<constructor-arg ref="sonnect29"></constructor-arg>
</bean> <!-- 建立一个Instrumentalist bean
@通过property为bean设置属性值,一旦instrumentalist被实例化,则对象会被赋此值
-->
<bean id="Kenny" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells"></property>
<property name="age" value="37"></property> <!-- 这种做法可以实现接口与类的松耦合,比如下面两个都实现了Instrument接口的乐器类,Kenny bean可以随意引用 -->
<!--
<property name="instrument" ref="saxphone"></property>
<property name="instrument" ref="piano"></property>
-->
<!-- 内部bean的使用方式,这里用在property,constructor里面也是一样用 -->
<property name="instrument">
<bean class="com.springinaction.springidol.piano"></bean>
</property>
</bean> <bean id="saxphone" class="com.springinaction.springidol.saxphone"></bean>
<bean id="piano" class="com.springinaction.springidol.piano"></bean> <!-- p命名空间用法 -->
<bean id="Kenny2" class="com.springinaction.springidol.Instrumentalist"
p:song="Lemon Tree" p:age="30" p:instrument-ref="saxphone" >
</bean> <!-- 为集合配置bean -->
<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<list>
<ref bean="piano" />
<ref bean="saxphone" />
</list>
</property>
<property name="instruments2">
<map>
<entry key="piano" value-ref="piano"></entry>
<entry key="saxphone" value-ref="saxphone"></entry>
</map>
</property>
</bean> <!-- properties的写法 -->
<bean id="hank2" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<props>
<!-- key和value都为String -->
<prop key="piano">la la la</prop>
<prop key="saxphone">ta ta ta</prop>
</props>
</property>
</bean> <!-- 赋null值 -->
<!--
...
<property name="xxx"><null/></property>
...
-->
</beans>
package com.springinaction.springidol; /**
*
* @author Alan.chen
* @定义表演者
* @类型:接口
*/ public interface Proformer {
void perform() throws Exception;
}
package com.springinaction.springidol; public interface Instrument {
public void play();
}
package com.springinaction.springidol; public class saxphone implements Instrument { public saxphone() {
} @Override
public void play() {
System.out.println("toot toot toot");
} }
package com.springinaction.springidol; public class piano implements Instrument { public piano(){ } @Override
public void play() {
System.out.println("弹得一手好琴。。。");
} }
package com.springinaction.springidol; /**
*
* @author Alan.chen
* @诗人实现类
*/
public class Sonnet29 implements Poem { private static String[] LINES = {
"When in disgrace with fortune and men's eyes,",
"I all alone beweep my outcast state,",
"And trouble deaf heaven with my bootless cries,",
"And look upon myself, and curse my fate,",
"Wishing me like to one more rich in hope,",
"Featured like him, like him with friends posses'd,",
"Desiring this man's art, and that man's scope,",
"With what I most enjoy contented least;",
"Yet in these thoughts myself almost despising,",
"Haply I think on thee,-and then my state",
"(Like to the lark at break of day arising From sullen earth) sings hymns at heaven's gate;",
"For thy sweet love remember'd , such wealth brings,",
"That then I scorn to change my state with kings.!"
}; Sonnet29(){ } @Override
public void recite() {
for(int i=0;i<LINES.length;i++)
System.out.println(LINES[i]);
} }
package com.springinaction.springidol; /**
*
* @author Alan.chen
* @注入Bean属性示例,用setXXX(),getXXX()接收
* @描述:Instrumentalist,演奏家
*/
public class Instrumentalist implements Proformer { public Instrumentalist() {
} private int age; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public void perform() throws Exception {
System.out.println("playing "+song+" : ");
instrument.play();
} private String song;
public void setSong(String song){
this.song = song;
} public String getSong(){
return song;
} public String screamSong(){
return song;
} private Instrument instrument;
public void setInstrument(Instrument instrument){
this.instrument = instrument;
} }
package com.springinaction.springidol; import java.util.Collection;
import java.util.Map; public class OneManBand implements Proformer { public OneManBand() {
} @Override
public void perform() throws Exception {
for(Instrument instrument:instruments)
instrument.play();
for(String key:instruments2.keySet()){
System.out.println(key + " : ");
Instrument instrument = instruments2.get(key);
instrument.play();
}
} //当被注入的对象时集合时
private Collection<Instrument> instruments; public Collection<Instrument> getInstruments() {
return instruments;
} public void setInstruments(Collection<Instrument> instruments) {
this.instruments = instruments;
} //当被注入的对象是map时
private Map<String,Instrument> instruments2; public Map<String, Instrument> getInstruments2() {
return instruments2;
} public void setInstruments2(Map<String, Instrument> instruments2) {
this.instruments2 = instruments2;
} //当被注入的map的key和value都是String时,可以用properties来代替map
//private Properties instruments3;
// public void setInstruments3(Properties instruments2) {
// this.instruments2 = instruments3;
// } }
package com.springinaction.springidol; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/springinaction/springidol/spring-idol.xml");
// Proformer performer = (Proformer)ctx.getBean("duke");
// Proformer performer = (Proformer)ctx.getBean("poeticPoem");
// Proformer performer = (Proformer)ctx.getBean("Kenny2");
Proformer performer = (Proformer)ctx.getBean("hank");
try {
performer.perform();
} catch (Exception e) {
e.printStackTrace();
}
}
}
一个完成的spring xml配置文件的更多相关文章
- spring*.xml配置文件明文加密
spring*.xml配置文件明文加密 说明:客户要求spring*.xml中Oracle/Redis/MongoDB的IP.端口.用户名.密码不能明文存放,接到需求的我,很无奈,但是还是的硬着头皮搞 ...
- Eclipse创建Spring XML配置文件插件
引用:https://www.cnblogs.com/lideqiang/p/9067219.html 第一步:在 Eclipse Marketplace仓库中,搜索sts 第二步:安装Spring ...
- Spring XML配置文件无法自动提示 eclipse中XML配置文件open with打开方式选择 XML Editor:注意它的编辑方式也是有两种的design和source
双击XML配置文件,如果打开方式不正确 则如下图: 都是灰色显示,不会有自动提示,也不会有颜色标注 右击XML配置文件,选择打开方式为XML Editor,则会有颜色标注 如果此时没有自动提示 则要手 ...
- Spring XML配置文件示例(二)——web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...
- 03SpringMvc_自定义的spring.xml配置文件和逻辑视图名
这篇文章的目的是实现Struts2中一种形式(封装视图的逻辑名称),在Struts2中Action处理后会返回"SUCCESS"这样,然后根据"SUCCESS" ...
- spring xml 配置文件中标签的解析
一个springmvc配置文件的例子为: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- spring框架-spring.xml配置文件
运行的时候会报错的,因为写到<bean>标签里面去了,肯定会报错的,要记得把注释删掉,就不会报错了,这样写注释是为了方便下次自己看. <?xml version="1.0& ...
- 4.Spring——xml配置文件
如果使用Maven构建项目,spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件), 如果没有找到,才会转向去URL指定的路径下载.ap ...
- [spring]xml配置文件中的"classpath:"与"classpath*:"的区别
<bean id="sessionFactorySaas" class="org.mybatis.spring.SqlSessionFactoryBean" ...
随机推荐
- Redis—数据结构之list
Redis的列表对象底层所使用的数据结构其中之一就是list. list Redis的list是一个双端链表,其由3部分构成:链表节点.链表迭代器.链表.这一设计思想和STL的list是一样的,STL ...
- linux中core dump开启使用教程【转】
转自:http://www.111cn.net/sys/linux/67291.htm 一.什么是coredump 我们经常听到大家说到程序core掉了,需要定位解决,这里说的大部分是指对应程序由于各 ...
- 芒果TV 视频真实的地址获取
# coding=utf-8 import requests import json import re import os import urlparse import random vid = r ...
- poj1976
dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...
- win、mac系统配置本地电脑ip为域名教程
win系统: 如何修改hosts文件 主机文件原内容如下: #Copyright(c)1993-2009 Microsoft Corp. # #这是Windows的Microsoft TCP / IP ...
- 读书笔记--C陷阱与缺陷(三)
第三章 1. 指针与数组 书中强调C中数组注意的两点: 1) C语言只有一维数组,但是数组元素可以是任何类型对象,是另外一个数组时就产生了二维数组.数组大小是常数(但GCC实现了变长数组..) ...
- ZooKeeper常见问题
转载自原文:zookeeper(二)常见问题汇总 一.为什么zookeeper要部署基数台服务器? 所谓的zookeeper容错是指,当宕掉几个zookeeper服务器之后,剩下的个数必须大于宕掉的个 ...
- Centos之目录处理命令
linux中 关于目录 有几个重要概念 一个是 / 根目录 还有一个当前用户的家目录 比如 root用户的家目录是 /root 普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...
- 毕设demo写好
2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...
- 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)
题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...