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

spring aop 基于xml配置版

2016-03-11 00:00 691 查看
maven

<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.myaop</groupId>
<artifactId>spring-aop</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.2.RELEASE</spring.version>
</properties>
<build />
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>

applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://w
7fe0
ww.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="com.spring"></context:component-scan>
<context:component-scan base-package="com.aop"></context:component-scan>
<bean id="aspectAop" class="com.aop.Aop"></bean>
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.spring..*.*Do(..)) " />
<aop:aspect id="aspect" ref="aspectAop">
<aop:around method="doAround" pointcut-ref="pc" />
</aop:aspect>
</aop:config>

</beans>

package com.aop;
import javax.annotation.Resource;

import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.ui.ModelMap;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.AfterThrowing;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Repository;

//@Repository
//@Component
//@Aspect
public class Aop implements AopInfrastructureBean {
private Boolean success = true;
private String err = "";

//@Pointcut("execution(* com.spring..*.*Do(..)) ")
public void pointCut() {
}

//@Before("@annotation(auth)")
public void before(JoinPoint joinPoint, Resource res) {
res.toString();
System.out.println(res);
// //如果需要这里可以取出参数进行处理
// Object[] args = joinPoint.getArgs();
// System.out.println("Before:"+args[0]);
}

//@After("pointCut()")
public void after(JoinPoint joinPoint) {
// Object[] args = joinPoint.getArgs();
// System.out.println("after:"+args[0]);
}

//@AfterReturning(pointcut = "pointCut()")
public void afterReturning(JoinPoint joinPoint) {
// Object[] args = joinPoint.getArgs();
// Gson gson =new Gson();
// args[0]= gson.toJson(args[0]);
// Map<String, Object> map =new HashMap<String, Object>();
// if(success)
// {
// map.put("states", true);
// }
// else
// {
// map.put("states", false);
// map.put("code", "0");
// map.put("msg", "失败了");
// }
// map.put("value",returnVal );
// System.out.println("afterReturn return:" + gson.toJson(returnVal));
// //args[0]=map;
// System.out.println("afterReturn param:"+args[0]);
// System.out.println("afterReturn return:" + gson.toJson(map));
// System.out.println("afterReturn return:" + gson.toJson(returnVal));
}

//@Around("pointCut()")
public Object around(ProceedingJoinPoint pjp)  {
ModelMap map ;
try {
//获取原始返回值
map = (ModelMap) pjp.proceed();
Signature signature= pjp.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();
String name=returnType.getName();
String[] type=name.split("\\.");
if(name.contains("ModelMap"))
{
map.put("states", true);
}
} catch (Throwable ex) {
map =new ModelMap();
map.put("err", ex.getMessage());
}
return  map;
}

//@AfterThrowing(pointcut = "pointCut()", throwing = "error")
public void afterThrowing(JoinPoint jp, Throwable error) {
// System.out.println("throw:" + error);
// err="出错了";
// success=false;
}
}

package com.spring;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.google.gson.annotations.Until;

@Service
public class User {
private String name;
private String sex;
private int age;
private List<Friend> friends;
User(){
friends=new ArrayList<Friend>();
}
public String getName() {
return name;
}
public void setName(String name) {
name= "姓名:"+name;
this.name =name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age>120){
throw new Exception("年龄超值");
}
this.age = age;
}

public List<Friend> getFriends() {
return friends;
}
public ModelMap  addDo(Friend friend) {
this.friends.add(friend);
ModelMap map=new ModelMap();
map.addAttribute("value", friend);
return map;
}

public ModelMap delDo(Friend friend) throws Exception {
ModelMap map=new ModelMap();
if(true){
throw new Exception("錯誤的參數");
}
return map;
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", age=" + age
+ ", friends=" + friends + "]";
}
}

package com.spring.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.gson.Gson;
import com.spring.Friend;
import com.spring.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })

public class userTest {
@Autowired
private ApplicationContext ac;
@Autowired
private User user;
@Autowired
private Friend friend;
@Autowired
private Friend friend2;
@Before
public void setUp() throws Exception {
}

@Test
public void testNew() throws Exception{

friend.setName("李梅");
friend.setAge(18);
friend.setSex("女");
Object obj=	  user.addDo(friend);
System.out.println("最终返回"+obj);
friend2.setName("王淘");
friend2.setAge(33);
friend2.setSex("男");
Object obj2=	  user.delDo(friend2);
System.out.println("最终返回"+obj2);
//	  user.setName("peter");
//	  user.setSex("男");

//  user.setAge(166);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: