Spring注解

来源:互联网 发布:ubuntu 17.04 输入法 编辑:程序博客网 时间:2024/04/29 01:21


一.  spring注解

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

       xsi:schemaLocation="
           .......
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"  >

.....
   <!--将针对注解的处理器配置好  -->
  <context:annotation-config />
.....
<beans>

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


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

@Resource(name="personDao")  private PersonDao dao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
判断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中加入
<context:component-scan base-package="com.hf" />
其中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节点中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="
           .......
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  >

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

(2)
<bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"></bean>
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" ></bean>
将切面和被拦截的类交给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方法";
 }
}

0 0
原创粉丝点击