spring框架一之属性值的初始化

今天我们来了解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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值