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
原创粉丝点击