---
title: "spring注解"
author: "zy77612"
date: "2010-06-01T16:26:47Z"
tags: ["AOP", "Bean", "DAO", "Spring", "XML"]
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2010-06-01T16:26:47Z"
id: 681141
url: "https://zy77612.iteye.com/blog/681141"
---

一.  spring注解

1.准备工作  
(1)导入common-annotations.jar  
(2)导入schema文件 文件名为spring-context-2.5.xsd  
(3)在xml的beans节点中配置  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans   
       .......  
       xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)"

       xsi:schemaLocation="  
           .......  
           [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)  
           [http://www.springframework.org/schema/context/spring-context-2.5.xsd](http://www.springframework.org/schema/context/spring-context-2.5.xsd)"  &gt;

.....  
   &lt;!--将针对注解的处理器配置好  --&gt;  
  &lt;context:annotation-config /&gt;  
.....  
&lt;beans&gt;

2.在java代码中使用@Autowired或@Resource注解方式进行装配，这两个注解的区别是：  
@Autowired默认按类型装配，@Resource默认按名称装配，当找不到名称匹配的bean才会按类型装配。

默认注解  
@Resource  private PersonDao persondao;  
&lt;bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"&gt;&lt;/bean&gt;  
首先是判断persondao是否与xml里的personDao名字相同，相同则注入，  
不同则判断persondao是否是com.hf.dao.impl.PersonDaoBean类型，是则注入不是则返回null.

@Resource(name="personDao")  private PersonDao dao;  
&lt;bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"&gt;&lt;/bean&gt;  
判断name名称是否与bean中id相同不同则返回null

Spring自带注解方式  
@Autowired @Qualifier("personDao") private PersonDao persondao;  
默认是按类型注入  加上@Qualifier("personDao")则按名称注入

3.通过在classpath 自动扫描方式把组件纳入spring容器中管理  
spring2.5为我们引入了组件自动扫描机制，它可以在类路径底下寻找标注了@Component @Service @Controller @Repository  
注解的类，并将这些类纳入进spring容器中管理。它们的作用和xml文件中使用bean 节点配饰组件是一样的。  
(1)使用到了注解的功能(需要注解准备工作的内容)  
(2)在xml中加入  
&lt;context:component-scan base-package="com.hf" /&gt;  
其中base-package 为需要扫描的包(包含子包)  
(3)  
@Service用于标注业务层组件  
@Controller用于标注控制层组件(如struts中的action)  
@Repository用于标注数据访问组件 ，即DAO 组件  
@Component泛指组件，当组件不好归类的时候，我们可以使用这个注解进行标注。

(4)  
业务类  
@Service  
public class PersonServiceBean implements PersonService {.....}  
输出类  
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");  
PersonService personService= (PersonService)cxt.getBean("personServiceBean");  
System.out.println(personService);  
cxt.close();  
使用注解中bean的id默认名称为类名称的首字母小写名称  
\--------------------------------------------------  
自己指定名称  
@Service("aa") //默认作用域范围 是单例范围  
public class PersonServiceBean implements PersonService {.....}  
输出类  
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");  
PersonService personService= (PersonService)cxt.getBean("aa");  
System.out.println(personService);  
cxt.close();  
\--------------------------------------------------  
@Service("aa") @Scope("prototype")//修改bean的作用域  
public class PersonServiceBean implements PersonService {....}

\-----------------------------------------------------------  
   @PostConstruct  
   public void init(){   
    System.out.println("初始化");  
   }  
   @PreDestroy  
   public void destory(){    
    System.out.println("释放资源");  
   }

4.AOP注解方式  
(1)准备工作：  
.导入common-annotations.jar  aspectjrt.jar aspectweaver.jar cglib-nodep-2.13.jar  
.导入schema文件 文件名为spring-aop-2.0.xsd  
.在xml的beans节点中配置  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans   
       .......  
       xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"

       xsi:schemaLocation="  
           .......  
           [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)  
           [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd](http://www.springframework.org/schema/aop/spring-aop-2.5.xsd)"  &gt;

.....  
    &lt;!-- 配置解释处理器 为@AspectJ注解提供支持  --&gt;  
  &lt;aop:aspectj-autoproxy /&gt;  
.....  
&lt;beans&gt;

(2)  
&lt;bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"&gt;&lt;/bean&gt;  
&lt;bean id="personService" class="com.hf.service.impl.PersonServiceBean" &gt;&lt;/bean&gt;  
将切面和被拦截的类交给spring管理  
(3)切面类

@Aspect //定义切面类  
public class MyInterceptor {  
   /\**  
    *  @Pointcut("execution(* com.hf.service..\*.\*(..))")表达式含义  
    * 第一个* 表示返回值类型为任意类型  
    * com.hf.service..  两个点表示包路径下的子包的类也要拦截  
    * com.hf.service..\*.* 子包的所有类中的所有方法 第一个\*是方法第二个\*是类  
    * (..)代表方法参数随意 可有可无可多可少  
    * \*\*/

   @Pointcut("execution (* com.hf.service.impl.PersonServiceBean.\*(..))")// 定义切入点  
   private void andMethod()//声明一个切入点  
   {}  
    
/*   @Before("andMethod()")  
   public void doAccessCheck(){  
    System.out.println("前置通知");     
   }  
   \*/  
    
   @Before("andMethod() && args(name)") //带参数 只拦截符合参数类型的方法  
   public void doAccessCheck(String name){  
    System.out.println("前置通知"+name);     
   }  
    
    
/*   @AfterReturning("andMethod()")  
   public void doFaterReturning(){     
    System.out.println("后置通知");  
   }\*/  
    
   @AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null  
   public void doFaterReturning(String result){//拦截方法执行后 获取返回值对象  
    System.out.println("后置通知:"+result);  
   }  
    
    
   @After("andMethod()")  
   public void doAfter(){  
    System.out.println("最终通知");   
   }  
    
/*   @AfterThrowing("andMethod()")  
   public void doAfterThrowing(){     
    System.out.println("例外通知");  
   }\*/  
    
   @AfterThrowing(pointcut="andMethod()" , throwing="e") //获取例外并打印  
   public void doAfterThrowing(Exception e){    
    System.out.println("例外通知:"+e);  
   }  
    
    
   @Around("andMethod()")//环绕通知  
   public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{  
   //if(){//判断是否与权限  
    System.out.println("进入通知");  
    Object result = pjp.proceed();  
    System.out.println("离开 通知");  
   //}  
    return result;  
     
     
   }  
}

(4)业务类 PersonServiceBean  
public class PersonServiceBean implements PersonService {

 public void save(String name){  
      throw new RuntimeException("纯属例外");  
      // System.out.println("我是Save方法"+name);  
    }  
 public String update() {   
  return "我是update方法";  
 }  
}

 

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/ELuYouNi/archive/2009/05/09/4162183.aspx](http://blog.csdn.net/ELuYouNi/archive/2009/05/09/4162183.aspx)