好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring 自动装配和aop的使用

使用注解配置 spring

一、步骤

1.为主配置文件引入新的命名空间(约束)
导入spring-context-4.2.xsd schema约束

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

?

1

2

3

4

// 在applicationcontext.xml中

// 指定扫描cn.zhli13.bean包下所有类的注解

// 扫描时会扫描指定包下的所有子孙包

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

3.在类中使用注解完成配置

?

1

// @componet等

二、将对象注册到容器

?

1

2

3

4

5

// 将user注册到spring容器中,相当于<bean name="user" class="cn.zhli13.bean.user"></bean>

@componet ( "user" )

@service ( "user" ) // service层

@controller ( "user" ) // web层

@repository ( "user" ) // dao层

三、修改对象的作用范围

?

1

2

// 指定对象的作用域

@scope (scopename= "prototypo" ) // 非单例模式

四、值类型注入

?

1

2

3

4

5

6

7

8

// 1.通过反射的field赋值,破坏了封装性

@value ( "tom" )

private string name;

// 2.通过set方法赋值,推荐使用

@value ( "tom" )

public void setname(string name) {

   this .name = name;

}

五、引用类型注入

?

1

2

3

4

@autowired // 自动装配

// 问题:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象

@qualifier ( "car2" ) // 使用@qualifier注解告诉spring容器自动装配哪个名称的对

private car car;

六、初始化、销毁方法

?

1

2

3

4

5

6

7

8

@postconstruct // 在对象创建后调用,xml配置中的init-method

public void init () {

   system.out.println( "init" );

}

@predestory // 在对象销毁之前调用,xml配置中的destory-method

public void destory () {

   system.out.println( "destory" );

}

spring与junit整合测试

一、导包

额外导入

二、配置注解

?

1

2

3

4

5

6

7

8

9

// 帮我们创建容器

@runwith ( "springjunit4classrunner" )

// 指定创建容器时使用哪个配置文件

@contextconfiguration ( "classpath:applicationcontext.xml" )

public class demo {

   // 将名为user的对象注入到变量u中

   @resource (name= "user" )

   private user u;

}

三、测试

?

1

2

3

4

@test

public void fun1() {

   system.out.println(u);

}

spring中的 aop

一、概念

aop思想:横向重复、纵向抽取

aop概念:spring能够为容器中管理的对象生成动态代理

二、spring实现aop的原理

1.动态代理(优先)
被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术

2.cglib代理(没有接口)
第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.

三、aop名词学习

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

spring aop的使用

一、导包

?

1

2

3

4

5

6

// spring的aop包

spring-aspects- 4.2 . 4 .release.jar

spring-aop- 4.2 . 4 .release.jar

// spring需要第三方aop包

com.springsource.org.aopalliance- 1.0 . 0 .jar

com.springsource.org.aspectj.weaver- 1.6 . 8 .release.jar

二、准备目标对象

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class userserviceimpl implements userservice {

   @override

   public void save() {

     system.out.println( "保存用户!" );

   }

   @override

   public void delete() {

     system.out.println( "删除用户!" );

   }

   @override

   public void update() {

     system.out.println( "更新用户!" );

   }

   @override

   public void find() {

     system.out.println( "查找用户!" );

   }

}

三、准备通知

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

// 1.使用注解方式

// 表示该类是一个通知类

@aspect

public class myadvice {

   @pointcut ( "execution(* cn.zhli13.service.*serviceimpl.*(..))" )

   public void pc(){}

   //前置通知

   //指定该方法是前置通知,并制定切入点

   @before ( "myadvice.pc()" )

   public void before(){

     system.out.println( "这是前置通知!!" );

   }

   //后置通知

   @afterreturning ( "execution(* cn.zhli13.service.*serviceimpl.*(..))" )

   public void afterreturning(){

     system.out.println( "这是后置通知(如果出现异常不会调用)!!" );

   }

   //环绕通知

   @around ( "execution(* cn.itcast.zhli13.*serviceimpl.*(..))" )

   public object around(proceedingjoinpoint pjp) throws throwable {

     system.out.println( "这是环绕通知之前的部分!!" );

     object proceed = pjp.proceed(); //调用目标方法

     system.out.println( "这是环绕通知之后的部分!!" );

     return proceed;

   }

   //异常通知

   @afterthrowing ( "execution(* cn.zhli13.service.*serviceimpl.*(..))" )

   public void afterexception(){

     system.out.println( "出事啦!出现异常了!!" );

   }

   //后置通知

   @after ( "execution(* cn.itcast.zhli13.*serviceimpl.*(..))" )

   public void after(){

     system.out.println( "这是后置通知(出现异常也会调用)!!" );

   }

}

// 2.使用xml配置

// 移除上述通知类的注解就是xml配置的通知类

四、配置进行织入,将通知织入目标对象中

?

1

2

// 1.使用注解配置

<!-- 准备工作: 导入aop(约束)命名空间 -->

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<!-- 1 .配置目标对象 -->

<bean name= "userservice" class = "cn.zhli13.service.userserviceimpl" ></bean>

<!-- 2 .配置通知对象 -->

<bean name= "myadvice" class = "cn.zhli13.aop.myadvice" ></bean>

<!-- 3 .开启使用注解完成织入 -->

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

// 2.使用xml配置

<!-- 准备工作: 导入aop(约束)命名空间 -->

<!-- 1 .配置目标对象 -->

<bean name= "userservice" class = "cn.zhli13.service.userserviceimpl" ></bean>

<!-- 2 .配置通知对象 -->

<bean name= "myadvice" class = "cn.zhli13.aop.myadvice" ></bean>

<!-- 3 .配置将通知织入目标对象 -->

<aop:config>

   <!-- 配置切入点

     public void cn.zhli13.service.userserviceimpl.save()

     void cn.zhli13.service.userserviceimpl.save()

     * cn.zhli13.service.userserviceimpl.save()

     * cn.zhli13.service.userserviceimpl.*()

    

     * cn.zhli13.service.*serviceimpl.*(..)

     * cn.zhli13.service..*serviceimpl.*(..)

   -->

   <aop:pointcut expression= "execution(* cn.zhli13.service.*serviceimpl.*(..))" id= "pc" />

   <aop:aspect ref= "myadvice" >

     <!-- 指定名为before方法作为前置通知 -->

     <aop:before method= "before" pointcut-ref= "pc" />

     <!-- 后置 -->

     <aop:after-returning method= "afterreturning" pointcut-ref= "pc" />

     <!-- 环绕通知 -->

     <aop:around method= "around" pointcut-ref= "pc" />

     <!-- 异常拦截通知 -->

     <aop:after-throwing method= "afterexception" pointcut-ref= "pc" />

     <!-- 后置 -->

     <aop:after method= "after" pointcut-ref= "pc" />

   </aop:aspect>

</aop:config>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000015809544

查看更多关于spring 自动装配和aop的使用的详细内容...

  阅读:43次