day43 Sping AOP @AspectJ

        前文介绍了基于Schema的AOP支持,这里再聊一聊spring aop 的@AspectJ支持,顾名思义,也就是spring aop的注解实现。

先来个热身聊一聊Ioc 注解和Di 注解


一、Ioc 注解和Di 注解

看个例子
 xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- <context:annotation-config> 引入解析器 -->	 
	 <context:component-scan base-package="cn.zjy.spring.di"/>
	 <!--   1、 该注解解析器包含了两个功能:依赖注入和类扫描
       		2、在base-package包及子包下查找所有的类
	  -->
</beans>
Person,java
package cn.zjy.spring.di;


import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Person {
	
	private String name;
	private String password;
	
//	@Resource
    @Autowired
    @Qualifier("student")
	private Student student;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	


}
Student.java
package cn.zjy.spring.di;
import org.springframework.stereotype.Component;

@Component
public class Student {
	public void show()
	{
		System.err.println("student show ");
	}

}

Test测试
package cn.zjy.spring.di;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1	{

	@Test
	public void test1(){
		ApplicationContext applicationcontext = new ClassPathXmlApplicationContext("cn/zjy/spring/di/applicationContext.xml");
		Person person =  (Person)applicationcontext.getBean("person");
		person.getStudent().show();
	}
}

output:student show 


二、原理解析

   @Resource注解的使用规则:

   1、在spring的配置文件中导入命名空间

         xmlns:context="http://www.springframework.org/schema/context"

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-2.5.xsd

   2、引入注解解析器

        <context:annotation-config></context:annotation-config>

   3、在spring的配置文件中把bean引入进来

   4、在一个类的属性上加

            @Resource(name="student_annotation")

            privateStudent student;

         从该注解本身

               @Target({TYPE, FIELD, METHOD})

              @Retention(RUNTIME)

               public@interface Resource {

                 String name() default "";

               }

           1、该注解可以用于属性上或者方法上,但是一般用户属性上

           2、该注解有一个属性name,默认值为""

   5、分析整个过程:

        1、当启动spring容器的时候,spring容器加载了配置文件

        2、在spring配置文件中,只要遇到bean的配置,就会为该bean创建对象

        3、在纳入spring容器的范围内查找所有的bean,看哪些bean的属性或者方法上加有@Resource

        4、找到@Resource注解以后,判断该注解name的属性是否为""(name没有写)

              如果没有写name属性,则会让属性的名称的值和springID的值做匹配,如果匹配成功则赋值

                                       如果匹配不成功,则会按照类型进行匹配,如果匹配不成功,则报错

              如果有name属性,则会按照name属性的值和springbeanID进行匹配,匹配成功,则赋值,不成功则报错



   类扫描的注解:

        1、在spring的配置文件中导入命名空间

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

        2、<context:component-scanbase-package="cn.itcast.annotation.scan"></context:component-scan>

1、 该注解解析器包含了两个功能:依赖注入和类扫描

2、在base-package包及子包下查找所有的类

        3、如果一个类上加了@Component注解,就会进行如下的法则

如果其value属性的值为""

@Component

publicclass Person {}

==

<beanid="person" class="..Person"> //注意这里的id开头字母是小写的

如果其value属性的值不为""

@Component("p")

publicclass Person {}

==

<beanid="p" class="..Person">

      4、按照@Resource的法则再次进行操作

相关注解

依赖注入

@Resource

@Autowired

@Qualifier

类扫描

@Component

@Controller

@Repository

@Service


三、spring aop 注解实现

了解了Ioc 和Di的注解,那么AOP的注解学习也就水到渠成了。

首先还是老样子,引入lib\aspectj 下的jar包

说一句题外话,为什么要引入jar包

————

spring AOP就是用aspectj来实现的,是依赖关系!
  AspectJ是动态代理的一种实现!而spring默认使用的就是AspectJ来实现的动态代理,
  spring自己的AOP就是使用AspectJ来实现的!当然你也可以使用其他的实现,如cglib!


修改xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<aop:aspectj-autoproxy/>
	<!-- 通过在你的Spring的配置中引入下列元素来启用Spring对@AspectJ的支持:
	 -->
	<context:component-scan base-package="cn.zjy.SpringAop.annotation"></context:component-scan>
	
	
</beans>
当类扫描的之后,会发现aop的注解,从而实现aop的配置,

这里用一个以前的hibernate + spring 实现

通过切面的前后通知,实现对事务的提交

这里只放出关键代码,项目架构如下



切面

package cn.zjy.SpringAop.annotation;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
@Component
@Aspect
//需要映入jar包
public class MyTransaction {
	private Transaction transaction;
	
	@Pointcut("execution(* cn.zjy.SpringAop.annotation.PersonDaoImpl.*(..))")
	public void aa(){}
	

	@Before("aa()")
	public void beginTransaction(JoinPoint jp){
		System.out.println(jp.getSignature().getName());//选择没有process方法�?
		
		this.transaction = SessionFactoryUtils.sessionfactory.getCurrentSession().beginTransaction();
	}
	@AfterReturning("aa()")
	public void commit(){
		this.transaction.commit();
	}
}
目标类

package cn.zjy.SpringAop.annotation;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Component;

@Component
public class PersonDaoImpl implements PersonDao{

	

	public void deletePerson(Serializable id) {
		Person person = (Person)SessionFactoryUtils.sessionfactory.getCurrentSession().get(Person.class, 2L);
		SessionFactoryUtils.sessionfactory.getCurrentSession().delete(person);
	}

	public String savePerson(Person person) {
		SessionFactoryUtils.sessionfactory.getCurrentSession().save(person);
		return "success";
	}

	public void updatePerson(Person person) {
		
		SessionFactoryUtils.sessionfactory.getCurrentSession().update(person);
	}

}
测试即可

package cn.zjy.SpringAop.annotation;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringAoptest {
	@Test
	public void test()
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("cn/zjy/SpringAop/annotation/applicationContext.xml");
		PersonDao persondao = (PersonDao)context.getBean("personDaoImpl");
		
		Person person = new Person();
		person.setPname("haha");
		person.setPsex("nan");
		persondao.savePerson(person);
	}

}

这里我测试的时候碰到一个问题,以后要小心了

org.hibernate.MappingException: Unknown entity,可能是没有在cfg文件中加入 *.hbm.xml造成的




四、总结

Spring实现动态代理配置是有两种配置文件:1、xml文件方式;2、annotation方式(使用AspectJ类库实现的。

xml 虽然书写麻烦,但是效率高,
annotation虽然方便,但是效率不高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值