spring

<!doctype html>

spring

配置文件

 
 
 
xxxxxxxxxx
16
 
 
 
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
       xmlns:aop="http://www.springframework.org/schema/aop"
5
       xmlns:context="http://www.springframework.org/schema/context"
6
       xmlns:tx="http://www.springframework.org/schema/tx"
7
       xsi:schemaLocation="http://www.springframework.org/schema/beans
8
    http://www.springframework.org/schema/beans/spring-beans.xsd
9
    http://www.springframework.org/schema/aop
10
    http://www.springframework.org/schema/aop/spring-aop.xsd
11
    http://www.springframework.org/schema/tx
12
    http://www.springframework.org/schema/tx/spring-tx.xsd
13
     http://www.springframework.org/schema/context
14
     http://www.springframework.org/schema/context/spring-context.xsd">
15
16
</beans>
 
 

aop配置

 
 
 
xxxxxxxxxx
18
 
 
 
 
1
   <aop:config>
2
<!--        配置切面-->
3
        <aop:aspect id="Aspect" ref="LogXmlAspect">
4
<!--            配置切入点-->
5
            <aop:pointcut id="pointcut" expression="execution(* huluxia.fun.bean.*(..))"/>
6
<!--            前置通知-->
7
            <aop:before method="doBefore" pointcut-ref="pointcut"/>
8
<!--            后置通知-->
9
            <aop:after-returning method="doAfterReturning" pointcut-ref="pointcut" returning="returnValue"/>
10
<!--            环绕通知-->
11
            <aop:around method="doAround" pointcut-ref="pointcut"/>
12
<!--            异常通知-->
13
            <aop:after-throwing method="doAfterThrowing" pointcut-ref="pointcut" throwing="throwable"/>
14
<!--            最终通知-->
15
            <aop:after method="doAfter" pointcut-ref="pointcut"/>
16
        </aop:aspect>
17
    </aop:config>
18
 
 

 

通知类

 
 
 
xxxxxxxxxx
42
 
 
 
 
1
public class LogXmlAspect {
2
 //前置通知
3
    public void doBefore(JoinPoint joinPoint) { // 在通知方法中,声明JoinPoint类型的形参,就可以在Spring调用当前方法时把这个类型的对象传入
4
5
        // 1.通过JoinPoint对象获取目标方法的签名
6
        // 所谓方法的签名就是指方法声明时指定的相关信息,包括方法名、方法所在类等等
7
        Signature signature = joinPoint.getSignature();
8
9
        // 2.通过方法签名对象可以获取方法名
10
        String methodName = signature.getName();
11
12
        // 3.通过JoinPoint对象获取目标方法被调用时传入的参数
13
        Object[] args = joinPoint.getArgs();
14
15
        // 4.为了方便展示参数数据,把参数从数组类型转换为List集合
16
        List<Object> argList = Arrays.asList(args);
17
18
        System.out.println("[前置通知]" + methodName + "方法开始执行,参数列表是:" + argList);
19
    }
20
//后置通知
21
    public void doAfterReturning(JoinPoint joinPoint, Object returnValue) {
22
        String methodName = joinPoint.getSignature().getName();
23
        System.out.println("[返回通知]" + methodName + "方法成功结束,返回值是:" + returnValue);
24
    }
25
    //环绕通知
26
    public void doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
27
        System.out.println("[环绕通知]" +""+ "方法成功开始");
28
        Object object=proceedingJoinPoint.proceed();
29
        System.out.println("[环绕通知]" + "" + "方法成功开始");
30
    }
31
//异常通知
32
    public void doAfterThrowing(JoinPoint joinPoint, Throwable throwable) {
33
        String methodName = joinPoint.getSignature().getName();
34
        System.out.println("[异常通知]" + methodName + "方法异常结束,异常信息是:" + throwable.getMessage());
35
    }
36
//最终通知
37
    public void doAfter(JoinPoint joinPoint) {
38
        String methodName = joinPoint.getSignature().getName();
39
        System.out.println("[后置通知]" + methodName + "方法最终结束");
40
    }
41
}
42
 
 

注解aop配置

 
 
 
xxxxxxxxxx
2
 
 
 
 
1
   <context:component-scan base-package="huluxia.fun"/>
2
    <aop:aspectj-autoproxy/>
 
 

