程序员社区

【深入Spring】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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 传统的基于代理的aop配置 创建代理工厂 -->
    <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value=""></property>
        <property name="interceptorNames" value="..,.."></property>
        <property name="interfaces" value="..,.."></property>
    </bean>
    
    <!-- 用于扫描jdbc配置文件 -->
    <context:property-placeholder location=""/>
    
    <!-- 配置数据源 (C3P0)-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value=""></property>
        <property name="jdbcUrl" value=""></property>
        <property name="user" value=""></property>
        <property name="password" value=""></property>
    </bean>
    
    <!-- 配置JDBC模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 用于配置基于注解的DI -->
    <context:component-scan base-package=""></context:component-scan>
    
    <!-- 依赖注入DI -->
    <!--(方式一)设值注入 set方法 -->
        <!-- 普通注入 -->
        <bean id="star" class="com.bjsxt.pojo.Star">
            <property name="name" value="吴京"></property>
        </bean>
    
        <!-- 域属性注入  -->
        <bean id="star" class="com.bjsxt.pojo.Star">
            <property name="name" ref="吴京"></property>
        </bean>
        
        <!-- p命名空间注入 -->
        <bean id="star" class="com.bjsxt.pojo.Star" p:name="吴京" p:age="11" p:score="99" p:partner-ref="123">
        </bean>
   
    <!-- (方式二)构造注入  constructor-->
        <bean id="star" class="com.bjsxt.pojo.Star">
            <constructor-arg name="name" value="吴京"></constructor-arg>
            <constructor-arg name="partner" ref="partner"></constructor-arg>
        </bean>
    <!-- (方式三)自动注入 -->
        <!-- 域属性设值自动注入byName  -->
        <bean id="star" class="com.bjsxt.pojo.Star" autowire="byName">
        </bean>
        
        <!-- 域属性设值自动注入byType(容器中与注入的bean的类型具有is-a关系的类只能有一个)  -->
        <bean id="star" class="com.bjsxt.pojo.Star" autowire="byName">
        </bean>
        
        <!-- 域属性构造注入constructor,首先byName,其次byType  -->
        <bean id="star" class="com.bjsxt.pojo.Star" autowire="constructor">
            <constructor-arg name="name" value="吴京"></constructor-arg>
        </bean>
        
        <!-- 域属性自动检测自动装配(autodetect),(首先根据构造方法注入,如果没有默认的构造方法,则以byType方式注入) -->
        <bean id="star" class="com.bjsxt.pojo.Star" autowire="autodetect">
            <constructor-arg name="name" value="吴京"></constructor-arg>
        </bean>
    
    <!-- aop -->
        
    <!-- 传统的基于代理的aop配置 创建代理工厂 -->
    <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 配置目标对象 -->
        <property name="targetName" value=""></property>
        <!-- 配置通知(字符串数组) -->
        <property name="interceptorNames" value="..,.."></property>
        <!-- 配置目标对象实现的接口(Class数组)(配不配都行) -->
        <property name="interfaces" value="..,.."></property>
    </bean>
    
    
    <!-- 基于aspectJ的aop 方式一(annotation配置) -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
    <!-- 基于aspectJ的aop 方式二(xml配置) -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="" id=""/>
        <!-- 配置切面  ref注入对应的切面的对象(aspectJ方式)-->
        <aop:aspect ref="">
            <!-- 配置切面对象的方法为何种通知 -->
  
            <!-- 前置通知 -->
            <aop:before method="" pointcut-ref=""/>
            <!-- 环绕通知 -->
            <aop:around method="" pointcut-ref=""/>
            <!-- 后置通知 -->
            <aop:after-returning method="" pointcut-ref=""/>
            <!-- 最终通知 -->
            <aop:after method="" pointcut-ref=""/>
            <!-- 异常通知 -->
            <aop:after-throwing method="" pointcut-ref=""/>
        </aop:aspect>
    </aop:config>
    
    <!-- Scheme base的aop配置方式 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="" id=""/>
        <!-- 配置具体的通知 (需要对应的advice-ref实现相应的接口)-->
        <!-- 前置通知 MethodBeforeAdvice -->
        <!-- 后置通知 AfterReturningAdvice -->
        <!-- 环绕通知 MethodInterceptor -->
        <!-- 异常通知 ThrowsAdvice -->
        <aop:advisor advice-ref="" pointcut-ref=""/>
    </aop:config>
    
    <!-- 事务 -->

    <!-- 配置事务对象 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
        
    <!-- 方式一 基于xml方式的事务配置 -->
    <!-- 配置是事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- name遇到什么方法执行配置的事务通知  隔离方式isolation  传播行为propagation rollback-for异常回滚-->
            <tx:method name="" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置aop切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* *..service.*.*(..))" id="point"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
    </aop:config>
    
    
    <!-- 方式二 基于注解的事务配置 -->
    <!-- 方法上配置 Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollBackFor=FundException.class) -->
    <!-- 配置事务注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

        
</beans>
赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【深入Spring】Spring中的配置文件集合

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区