- 浏览: 451717 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (371)
- Java (44)
- Flex (25)
- Design Pattern / UML (5)
- JVM (14)
- Software Engineer (4)
- Testing (30)
- Web Backend (60)
- Linux (7)
- Database (11)
- SOA (28)
- English (6)
- FX (5)
- SAP Support (25)
- SAP Development (5)
- SAP Impl & Oprn (8)
- Articles (15)
- Music (4)
- Recipe (14)
- Multi-thread (4)
- Lucene (2)
- CS basic / Algorithm (12)
- Tour (3)
- C C++ (21)
- Others (10)
- Code Repository C++ (1)
- Code Repository Java (1)
- Code Repository Algorithm (1)
- Code Repository SQL (1)
- Web Frontend (4)
最新评论
-
xiangxm:
Java 强引用、 软引用、 弱引用、虚引用 -
nml-lyb:
123
Mule 的第一个例子 -
nml-lyb:
http://www.baidu.com
Mule 的第一个例子 -
white___ant:
...
Java 强引用、 软引用、 弱引用、虚引用 -
joeyon:
...
Java NIO介绍
普通属性注入
依赖对象的注入方式,可以采用:
* ref属性
* <ref>标签
* 内部<bean>来定义
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="bean1" class="com.bjsxt.spring.Bean1">
<property name="strValue" value="Hello"/>
<!--
<property name="intValue" value="123"/>
-->
<property name="intValue">
<value>123</value>
</property>
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</map>
</property>
<property name="dateValue">
<value>2008-08-15</value>
</property>
</bean>
</beans>
public class Bean1 {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
private Date dateValue; // 字符串转换成date类需要一个特定的转换器。下一个段落中介绍
...
}
public class InjectionTest extends TestCase {
private BeanFactory factory;
@Override // testcase 中的初始化方法
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
public void testInjection1() {
Bean1 bean1 = (Bean1)factory.getBean("bean1");
System.out.println("bean1.strValue=" + bean1.getStrValue());
System.out.println("bean1.intValue=" + bean1.getIntValue());
System.out.println("bean1.listValue=" + bean1.getListValue());
System.out.println("bean1.setValue=" + bean1.getSetValue());
System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
System.out.println("bean1.mapValue=" + bean1.getMapValue());
System.out.println("bean1.dateValue=" + bean1.getDateValue());
}
}
自定义属性编辑器
上面段落中的
<property name="dateValue">
<value>2008-08-15</value>
</property>
中的字符串要转换成date类型,需要转换器的支持。
什么是属性编辑器,作用?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
* 将属性编辑器注册到spring中,参见:applicationContext-editor.xml
UtilDatePropertyEditor.java
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* java.util.Date属性编辑器
* @author Administrator
*
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format="yyyy-MM-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
applicationContext-editor.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 定义属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.bjsxt.spring.UtilDatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
<!--
<bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean>
-->
</beans>
公共属性的注入
当几个bean有相同的属性结构时,可以缩减配置,抽取公共属性设置
下面bean3 bean4有公共的属性
<bean id="bean2" class="com.bjsxt.spring.Bean2">
<property name="bean3" ref="bean3"/>
<property name="bean4">
<ref bean="bean4"/>
</property>
<property name="bean5" ref="bean5"/>
</bean>
<!--
<bean id="bean3" class="com.bjsxt.spring.Bean3">
<property name="id" value="1000"/>
<property name="name">
<value>Jack</value>
</property>
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.bjsxt.spring.Bean4">
<property name="id" value="1000"/>
<property name="name" value="Jack"/>
</bean>
-->
<bean id="bean5" class="com.bjsxt.spring.Bean5">
<property name="age" value="20"/>
</bean>
可以抽取相同属性:
在另一个单独的配置文件中配置
applicationContext-other.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="beanAbstract" abstract="true">
<property name="id" value="1000"/>
<property name="name" value="Jack"/>
</bean>
<bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
<property name="name" value="Tom"/>
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>
如何将公共的注入定义描述出来?
* 通过<bean>标签定义公共的属性,指定abstract=true
* 具有相同属性的类在<bean>标签中指定其parent属性
参见:applicationContext-other.xml
发表评论
-
Servlet Async
2015-05-15 02:29 410http://www.importnew.com/8864. ... -
EHcache
2014-09-06 23:32 462http://blog.csdn.net/liuzhenwe ... -
Maven构建并热部署一个简略的Web Project
2014-08-01 01:51 662zz from : http://www.myexcept ... -
Tomcat 权限配置,当使用Eclipse启动时都会被修改为默认
2014-07-31 07:57 452http://zhidao.baidu.com/link?u ... -
解决tomcat占用8080端口问题图文教程
2014-07-30 21:59 529zz from http://jingyan.baidu. ... -
HttpURLConnection用法详解
2013-09-04 02:08 605http://www.cnblogs.com/guodong ... -
使用HttpSessionListener接口监听Session 的创建和失效
2013-09-03 03:55 742http://uule.iteye.com/blog/824 ... -
web.xml 中的listener、 filter、servlet 加载顺序及其详解
2013-09-03 03:49 304http://www.cnblogs.com/JesseV/ ... -
servlet listener
2013-09-03 03:43 582http://sakyone.iteye.com/blog/ ... -
servlet filter
2013-09-03 03:42 644http://sakyone.iteye.com/blog/ ... -
Servlet中Request的getAttribute getParameter 区别
2013-08-29 12:31 1049http://blog.csdn.net/yangqillo ... -
用windows启动tomcat
2013-07-04 09:36 555services.msc -
Spring 加载 application.xml 出错FileNotFoundException
2013-06-23 21:26 889http://zhidao.baidu.com/questi ... -
<context-param>与<init-param>的区别与作用
2013-06-18 08:08 639http://www.cnblogs.com/hzj-/ar ... -
HttpWatch工具简介及使用技巧
2012-05-09 14:13 3807http://cfeng518.blog.163.com/bl ... -
Web Service (Axis2实现)
2012-04-19 11:13 1260转载from http://wangyaobeiji ... -
Tomcat4 源代码分析 (20) How Tomcat Works 第二十章
2009-07-27 17:24 839Chapter 20 : JMX-Based Manageme ... -
Tomcat4 源代码分析 (19) How Tomcat Works 第十九章
2009-07-27 17:19 716Chapter 19 : Manager Servlet ... -
Tomcat4 源代码分析 (18) How Tomcat Works 第十八章
2009-07-27 16:58 757Chapter 18 : Deployer T ... -
Tomcat4 源代码分析 (17) How Tomcat Works 第十七章
2009-07-24 17:57 1012Chapter 17 : Tomcat Startup ...
相关推荐
SXT--RBAC权限控制系统源码
mikrotik SXT 网桥点对点安装 全过程配置截图,拓扑图
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=...
-- p命名空间注入属性依然要设置set方法 --> <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"/> <!--c命名空间注入要求有对应参数的构造方法 --> <bean id="u1" class="cn.sxt.vo.User...
用VC++编写的一个开启摄像头的程序,可以不用安装驱动。
Video 开发 文档 所有下载文档 比较全,开发方便用
sxt shell 自动安装shell 脚本
sxt\weblogic\weblogic安装.avi sxt\weblogic\weblogic安装.avisxt\weblogic\weblogic安装.avi sxt\weblogic\weblogic安装.avi
sxt66329PPT模板.pptx
sxt_api_14.jar,便于下载可以使用
package cn.sxt.dynamicproxy; import java.util.ArrayList; import java.util.List; import cn.sxt.service.UserService; import cn.sxt.service.UserServiceImpl; public class Client { public ...
C#查看图片缩略图源码-8Sxt.rar
加热后软件恶霸vi热v贵部 将二姑vUI热 进欧冠ire
一个手写体文字识别的C++源代码,新思想新方法。
FastDFS 为互联网量身定制, 充分考虑了冗余备份、 负载均衡、 线性扩容等机制, 并注 重高可用、 高性能等指标, 使用 FastDFS 很容易搭建一套高性能的文件服务器集群提供文件 上传、 下载等服务。...
链表算法程序实现,指针操作干净利索,很经典的算法程序,
package cn.sxt.dao; public interface UserDao{ public void getUser(); }
我把这两个软件放在一起了,刘经理推荐的,一个是摄像头拍照片一个是制作dos gui的代码,请苗工程师使用看看。
万能摄像头 摄像头抓拍,获取摄像头图片 万能摄像头 摄像头抓拍,获取摄像头图片 万能摄像头 摄像头抓拍,获取摄像头图片
用C#编写的摄像头控制软件,一个简单的实例。