您的位置:首页 > 编程语言 > Java开发

java--Spring之AOP面向切片和Spring的简单用法

2017-01-04 13:10 423 查看
Spring-aop的切片是基于spring框架的。

切片为什么要在Sping框架下呢?原因很简单:

① 切片中的方法是在实例对象执行方法的时候触发的,那么切片是和实例对象有关。

② 如果在代码执行过程中,对象是new出来的,那么这个new出来的实例对象怎么和切片类有关系呢?显然没有任何关系。那么要怎么样才能让切片类和实例对象有关呢?只有将实例对象放入一个容器中,要用的时候再取出来。这就是Spring框架要干的活了。所以切片方法要在spring框架下

Spring框架

spring框架简单点说就是:将对象Bean放入beanFactory中,在加载对应的配置xml的时候,就创建了实例化的bean,以后就是通过beanFactory.get方法去获取就是了。

代码为:

BeanFactory beanFactory = new ClassPathXmlApplicationContext("application.xml");
Bean bean = (Bean)beanFactory.getBean("bean");


这里面的Bean类是一个对象,对应的application.xml文件(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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="bean" class="IMP.Bean"/>
<bean id="aspectHandler" class="IMP.AspectClass"/>
</beans>


id是beanFactory中get方法的参数,class是具体对应方法的路径

Spring的maven包:

<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- spring上下文包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- spring包 可以建beanFactory-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>


介绍完spring框架,下面我们介绍aop切片

Spring-Aop 切片 Aspect注解

参考路径:http://blog.csdn.net/lmdcszh/article/details/13774947

maven工程中导入的包

<!-- Aspect -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>


话不多说,直接上代码

普通的Bean类

package IMP;

/**
* 方法中要插入切片的类
* Created by sff on 2017/1/12.
*/
public class Bean {
public void consoleString(String string){
System.out.println(string);
}
}
这个类的作用就是,在执行这个类的consoleString方法就要执行切片类中的方法

main执行类:

package IMP;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.FileInputStream;

/**
* Created by sff on 2017/1/12.
*/

public class Server {

public static void main(String[] args){

BeanFactory beanFactory = new ClassPathXmlApplicationContext("application.xml");
Bean bean = (Bean)beanFactory.getBean("bean");
System.out.println("=================================");
bean.consoleString("我是点...");
System.out.println("=================================");

Bean createBean = new Bean();  //如果是新创建的,而不是从beanFactory中拿的话,就不会执行切片中的东西了。
createBean.consoleString("你是点...");
}
}


这里做切片有两种,一种是在xml文件中写配置,一种是用Aspect注解

1. 在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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="bean" class="IMP.Bean"/>
<bean id="aspectHandler" class="IMP.AspectClass"/>

<aop:config>
<aop:aspect id="securityAspect" ref="aspectHandler">
<aop:pointcut id="check" expression="execution(* *(..))"/>
<aop:before method="checkSecurity" pointcut-ref="check"/>
</aop:aspect>
</aop:config>
</beans>


对应的切片类为:

package IMP;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
* 切片类
* Created by sff on 2017/1/12.
*/

public class AspectClass {

private void check(){};

private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}

}


结果:



2. 使用Aspect注解

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="bean" class="IMP.Bean"/>
<bean id="aspectHandler" class="IMP.AspectClass"/>
<!-- 必须要有<aop:aspectj-autoproxy/> 不然就没有切片注入的功能 -->
<aop:aspectj-autoproxy/>

</beans>


对应的切片类:

package IMP;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
* 切片类
* Created by sff on 2017/1/12.
*/
@Aspect
public class AspectClass {
@Pointcut("execution(* *(..))")
private void check(){};

/**
* 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
*/
@Before("check()")
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}
}


结果:和XML配置的一样



如果XML上配置了<aop:config>,Aspect也注解了,那么切片中的方法就会运行两次,不报异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: