Spring Annotation(IOC篇)

Jul 28 2009 Published by Tony under Java

首先在application_context的xml配置文件中加入

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

而且保证xml的schema中包括context名称空间。
@Component

//例如
@Component("member")
public class Member {
   private String id;
   }

这个Annotation主要用来定义bean。
@Service,@Controller,@Repository
这3个目前与@component同意,以后版本会细化。
@Autowired

//例如
@Component("member")
public class Member {
   private String id;
   @Autowired
   private Project project;
   }

这个Annotation主要用来自动查找context,并且关联其中的bean。
@Qualifier

@Component("member")
public class Member {
   private String id;
   private String name;
   @Autowired
   @Qualifier("project")
   private Project project;
   }

这个Annotation主要用来自动关联时的微调
@Scope

    @Scope("singleton")
    @Component("member")
    public class Member {
        private String id;
        private String name;
        @Autowired
        @Qualifier("project")
        private Project project;
        }

这个Annotation用来指定bean作用域与生成状态,默认为singleton,其他还有prototype(多个实例),request,session,global session,这3个是用在基于Spring ApplicationContext的Web项目。
在bean中我们已经看到了最基本的使用Java annotation的方式了,但是Spring对于Annotation还存在一些问题,比如:@Autowired同样可以应用于Collection中,比如List中。但是如果在List的属性中加上这个Annotation,那么通过容器初始化的bean会自动往 List中存放一个通过@Autowired自动查找出来的对象,这么做我也很难说它不对,但是还是感觉怪怪的。希望大家注意。
另外谈一下自动检测组件名称的方式(针对于@Component,@Service,@Controller,@Repository):如果给组件命名了,则按照这个名称注册到BeanFactory中,如果没有命名,则把小写开头的类名注册为组件名称。

@Component("project")
public class MyProject {
//......
}

如果是@Component,那么在ApplicationContext或者BeanFactory中注册的Bean名称是”myProject”,如果是@Component(“project”),则注册的Bean名称是”project”。
@Resource

    @Resource
    public void setOwner(Member owner) {
        this.owner = owner;
    }

或者

    @Resource(name="member")
    public void setOwner(Member owner) {
        this.owner = owner;
    }

其中特别之处在于@Resource如果加载属性或者setter之上,则按照属性名或者根据setter名称得到bean名称查找,如果找不到则按照类型查找。如果给@Resource加了name属性,则相当于按照这个名称查找,这种方式查找的优先级最高。

No responses yet

Spring Annotation(AOP篇)

Jul 28 2009 Published by Tony under Java

本文主要简单了解一下使用Java annotation的情况下,先介绍一下比较主流和简单的用法,Spring对AspectJ的支持。在ApplicationContext中加入关于AOP的命名空间,具体内容见Spring 使用AspectJ来配置AOP.首先明确一点,用作通知(Advice)的类也应该受到ApplicationContext的管理,之后再使用AspectJ的标签来管理通知动作,见代码:

//import ...
@Component
@Aspect
public class Alert {
    @Before("execution(* get*(..))")//方法运行前通知
    public void show() {
        System.out.println("before");
    }
    @After("execution(* get*(..))")//最终通知
    public void show2() {
        System.out.println("after");
    }
    @Pointcut("execution(* get*(..))")
    public void afterReturn(){}
    //方法返回值通知,注意这个通知需要与@Pointcut配合使用
    @AfterReturning(pointcut="afterReturn()",returning="value")
    public void returning(Object value){
        System.out.println("returning");
    }
    @AfterThrowing(pointcut="afterReturn()",throwing="ex")
    //异常通知,这个通知复用了afterReturn的pointcut。当然你也可以使用新的pointcut
    public void doRecoveryActions(Exception ex){
        //...
    }
    @Around("afterReturn()")
    //环绕通知。真正的方法实际上是joinPoint.proceed();
    public Object doSomeForAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
       System.out.println("before around");
       Object retVal=joinPoint.proceed();
       System.out.println("after around");
      return retVal;
    }
}

这段代码的意思就是在所有以get开头的方法被调用之前和之后都会执行show和returning,show2方法,其运行顺序是 show(),returning(),show2()。注意*与get*之间有一个空格。对一个方法,你可以同时使用Before Advice,Around Advice,AfterReturning Advice,After Advice,Throwing Advice。那么这些通知的顺序是什么呢?一个方法运行时,最先触发的是Around Advice,之后是Before Advice 是 AfterReturning(如果有的话),之后是After Advice,最后还要再通知Around。
就拿上面代码这个例子说,打印的顺序是:

before around
before
returning
after
after around

下面着重介绍一下除了execution之外的其他类型的pointcut。
within:within指的是在某个范围内所有的方法执行。
例如:
@pointcut(“within(com.xyz.project.web..*)”)就是指在com.xyz.project.web包及其子包中所有的方法被执行的时候,都会触发这个pointcut。within(com.xyz.project.web.*)这样写就只是针对这个包,而不包括这个包的子包。
this:this指的是实现了某个确定的接口的代理对象中的方法被执行。
target:target指的是实现了某个确定的接口的代理对象中的方法被执行。

No responses yet