spring框架一之属性值的初始化
来源:互联网 发布:常见文本文件格式知乎 编辑:程序博客网 时间:2024/06/05 11:12
今天我们来了解spring框架,首先我们来了解spring的注入功能。值得注意的是spring采用的是java本身的代理,所以在配置spring框架的项目时候,最后采用面向接口编程。这样就可以通过配置文件来降低耦合性
我们先从配置文件讲解怎么实现属性的注入
<?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"> <!-- 配置具体的实现dao层,其中id为引用key,class为具体的实现类 --> <bean id="userDao" class="com.xingyao.dao.UserDao"></bean> <!-- 配置业务层的类 --> <bean id="userManager" class="com.xingyao.manager.UserManager"> <!-- property的中文意思为:属性,所以这个标签的作用是配置userManager里面的属性 --> <!-- name:该类的属性名(和Struts的配置是一个作用) ref:表示参照某个bean,值为配置的bean的id --> <property name="baseDao" ref="userDao"></property> <!-- 配置该类属性名为student的值 --> <property name="student" ref="student"></property> </bean> <!-- 定义格式转换器:将字符串类型转化为Date类型,这里的class为系统类--> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <!-- 系统类里面的属性,是一个map集合类型的属性 --> <property name="customEditors"> <map> <!-- key为你需要转化的成的类型 --> <entry key="java.util.Date"> <!-- 这里是配置你自定义的类型转换器 --> <bean class="com.xingyao.model.util.UtilDatePropertyEditor"> <property name="format" value="yyyy-MM-dd"></property> </bean> </entry> </map> </property> </bean></beans>
从上面我们可以看到,这里的ref的属性值并没有配置Bean,这是因为我在其他的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: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"> <!-- 配置类为 com.xingyao.model.Student的bean--> <bean id="student" class="com.xingyao.model.Student"> <property name="student_id"> <!-- 这里是在这个属性值里面是设置初始值 --> <value>20132092</value> </property> <property name="student_name"> <value>liuxin</value> </property> <property name="class_id"> <!-- 如果该属性的类型是set集合就这样配置 --> <set> <value>1111</value> <value>2222</value> </set> </property> <property name="create_time"> <!-- 时间类型,需要用到转换器,所以就自定义了一个转换器 --> <value>2016-09-09</value> </property> </bean></beans>
另:
想看一个最简单的项目实现,请查看整理笔记一里面的附件
转载请标明出处
感谢苏波老师的指导
个人见解、错误请指出!请莫怪
运行程序为
运行结果为
阅读全文
0 0
- spring框架一之属性值的初始化
- Spring框架解析一【初始化】
- 框架之spring(一)
- Spring学习笔记 初始化属性为null值的方法
- 5、SSM框架-Spring之bean的属性值XML注入方式(5)
- SSM框架之--spring(一)
- SSM框架之--spring(一)
- Spring框架下类的初始化顺序
- Spring框架下类的初始化顺序
- Spring 框架IOC的初始化过程
- Spring框架下类的初始化顺序
- Spring框架的学习之路(一)
- 基于mvc模式的应用框架之spring(一)
- spring框架的学习(一)之我们为什么要用spring框架
- 框架漫谈之spring(一)工厂模式实现程序解耦,spring框架的引出
- Spring 一、 之Bean容器初始化以及bean实例的获取
- Spring原理(一)IoC容器的初始化过程之BeanFactory
- Spring框架bean的scope属性
- 微信WeToast增强插件
- [学习笔记]认识Android Service
- hdu 2612 Find a way ( 双bfs )
- 限制QLineEdit的数值输入范围(QT正则表达式方法),顺便简单介绍下QT正则表达式方法
- 【机房重构】配置文件
- spring框架一之属性值的初始化
- 深入理解JAVA虚拟机(三)内存分配与回收机制
- 标准C++中的string类的用法总结
- PID的快速理解与掌握
- 函数中的数组传递
- BZOJ 2306 [Ctsc2011] 幸福路径 [倍增+floyd做法]
- 机器学习的相关模块安装:pandas,numpy, scipy,scikit_learn(sklearn), matplotlib在winows系统下的安装问题
- swjtu2381(Matrix)
- Java过滤器与SpringMVC拦截器之间的关系与区别