一.将对象配置到容器

1.xml文件:空参构造

<bean name="user" class="com.spring.bean.User" scope="prototype" init-method="init" destroy-method="destroy"></bean>

2.指定扫描com.spring.bean包下所有类中的注解.注:会扫描包下的所有后代包

<context:component-scan base-package="com.spring.bean"></context:component-scan>

注解:

  • @Component("BeanName") 将对象注册到spring容器

    • @Controler 控制层
    • @Service 业务层
    • @Repository dao层
  • @Scope指定对象的作用范围

    • singleton 单例模式
    • prototype 多例
  • @Value值类型属性注入

  • @Autowired 自动属性注入.根据类型注入

  • @Qulifier 指定注入的对象的名称

  • @Resource 指定对象的名称注入

  • @PostConstruct 初始化方法

  • @PreDestory 销毁方法

二.Spring AOP开发

  1. 定义代理对象

    • 配置目标对象
    • 配置通知对象
    • 定义代理对象
    <!-- proxyInterfaces:织入目标对象接口 -->
    <!-- target-ref:织入目标引用 -->
    <!-- interceptorNames:织入后置增强 -->
    <bean name="customerBean" class="org.springframework.aop.framework.ProxyFactoryBean"
    p:proxyInterfaces="com.spring.service.ICustomerService"
    p:target-ref="customerService" p:interceptorNames="surveyAfterAdvice,buyBeforeAdvice">
    </bean>
  2. 将通知织入到目标对象(xml文件)

    • 配置目标对象
    • 配置通知对象
    • 配置将通知织入目标对象
        <aop:config>
    <!-- 配置切入点 -->
    <aop:ponint-cut id="切点名称" expression="execution(切点表达式)" />
    <!-- 指定通知类中方法的通知类型 -->
    <aop:aspect ref="通知对象名称" >
    <aop:before method="" ponintcut-ref="" />
    <aop:after-returning method="" ponintcut-ref="" />
    <aop:around method="" ponintcut-ref="" />
    <aop:after-throwing method="" ponintcut-ref="" />
    <aop:after method="" ponintcut-ref="" />
    </aop:aspect>
    </aop:config>
  3. 开启aop注解(注解)

    • 配置目标对象(xml)

    • 配置通知对象(xml)

    • 开启使用注解完成注入

      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>(xml)

      @Aspect 指定当前类是通知类

      @Before(execution(切点表达式)) 前置通知方法

      @after-returning(execution(切点表达式)) 后置通知方法

      @around(execution(切点表达式)) 环绕通知方法

      @after-throwing(execution(切点表达式)) 异常拦截通知方法

      @after(execution(切点表达式)) 后通知方法


    @Pointcut("execution(* com.spring.service.*ServiceImpl.*(..))")

    @PointCut 抽取切点表达式

    public void pc(){}

    @Before("MyAdvice.pc()")

    @after-returning("MyAdvice.pc()")

    @Around("MyAdvice.pc()")

    @after-throwing("MyAdvice.pc()")

    @after("MyAdvice.pc()")

三.spring事务

  1. spring声明式事务
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性 -->
<!-- name:事务方法名; isolation:隔离级别; propagation:传播行为; read-only:是否只读 -->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- 配置将事务通知织入目标对象 -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.spring.tx.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面:通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>

2.spring事务注解

<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/> //类上
@Transaction(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl{} //方法上
@Transaction(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public void update*(..){}
05-11 15:39
查看更多