Spring面向切面编程AOP使用编程式事务解决方案进行开发-----Spring框架

发布于:2023-09-22 ⋅ 阅读:(69) ⋅ 点赞:(0)
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    扫描包-->
    <context:component-scan base-package="com.powernode.spring6.service"></context:component-scan>
<!--    启用AOP-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--    设置仅用CGLIB的继承方式代理实现AOP-->
    <aop:config proxy-target-class="true"></aop:config>
</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:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    扫描包-->
    <context:component-scan base-package="com.powernode.spring6.service"></context:component-scan>
<!--    启用AOP-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--    设置仅用CGLIB的继承方式代理实现AOP-->
    <aop:config proxy-target-class="true"></aop:config>
</beans>
package com.powernode.spring6.test;

import com.powernode.spring6.service.AccountService;
import com.powernode.spring6.service.OrderService;
import com.powernode.spring6.service.Spring6Config;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPRealAPPTest
{
    @Test
    public void TestTransaction()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        AccountService accountService = context.getBean("accountService", AccountService.class);
        accountService.transfer();
    }
    @Test
    public void TestTransaction1()
    {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring6Config.class);
        OrderService orderService = context.getBean("orderService", OrderService.class);
        //编程式事务解决方案
        orderService.cancel();
    }
}
package com.powernode.spring6.test;

import com.powernode.spring6.service.AccountService;
import com.powernode.spring6.service.OrderService;
import com.powernode.spring6.service.Spring6Config;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPRealAPPTest
{
    @Test
    public void TestTransaction()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        AccountService accountService = context.getBean("accountService", AccountService.class);
        accountService.transfer();
    }
    @Test
    public void TestTransaction1()
    {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring6Config.class);
        OrderService orderService = context.getBean("orderService", OrderService.class);
        //编程式事务解决方案
        orderService.cancel();
    }
}
package com.powernode.spring6.service;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@ComponentScan({"com.powernode.spring6.service"})
@Configuration
//启用Aspect自动代理,使用CGLIB
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Spring6Config
{
}
package com.powernode.spring6.service;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@ComponentScan({"com.powernode.spring6.service"})
@Configuration
//启用Aspect自动代理,使用CGLIB
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Spring6Config
{
}
package com.powernode.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TransactionAspect
{
    private static final Logger logger = LoggerFactory.getLogger(TransactionAspect.class);
    @Pointcut("execution(* com.powernode.spring6.service..*(..))")
    public void Aspect()
    {
    }
    @Around("Aspect()")
    public void around(ProceedingJoinPoint joinPoint)
    {
        try
        {
            //前环绕
            logger.info("开启事务");
            //执行目标
            joinPoint.proceed();
            //后环绕
            logger.info("提交事务");
        }
        catch (Throwable e)
        {
            logger.info("回滚事务");
        }
    }
}
package com.powernode.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TransactionAspect
{
    private static final Logger logger = LoggerFactory.getLogger(TransactionAspect.class);
    @Pointcut("execution(* com.powernode.spring6.service..*(..))")
    public void Aspect()
    {
    }
    @Around("Aspect()")
    public void around(ProceedingJoinPoint joinPoint)
    {
        try
        {
            //前环绕
            logger.info("开启事务");
            //执行目标
            joinPoint.proceed();
            //后环绕
            logger.info("提交事务");
        }
        catch (Throwable e)
        {
            logger.info("回滚事务");
        }
    }
}
package com.powernode.spring6.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class OrderService
{
    private static final Logger logger = LoggerFactory.getLogger(OrderService.class);
    public void generate()
    {
        logger.info("正在生成订单");
    }
    //取消订单的方法
    public void cancel()
    {
        logger.info("订单已取消");
        String s = null;
        s.toString();
    }
}
package com.powernode.spring6.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class OrderService
{
    private static final Logger logger = LoggerFactory.getLogger(OrderService.class);
    public void generate()
    {
        logger.info("正在生成订单");
    }
    //取消订单的方法
    public void cancel()
    {
        logger.info("订单已取消");
        String s = null;
        s.toString();
    }
}
package com.powernode.spring6.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class AccountService
{
    private static final Logger logger = LoggerFactory.getLogger(AccountService.class);
    public void transfer()
    {
        logger.info("银行账户正在完成转账操作");
    }
    public void withdraw()
    {
        logger.info("银行账户正在取款");
    }
}
package com.powernode.spring6.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class AccountService
{
    private static final Logger logger = LoggerFactory.getLogger(AccountService.class);
    public void transfer()
    {
        logger.info("银行账户正在完成转账操作");
    }
    public void withdraw()
    {
        logger.info("银行账户正在取款");
    }
}

网站公告

今日签到

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