Spring的设计模式解析与导入其他的核心配置文件实现-----Spring框架

发布于:2023-09-21 ⋅ 阅读:(55) ⋅ 点赞:(0)
观察者模式:
观察者:ServletContextListener
被观察者:ServletContext
事件:ServletContext对象被创建或者被销毁
监听器方法就被执行
Spring的事件编程就是观察者模式的体现
定义的ApplicationListener接口就是用来监听Application的事件
ApplicationContext内置了几个事件:ContextRefreshedEvent,ContextStartedEvent,ContextClosedEvent
策略模式重要实现就是面向接口编程,几个实现类可以互相切换,这就是我们Spring的策略模式
模板类的模板方法调用的是子类方法的执行
观察者模式:
观察者:ServletContextListener
被观察者:ServletContext
事件:ServletContext对象被创建或者被销毁
监听器方法就被执行
Spring的事件编程就是观察者模式的体现
定义的ApplicationListener接口就是用来监听Application的事件
ApplicationContext内置了几个事件:ContextRefreshedEvent,ContextStartedEvent,ContextClosedEvent
策略模式重要实现就是面向接口编程,几个实现类可以互相切换,这就是我们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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    引入外部的Spring配置文件-->
    <import resource="common.xml"></import>
<!--    引入外部的配置文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--    配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!--    配置SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
<!--        指定mybatis核心配置文件-->
        <property name="configLocation" value="mybatis-config.xml"></property>
<!--        指定别名,就是我们原本在mybatis-config配置文件里做的-->
        <property name="typeAliasesPackage" value="com.powernode.bank.pojo"></property>
    </bean>
<!--    mapper扫描配置器,指定我们扫描的Mapper,也是原本在mybatis核心配置文件里完成的-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.powernode.bank.mappers"></property>
    </bean>
<!--    启用事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    启用事务注解-->
    <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>

 

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    引入外部的Spring配置文件-->
    <import resource="common.xml"></import>
<!--    引入外部的配置文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--    配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!--    配置SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
<!--        指定mybatis核心配置文件-->
        <property name="configLocation" value="mybatis-config.xml"></property>
<!--        指定别名,就是我们原本在mybatis-config配置文件里做的-->
        <property name="typeAliasesPackage" value="com.powernode.bank.pojo"></property>
    </bean>
<!--    mapper扫描配置器,指定我们扫描的Mapper,也是原本在mybatis核心配置文件里完成的-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.powernode.bank.mappers"></property>
    </bean>
<!--    启用事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    启用事务注解-->
    <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--       设置组件扫描-->
    <context:component-scan base-package="com.powernode.bank"></context:component-scan>
</beans>

 

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--       设置组件扫描-->
    <context:component-scan base-package="com.powernode.bank"></context:component-scan>
</beans>

网站公告

今日签到

点亮在社区的每一天
去签到