Spring-day2-注解注册及aop思想

一、使用注解配置Spring

1.导包。

导入aop包。

2.为配置文件引入命名空间。

contex约束,

3.开启使用注解代理配置文件。

<context:component-scan base-pakage=" 包名"><context:component-scan>

4.在类中使用注解来完成配置。

 1)对象的注册。

@component("user")

@service("user") //web层

@controller("user")//service层

@repository("user")//dao层

分别使用service,controller和repository三个注解来区分三层的类。

component和其他的功能都一样。

2)指定对象的作用范围

@scope(scopeName=“prototype”)

3)值得注入。

在成员变量上方加上

@Value("tom")

4)引用类型的注入。

@resource(name="car")

5)注解初始化方法,和销毁方法

@PostConstruct  //初始化方法

@preDestroy  //销毁方法

二、Sping和junit的整合测试

1.引入Spring的test包。

2.注解配置

//帮我们创建容器

@RunWith(SpingJunit4ClassRunner.class)

//读取哪个配置创建容器

@ContetxConfigration("classpath:applicationContext.xml")

//将容器中的user对象注入到u变量里。

@Resource(name="user")

private User u;

三、Spring中的aop思想

1.sping中的aop就是spring帮我们生成代理对象。

1)动态代理:必须有接口才可以实现动态代理。

2)cglib:类没有接口。且不被fanal修饰。可以生成动态代理对象。利用继承。

2.aop中的名词。

  • joinpoint(连接点):目标对象中所有可以增强的方法。
  • pointcut(切入点):目标对象中,已经增强的方法。
  • advice(通知):增强的代码
  • target(目标对象):被代理的对象
  • weaving(织入):将通知写入切入点的过程
  • poxy(代理对象):将通知织入目标对象之后形成的代理对象。
  • aspect(切面):切入点+通知

四、Spring中的aop演示。

1.导包

基本的4+2之后还需要导入aop和aspect包,另外还需要导入两个第三方jar包

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

2.目标对象准备代码

package cn.guanyq.aop;

import cn.guanyq.bean.User;

public class UserServicaImpl implements Userservice {

    @Override
    public void save(User u) {
        // TODO Auto-generated method stub
    System.out.println("保存用户");

    }

    @Override
    public void update(User u) {
        // TODO Auto-generated method stub
        System.out.println("更新用户");
    }

    @Override
    public void delete(User u) {
        // TODO Auto-generated method stub
        System.out.println("删除用户");
    }

    @Override
    public void find(User u) {
        // TODO Auto-generated method stub
        System.out.println("查找用户");
    }

}

 

3.通知准备代码

package cn.guanyq.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {
   public void before(){
       System.out.println("前置通知");
   }
   public void after(){
       System.out.println("后置通知(出现异常也会调用)");
   }
   public void afterReturning(){
       System.out.println("后置通知(出现异常不会调用)");
   }
   public void afterException(){
       System.out.println("出现异常后调用");
   }
   public Object around(ProceedingJoinPoint pjg) throws Throwable{
       System.out.println("环绕通知之前的部分");
      //调用目标方法
       Object proceed = pjg.proceed();
       System.out.println("环绕通知之后的部分");
       return proceed;
   } 
}

 

4.配置

首先需要导入aop约束。(aop命名空间)

1)配置对象。

<bean name="userService" class="cn.guanyq.aop.UserServicaImpl"></bean>
<bean name="myAdvice" class="cn.guanyq.aop.MyAdvice"></bean>

 

2)aop配置

<aop:config>
   //配置切入点
   <aop:pointcut   expression="execution(* cn.guanyq.aop.*ServicaImpl.*(..))" id="pc">
   </aop:pointcut>
      <aop:aspect ref="myAdvice">
         <aop:before method="before"pointcut-ref="pc"/>
         <aop:after-returning  method="afterReturning"  pointcut-ref="pc"/>
         <aop:after method="after" pintcut-ref="pc"/>
         <aop:around method="around" pointcut-ref="pc"/>
         <aop:after-throwing method="afterExcetion" pointcut-ref="pc"/>
      </aop:aspect>
</aop:config>

 

 

首先需要配置切入点,也就是明确需要增强哪个类中的方法。

然后将通知中的方法配置到切入点中。

注意:1.execution的cn前面有一个空格。

              2.再获取对象的时候一定要利用接口获取,在下面代码的16行。一定使用接口,不能使用它的实现类。会出现下面这个异常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.guanyq.aop.Test': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userservice' must be of type [cn.guanyq.aop.UserServicaImpl], but was actually of type [com.sun.proxy.$Proxy12]

 1 package cn.guanyq.aop;
 2 
 3 
 4 import javax.annotation.Resource;
 5 
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 import cn.guanyq.bean.User;
11 
12 @RunWith(SpringJUnit4ClassRunner.class)
13 @ContextConfiguration("classpath:cn/guanyq/aop/applicationContext.xml")
14 public class Test {
15  @Resource(name="userservice")
16  private Userservice us;
17  
18  @org.junit.Test
19  public void test(){
20      us.save();
21  }
22 }

 

posted @ 2018-06-26 23:22  流浪的小蛤蟆  阅读(157)  评论(0)    收藏  举报