spring框架学习笔记04

本文介绍了Spring框架中的多种配置方法,包括日期注入、数据源配置、Bean继承及依赖检查等,并探讨了Spring的自动扫描和过滤机制。

spring注入日期到bean:

  • 在Spring中,可以通过两种方式注入日期:
  • 1 .Factory bean方法:
    • 声明一个dateFormat bean,在“customer” Bean,引用 “dateFormat” bean作为一个工厂bean。该工厂方法将调用SimpleDateFormat.parse()自动转换成字符串Date对象。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
    </bean>
    <bean id="customer" class="com.hyq.common.Customer">
        <property name="date">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2017-4-9" />
            </bean>
        </property>
    </bean>

</beans>
  • 2.CustomDateEditor方法:
    • 声明一个 CustomDateEditor 类将字符串转换成 java.util.Date。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="dateEditor"       class="org.springframework.beans.propertyeditors.CustomDateEditor">
        <constructor-arg>
            <bean class="java.text.SimpleDateFormat">
                <constructor-arg value="yyyy-MM-dd" />
            </bean>
        </constructor-arg>
        <constructor-arg value="true" />
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date">
                    <ref local="dateEditor" />
                </entry>
            </map>
        </property>
    </bean>

    <bean id="customer" class="com.hyq.common.Customer">
        <property name="date" value="2017-4-9" />
    </bean>
</beans>

spring 数据源配置:

  • Spring PropertyPlaceholderConfigurer实例:
    • 可以使用 PropertyPlaceholderConfigurer 类通过一个特殊的格式在外部部署细节到一个属性(properties )文件,以及访问bean的配置文件 – ${variable}.
    • 创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
  • database.properties代码:
jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/hyq
    jdbc.username=root
    jdbc.password=1234
  • 在声明bean配置文件和提供一个PropertyPlaceholderConfigurer映射到 刚才创建的“database.properties”属性文件。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>database.properties</value>
        </property>
</bean>
  • 完整代码:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>database.properties</value>
        </property>
    </bean>

    <bean id="customerDAO" class="com.hyq.customer.dao.impl.JdbcCustomerDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="customerSimpleDAO"              class="com.hyq.customer.dao.impl.SimpleJdbcCustomerDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

</beans>

Spring bean配置继承:

  • 在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
  • 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。

  • Bean配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="BaseCustomerMalaysia" class="com.hyq.common.Customer">
        <property name="country" value="Malaysia" />
    </bean>
    <bean id="CustomerBean" parent="BaseCustomerMalaysia">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>
  • 以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父(‘BaseCustomerMalaysia’)这个值。
  • 执行它:
Customer [type=1, action=buy, Country=Malaysia]
  • 继承抽象:
    • 如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个元素中添加一个“abstract”的属性。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="BaseCustomerMalaysia" abstract="true">
        <property name="country" value="Malaysia" />
    </bean>
    <bean id="CustomerBean" parent="BaseCustomerMalaysia" 
        class="com.hyq.common.Customer">   
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>
</beans>

Spring依赖检查:

  • 4个依赖检查支持的模式:
    • none – 没有依赖检查,这是默认的模式。
    • simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
    • objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
    • all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。

注:默认模式是 none

  • 1.none 依赖检查
    • Spring bean配置文件使用 “none” 依赖检查模式。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="CustomerBean" class="com.hyq.common.Customer" >
        <property name="action" value="buy" />
    </bean>
    <bean id="PersonBean" class="com.hyq.common.Person">
        <property name="name" value="yiibai" />
        <property name="address" value="address ABC" />
        <property name="age" value="29" />
    </bean>

</beans>

注解:

  • Spring使用@Required注解依赖检查:
    • Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性..对于这种情况,你需要 @Required 注解
  • Spring自定义@Required-style注解:
    • @Required注解是用来确保特定属性已设置。如果您迁移现有项目到Spring框架或有自己的@Required-style注解不管是什么原因,Spring允许您定义自定义@Required-style注解,相当于@Required注解。

Spring过滤器组件自动扫描:

  • 1.过滤组件 - 包含
    • 使用Spring “过滤” 扫描并注册匹配定义“regex”,即使该类组件的名称未标注 @Component 。也会被扫描。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.hyq" >
        <context:include-filter type="regex"              expression="com.hyq.customer.dao.*DAO.*" />
        <context:include-filter type="regex"           expression="com.hyq.customer.services.*Service.*" />
    </context:component-scan>
</beans>
  • 在这个XML过滤中,所有文件的名称中包含 DAO 或 Service(DAO., Services.) 单词将被检测并在 Spring 容器中注册。
  • 2.过滤组件 - 不包含
    • 排除指定组件,以避免 Spring 检测和 Spring 容器注册。不包括在这些文件中标注有 @Service 。
<context:component-scan base-package="com.hyq.customer" >
        <context:exclude-filter type="annotation" 
    expression="org.springframework.stereotype.Service" />      
    </context:component-scan>
<!--不包括那些包含DAO这个词组文件名。-->
<context:component-scan base-package="com.hyq" >
        <context:exclude-filter type="regex" 
            expression="com.hyq.customer.dao.*DAO.*" />     
    </context:component-scan>

注:以上内容均参考‘易白教程’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值