<?xml version="1.0" encoding="GBK"?> <project name="spring" basedir="." default=""> <property name="src" value="src"/> <property name="dest" value="classes"/> <path id="classpath"> <fileset dir="../../lib"> <include name="**/*.jar"/> </fileset> <pathelement path="${dest}"/> </path> <target name="compile" description="Compile all source code"> <delete dir="${dest}"/> <mkdir dir="${dest}"/> <copy todir="${dest}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <javac destdir="${dest}" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <src path="${src}"/> <classpath refid="classpath"/> <compilerarg value="-Xlint:deprecation"/> </javac> </target> <target name="run" description="Run the main class" depends="compile"> <java classname="lee.BeanTest" fork="yes" failonerror="true"> <classpath refid="classpath"/> </java> </target> </project>
<?xml version="1.0" encoding="GBK"?> <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-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/aop HTTP: // www.springframework.org/schema/aop/spring-aop-4.0.xsd "> <- specify automatic search Bean components, automated search section category ->! < context: Scan-base- Component Package = "org.crazyit.app.service , org.crazyit.app.aspect"> <context: the include filter-type = "Annotation" expression The = "org.aspectj.lang.annotation.Aspect "/> </ context: the Component-Scan> <- start @AspectJ support ->! <AOP: AspectJ-autoproxy /> </ Beans>
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ public class BeanTest { public static void main(String[] args) { // 创建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = ctx.getBean("hello" , Hello.class); hello.foo(); hello.addUser("孙悟空" , "7788"); World world = ctx.getBean("world" , World.class); world.bar(); } }
package org.crazyit.app.aspect; import org.aspectj.lang.annotation.*; import org.aspectj.lang.*; import java.util.Arrays; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 FourAdviceTestclasspublicthe @Aspect defining aspects//* / // {Around enhancement process defined @Around ( "Execution (org.crazyit.app.service.impl *. *. * (..))" ) public Object processTx (JP ProceedingJoinPoint) throws java.lang.Throwable { System.out.println ( "Around enhanced: before executing the target method to simulate begin transaction ..." ); // access performance parameters of the target method Object [] args = jp.getArgs (); // when there is objective method of execution parameters, // and the first parameter is a string parameter IF (args! = null && args.length> 0 && args [0] .getClass () == string. class ) { // the first parameter to modify certain parameters of the method call args [0] = "[increased] prefix" + args [0 ]; } // execution of the target method, and return value after execution of the target method Object RVT = jp.proceed (args); System.out.println ( "Around enhancement: after executing the target method to simulate the end of the transaction ..." ); // if rvt type is Integer, the rvt changed its square IF (rvt =! null && rvt instanceof Integer) rvt = (Integer) * RVT (Integer) RVT; return RVT; } // definition of reinforcement treatment Before @Before (. ". Execution (org.crazyit.app.service.impl * * * (..))" ) public void Authority (the JoinPoint JP ) { System.out.println ( "the Before enhancement: Analog permission checks" ); // returns the weaving method of enhancing the target process System.out.println ( "Before enhancement: the target method is woven reinforcement treatment is:" + jp.getSignature () getName ());. // parameter access execution of the target method System.out.println ( "Before enhanced: parameters of the target method is:" + Arrays.toString (jp.getArgs ())); / / access process is enhanced target object System.out.println ( "Before enhanced: is woven into a target object processing is enhanced:" + jp.getTarget ()); } // define AfterReturning enhancement process @AfterReturning (pointcut = " Execution (org.crazyit.app.service.impl *. *. * (..)) " , returning ="rvt ") Public void log (the JoinPoint JP, Object RVT) { System.out.println ( "Enhanced afterReturning: acquiring the target method returns the value:" + RVT); System.out.println ( "afterReturning enhancement: Analog logging function ... " ); // returns the weaving method of enhancing the target process System.out.println (" afterReturning enhancement: the target method is woven reinforcement treatment is: " + jp.getSignature (). getName ()); // access the method of execution of the target parameter System.out.println ( "afterReturning enhanced: parameters of the target method is:" + of Arrays.toString (jp.getArgs ())); // access the target object of processing is enhanced System.out.println ( "afterReturning enhanced: is knitted enhancement processing target audience is: " + jp.getTarget ()); } // define After enhancement process @After ( "execution (* org.crazyit.app.service .. the .impl * * (..)) " ) public void release (the JoinPoint JP) { System.out.println ( " enhanced the after: release resources after the end of the simulation method ... " ); // return is knitted the method of enhancing the target process System.out.println ( "After enhancement: the target method is woven reinforcement treatment is:" + jp.getSignature () getName ().); // access execution parameters of the target method System.out. println ( "After enhanced: parameters of the target method is:" + of Arrays.toString (jp.getArgs ())); // access the target object is enhanced process System.out.println ( "After Enhanced: enhanced audience is woven into the process as follows:" + JP.getTarget ()); } }
Package Penalty for org.crazyit.app.service; / ** * the Description: * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> * the Copyright (C), 2001-2016, Yeeku.H.Lee * This Program IS protected by a Copyright Laws. * a Program the Name: * a a Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 * / public interface the Hello { // definition of a simple method, the business logic of the application method for simulating void foo (); // define the addUser a () method of adding a user, simulate the application of the method int the addUser (String name, String Pass); }
Package Penalty for org.crazyit.app.service; / ** * the Description: * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> * the Copyright (C), 2001-2016, Yeeku.H.Lee * This Program IS protected by a Copyright Laws. * a Program the Name: * a a Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 * / public interface World { // definition of a simple method, the business logic of the application method for simulating public void bar (); }
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ @Component("hello") public class HelloImpl implements Hello { // definition of a simple method, the business logic of the application method for simulating public void foo () { System.out.println ( "Hello execution component foo () method" ); } // define the addUser a () method, the analog application of the method to add a user public int addUser (String name, String Pass) { System.out.println ( "Hello execution component addUser Add user:" + name); return 20 is ; } }
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 */ @Component("world") public class WorldImpl implements World { // definition of a simple method, the business logic of the application method for simulating public void bar () { System.out.println ( "bar () method performed World assembly" ); } }