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

Spring再学习-Aop注解开发(三)

2018-03-26 15:26 441 查看

1. 文件结构



2.pom文件导入包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.wyyblog.demo</groupId>
<artifactId>spring_aop01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring_aop01</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-cas</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project>


3.Log.java 

package com.wyyblog.spring_aop.log;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.MethodBeforeAdvice;

/**
* @author wyyblog.top
* @Aspect 声明切面
*/
@Aspect
public class Log{

@Before("execution(* com.wyyblog.spring_aop.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("方法执行前被执行");
}

@After("execution(* com.wyyblog.spring_aop.service.UserServiceImpl.*(..))")
public void after() {
System.out.println("方法执行后被执行");
}

@Around("execution(* com.wyyblog.spring_aop.service.UserServiceImpl.*(..))")
public Object around(ProceedingJoinPoint jPoint) throws Throwable{
System.out.println("环绕前");
System.out.println("签名:" + jPoint.getSignature());
Object result = jPoint.proceed();//执行方法
System.out.println("环绕后");
System.out.println("返回值:" + result);
return result;
}
}


4.UserService.java接口

package com.wyyblog.spring_aop.service;

public interface UserService {

public void add();
public void search();
public void delete();
public void update();

}


5.UserServiceImpl.java接口实现

package com.wyyblog.spring_aop.service;

public class UserServiceImpl implements UserService{

public void add() {
System.out.println("add");
}
public void delete() {
System.out.println("delete");
}
public void search() {
System.out.println("search");
}
public void update() {
System.out.println("update");
}

}


6.beans.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 
<bean id="userService" class="com.wyyblog.spring_aop.service.UserServiceImpl"/>
<bean id="log" class="com.wyyblog.spring_aop.log.Log"/>

<!-- 开启切面扫描 -->
<aop:aspectj-autoproxy />

</beans>

7.测试类

package com.wyyblog.spring_aop.test;

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

import com.wyyblog.spring_aop.service.UserService;

public class Test {

public static void main(String[] args) {
Applica
97bb
tionContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService)context.getBean("userService");
userService.update();
}
}



spring aop就是将公共的业务(如日志、安全)和领域业务结合,当执行领域业务时将会把公共业务加进来,实现公共业务的重复利用。领域业务将变得更纯粹。而程序员就是专注于领域业务,其本质就是动态代理
动态代理:动态代理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: