Spring 注入随笔

Spring 注入随笔

平常在Java开发中,程序员在某个类中需要依赖其他类的方法,则通常new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,Spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过Spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工资不由我们自己来做,而是交给Spring容器来做。

这篇博客表述的几种注入方式队初学者很有借鉴之用
http://blessht.iteye.com/blog/1162131
注解注入,参考
http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/

转载:http://blog.csdn.net/zhu_tianwei/article/details/43352471

Spring-AOP之aspectj注解方式
一、简介
1、AOP用在哪些方面:AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任,例如事务处理、日志管理、权限控制,异常处理等,封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
2、AOP中的概念:
Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象.
joinpoint(连接点):所谓连接点是指那些被拦截到的点(可以是方法、属性、或者类的初始化时机(可以是Action层、Service层、dao层))。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器)
Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义,也即joinpoint的集合.
Advice(通知):所谓通知是指拦截到joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知
Target(目标对象):代理的目标对象
Weave(织入):指将aspects应用到target对象并导致proxy对象创建的过程称为织入.
Introduction(引入):在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.

3、AOP带来的好处:降低模块的耦合度;使系统容易扩展;更好的代码复用性
二、通过注解方式实现spring的AOP
1.定义业务类
接口:

    package cn.slimsmart.spring.demo.aop;  

    public interface UserService {  
        String save(String name);  
        String update(String name);  
        String delete(String name);  
    }  

实现:

    package cn.slimsmart.spring.demo.aop;  

    import org.springframework.stereotype.Service;  

    @Service  <span style="font-family: Arial, Helvetica, sans-serif;">//使用自动注解的方式实例化并初始化该类</span>  
    public class UserServiceImpl implements UserService{  

        @Override  
        public String save(String name) {  
            System.out.println("--------save");  
            return "save";  
        }  

        @Override  
        public String update(String name) {  
            System.out.println("--------update");  
            System.out.println(1/0);  
            return "update";  
        }  

        @Override  
        public String delete(String name) {  
            System.out.println("--------delete");  
            return "delete";  
        }  

    }  

2.定义切面类

    package cn.slimsmart.spring.demo.aop;  

    import org.aspectj.lang.JoinPoint;  
    import org.aspectj.lang.ProceedingJoinPoint;  
    import org.aspectj.lang.annotation.After;  
    import org.aspectj.lang.annotation.AfterReturning;  
    import org.aspectj.lang.annotation.AfterThrowing;  
    import org.aspectj.lang.annotation.Around;  
    import org.aspectj.lang.annotation.Aspect;  
    import org.aspectj.lang.annotation.Before;  
    import org.aspectj.lang.annotation.Pointcut;  
    import org.springframework.stereotype.Component;  

    //@Aspect : 标记为切面类  
    //@Pointcut : 指定匹配切点集合  
    //@Before : 指定前置通知,value中指定切入点匹配  
    //@AfterReturning :后置通知,具有可以指定返回值  
    //@AfterThrowing :异常通知  
    //@Around 环绕通知 环绕通知的方法中一定要有ProceedingJoinPoint 参数,与Filter中的  doFilter方法类似  
    //注意:前置/后置/异常通知的函数都没有返回值,只有环绕通知有返回值  
    @Component //使用自动注解的方式实例化并初始化该类  
    @Aspect  
    public class TestInterceptor {  

        //如果要设置多个切点可以使用 || 拼接  
        @Pointcut("execution(* cn.slimsmart.spring.demo.aop.UserServiceImpl.*(..))")  
        private void anyMethod() {  
        }// 定义一个切入点  

        @Before(value="anyMethod()")  
        public void doBefore(JoinPoint joinPoint) {  
            System.out.println("前置通知");  
        }  

        @AfterReturning(value="anyMethod()",returning="result")  
        public void doAfter(JoinPoint jp, String result) {  
            System.out.println("后置通知");  
        }  

        @After("anyMethod()")  
        public void after() {  
            System.out.println("最终通知");  
        }  

@AfterThrowing(value="execution(* cn.slimsmart.spring.demo.aop.*.*(..))",throwing="e")  
 public void doAfterThrow(JoinPoint joinPoint, Throwable e) {  
            System.out.println("异常通知");  
        }  

@Around("execution(* cn.slimsmart.spring.demo.aop.*.*(..))")  
public Object doBasicProfiling(ProceedingJoinPoint joinPoint)       throws Throwable {  
     System.out.println("进入环绕通知");  
     System.out.println("目标类名称:"+joinPoint.getTarget().getClass().getName());  
            System.out.println("方法名称:"+joinPoint.getSignature().getName());  
            System.out.println("方法参数:"+joinPoint.getArgs());  
            System.out.println("staticPart:"+ joinPoint.getStaticPart().toShortString());  
            System.out.println("kind:"+joinPoint.getKind());  
            System.out.println("sourceLocation:"+joinPoint.getSourceLocation());  
            Object object = joinPoint.proceed();// 执行该方法  
            System.out.println("退出方法");  
            return object;  
        }        
    }  

3.applicationContext.xml配置

<?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:aop="http://www.springframework.org/schema/aop"  
       xmlns:tx="http://www.springframework.org/schema/tx"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
    <context:component-scan base-package="cn.slimsmart.spring.demo" />  
     <!-- 打开aop 注解 -->  
    <aop:aspectj-autoproxy proxy-target-class="true"/>  

</beans> 

4.单元测试类

package cn.slimsmart.spring.demo;  

import org.junit.Test;  
import org.junit.runner.RunWith;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  

import cn.slimsmart.spring.demo.aop.UserService;  

@RunWith(SpringJUnit4ClassRunner.class)//让junit工作在spring环境中  
@ContextConfiguration(locations={"classpath:applicationContext.xml"})  
public class SpringTest{  

    @Autowired  
    UserService userService;  

    @Test  
    public void testStart(){  
        System.out.println("启动服务");  
        userService.delete("abc123");  
        System.out.println("====================");  
        userService.update("aaa");  
    }  

}

运行结果:执行update抛异常

    启动服务  
    进入环绕通知  
    目标类名称:cn.slimsmart.spring.demo.aop.UserServiceImpl  
    方法名称:delete  
    方法参数:[Ljava.lang.Object;@2de1c3f9  
    staticPart:execution(UserServiceImpl.delete(..))  
    kind:method-execution  
    sourceLocation:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@6ed745c2  
    前置通知  
    --------delete  
    退出方法  
    最终通知  
    后置通知  
    ====================  
    进入环绕通知  
    目标类名称:cn.slimsmart.spring.demo.aop.UserServiceImpl  
    方法名称:update  
    方法参数:[Ljava.lang.Object;@1d370b4d  
    staticPart:execution(UserServiceImpl.update(..))  
    kind:method-execution  
    sourceLocation:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@8c6fb37  
    前置通知  
    --------update  
    最终通知  
    异常通知  

转载:http://blog.csdn.net/zhu_tianwei/article/details/43352471

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值