之前用DWP项目做spring的IOC,xml总是提示有问题,之后改用maven通过。

之后将这一块的内容补充。

仔细考虑一下spring 的IOC是无处不在的,演示Aop也需要依赖spring的IOC。

spring Aop例子。

本文例子在http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html基础上进行些许修改,首先项目结构图:

spring Ioc Aop整合-LMLPHP

其次运行结果图:

spring Ioc Aop整合-LMLPHP

最开始的pom文件(property与dependency可以复制)

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>mavenAop</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenAop Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.19</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>mavenAop</finalName>
</build>
<properties>
<springVersion>3.2.5.RELEASE</springVersion>
</properties>
</project>

其次是主类的接口

package com.dragon.study;

public interface IStudent {
public void addStudent(String name);
}

主类的实现

package com.dragon.study.Impl;
import com.dragon.study.IStudent; public class StudentImpl implements IStudent { public void addStudent(String name) {
// TODO Auto-generated method stub
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
} }

前置、后置、环绕通知

package com.dragon.Advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {

    public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println( " 这是BeforeAdvice类的before方法. " );
} }
package com.dragon.Advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice {

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("这是AfterAdvice类的afterReturning方法.");
} }
package com.dragon.Advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class CompareInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
Object result = null;
String stu_name = invocation.getArguments()[0].toString();
if ( stu_name.equals("dragon")){
//如果学生是dragon时,执行目标方法,
result= invocation.proceed(); } else{
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
} return result;
} }

其次进行bean文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean> <bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property> </bean> </beans>

最后测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dragon.study.IStudent; public class Test { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx =new ClassPathXmlApplicationContext("bean.xml");
IStudent person = (IStudent)ctx.getBean("student");
person.addStudent("drasdfgon");
} }
03-31 23:57