注解通知类

 
 
 
xxxxxxxxxx
52
 
 
 
 
1
@Aspect
2
@Component
3
public class LogXmlAspect {
4
@Pointcut("execution(* huluxia.fun.bean.*(..))")
5
    private void  pointcut(){
6
7
    }
8
    @Before("pointcut()")
9
    //前置通知
10
    public void doBefore(JoinPoint joinPoint) { // 在通知方法中,声明JoinPoint类型的形参,就可以在Spring调用当前方法时把这个类型的对象传入
11
12
        // 1.通过JoinPoint对象获取目标方法的签名
13
        // 所谓方法的签名就是指方法声明时指定的相关信息,包括方法名、方法所在类等等
14
        Signature signature = joinPoint.getSignature();
15
16
        // 2.通过方法签名对象可以获取方法名
17
        String methodName = signature.getName();
18
19
        // 3.通过JoinPoint对象获取目标方法被调用时传入的参数
20
        Object[] args = joinPoint.getArgs();
21
22
        // 4.为了方便展示参数数据,把参数从数组类型转换为List集合
23
        List<Object> argList = Arrays.asList(args);
24
25
        System.out.println("[前置通知]" + methodName + "方法开始执行,参数列表是:" + argList);
26
    }
27
//后置通知
28
    @AfterReturning(value = "pointcut()",returning = "returnValue")
29
    public void doAfterReturning(JoinPoint joinPoint, Object returnValue) {
30
        String methodName = joinPoint.getSignature().getName();
31
        System.out.println("[返回通知]" + methodName + "方法成功结束,返回值是:" + returnValue);
32
    }
33
    //环绕通知
34
    @Around("pointcut()")
35
    public void doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
36
        System.out.println("[环绕通知]" +""+ "方法成功开始");
37
        Object object=proceedingJoinPoint.proceed();
38
        System.out.println("[环绕通知]" + "" + "方法成功开始");
39
    }
40
//异常通知
41
    @AfterThrowing(value = "pointcut()",throwing = "throwable")
42
    public void doAfterThrowing(JoinPoint joinPoint, Throwable throwable) {
43
        String methodName = joinPoint.getSignature().getName();
44
        System.out.println("[异常通知]" + methodName + "方法异常结束,异常信息是:" + throwable.getMessage());
45
    }
46
//最终通知
47
    @After("pointcut()")
48
    public void doAfter(JoinPoint joinPoint) {
49
        String methodName = joinPoint.getSignature().getName();
50
        System.out.println("[后置通知]" + methodName + "方法最终结束");
51
    }
52
}
 
 

spring jdbc

 
 
 
xxxxxxxxxx
29
 
 
 
 
1
<!--    配置数据源-->
2
  <context:property-placeholder location="classpath:resource/db.properties" ignore-unresolvable="true" />
3
    <bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
4
        <property name="url"
5
                  value="${jdbc.url}"/>
6
        <property name="driverClassName" value="${jdbc.driver}"/>
7
        <property name="username" value="${jdbc.username}"/>
8
        <property name="password" value="${jdbc.password}"/>
9
    </bean>
10
<!--    配置jdbc模板-->
11
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
12
<!--       使用数据源-->
13
       <property name="dataSource" ref="dataSouce"/>
14
   </bean>
15
<!--    事务管理器,依赖于数据源-->
16
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
17
        <property name="dataSource" ref="dataSouce"></property>
18
    </bean>
19
    
20
   <tx:advice id="txadvice"  transaction-manager="transactionManager">
21
        <tx:attributes>
22
            <tx:method name="*"/>
23
        </tx:attributes>
24
    </tx:advice>
25
    <aop:config>
26
        
27
            <aop:pointcut id="pointcut" expression="execution(* huluxia.fun.spring.*(..))"/>
28
        <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
29
    </aop:config>
 
 

spring的事务管理

 
 
 
xxxxxxxxxx
11
 
 
 
 
1
<!--    对事务进行增强(通知),需要编写对切入点和具体执行事务细节-->
2
    <tx:advice id="txadvice"  transaction-manager="transactionManager">
3
        <tx:attributes>
4
            <tx:method name="*"/>
5
        </tx:attributes>
6
    </tx:advice>
7
    <aop:config>
8
9
            <aop:pointcut id="pointcut" expression="execution(* huluxia.fun.spring.*(..))"/>
10
        <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
11
    </aop:config>
 
 

spring的事务管理注解版

 
 
 
xxxxxxxxxx
4
 
 
 
 
1
<tx:annotation-driven transaction-manager="transactionManager"/>
2
   
3
@Transactional
4
public class ....(){}
 
 

 

 

posted @ 2021-07-09 11:17  老运维  阅读(29)  评论(0)    收藏  举报