Original link:
http://www.cnblogs.com/c0liu/p/7469186.html
spring-2 1. Xml <!-- \src\applicationContext-annotation.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!- ->configured to automatically scan packages <context:component-scan base-package="com.atguigu.spring.ref"></context:component-scan> </beans> <!-- \src\applicationContext-aop.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop HTTP: / /www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0 .xsd " > ! <- automatically scan packages -> < context: scan-Component Base-package =" com.atguigu.spring.aop " > </ context: Component-scan > ! <- make the AspectJ annotations function -> < AOP: AspectJ-the autoproxy > </ AOP: AspectJ-the autoproxy > </beans> <-! \ Src \ 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" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans / Spring-beans.xsd " > <-! 1. By default, the bean IOC container is if the object is a single example of a single embodiment, the created instance is created when the bean IOC container, and! bean properties initialized. 2. scope attribute can be modified by the scope of the bean when the bean value of the prototype, the prototype of bean: each instance to obtain containers, a new object is obtained. Further create an instance of the bean, the IOC does not create container. 3. IOC container bean life cycle: 3.1 In general, the discussion lifecycle of the bean, is built in the bean Is based on a single case can be specified for the bean init and destroy methods 3.2 lifecycle methods richer 3.3 bean can also be post-processor via bean ( interview).. -> < the bean ID = "the helloWorld" class = "com.atguigu.spring.helloworld.HelloWorld" scope = "Singleton" the init-Method = "the init" the destroy-Method = "the destroy" > < Property name = "the userName" value = "atguigu" > </ Property > </ the bean > <-! . 1.Disposed in the relationship between the IOC bean container -> <bean id="userDao" class="com.atguigu.spring.ref.UserDao"></bean> <bean id="userService" class="com.atguigu.spring.ref.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="com.atguigu.spring.ref.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans>
2. Java // \src\com\atguigu\spring\aop\ArithmeticCalculator.java package com.atguigu.spring.aop; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } // \src\com\atguigu\spring\aop\ArithmeticCalculatorImpl.java package com.atguigu.spring.aop; import org.springframework.stereotype.Component; @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } } // \src\com\atguigu\spring\aop\ArithmeticCalculatorLoggingImpl.java package com.atguigu.spring.aop; public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { System.out.println("The method add begins with [" + i + "," + j + "]"); int result = i + j; System.out.println("The method add ends with " + result); return result; } @Override public int sub(int i, int j) { System.out.println("The method sub begins with [" + i + "," + j + "]"); int result = i - j; System.out.println("The method sub ends with " + result); return result; } @Override public int mul(int i, int j) { System.out.println("The method mul begins with [" + i + "," + j + "]"); int result = i * j; System.out.println("The method mul ends with " + result); return result; } @Override public int div(int i, int j) { System.out.println("The method div begins with [" + i + "," + j + "]"); int result = i / j; System.out.println("The method div ends with " + result); return result; } } // \src\com\atguigu\spring\aop\ArithmeticCalculatorLoggingProxy.java package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; public class ArithmeticCalculatorLoggingProxy { //要代理的对象 private ArithmeticCalculator target; public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) { super(); this.target = target; } //返回代理对象 public ArithmeticCalculator getLoggingProxy(){ ArithmeticCalculator proxy = null; ClassLoader loader = target.getClass().getClassLoader(); Class [] interfaces = new Class[]{ArithmeticCalculator.class}; InvocationHandler h = newOf InvocationHandler () { / ** * Proxy: a proxy object. Objects generally do not use the * method: method being invoked * args: method call parameters passed * / @Override public Object Invoke (Object Proxy, Method, Method, Object [] args) throws the Throwable { String methodName = method.getName (); // print log System.out.println ( "[before] method of The" + methodName + "Begins with" + Arrays.asList (args)); // call to the target method Object Result = null ; the try { // front notification Result = Method.invoke (target, args); // return notification, access to the return value } the catch (a NullPointerException E) { e.printStackTrace (); // abnormality notification, the access method the abnormal } // post notification methods might as abnormal, it can not access the return value. // print log System.out.println ( "[after] method of the ends with" + Result); return Result; } }; / ** * Loader: class loader proxy objects. * Interfaces: Specify the proxy object type that is what method can proxy proxy object. * H: DETAILED invoked when the proxy object, how to respond, actually invoke method call InvocationHandler * / Proxy = (ArithmeticCalculator) the Proxy.newProxyInstance (Loader, the interfaces, H); return Proxy; } } // \ src \ COM \ atguigu \ the Spring \ AOP \ LoggingAspect.java Package Penalty for com.atguigu.spring.aop; Import java.util.Arrays; Import org.aspectj.lang.JoinPoint; Import org.aspectj.lang.annotation.After; Import org.aspectj.lang.annotation.Aspect; Import org.aspectj.lang.annotation.Before; Import org.springframework.stereotype.Component; / ** * AOP's helloWorld * 1. 加入 jar 包 * com.springsource.net.sf.cglib-2.2.0.jar * com.springsource.org.aopalliance-1.0.0.jar * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar * the Spring-Aspects-4.0.0.RELEASE.jar * * 2. Add the aop namespace in the Spring configuration file. * * 3. annotation-based approach to the use of AOP packets * 3.1 automatic scanning configuration in the configuration file: <context: component-scan base -package = "com.atguigu.spring.aop"> </ context: component-scan > * 3.2 AspjectJ annotations added to make the configuration function: <AOP: AspectJ-the autoproxy> </ AOP: AspectJ-the autoproxy> . * automatically generated to match the dynamic proxy class objects * * aspect class 4. Preparation of: * a general 4.1 Java class . * 4.2 to add extra functionality implemented * * 5. configuration section * 5.1 IOC must be cut in the bean: actually add a comment @Component * 5.2 declaration is a section: Add @Aspect * 5.3 Disclaimer notice: the method function corresponding extra added. * 5.3.1 Before advice: @Before (. "Execution (com.atguigu.spring.aop.ArithmeticCalculator public int * (int, int))") * @Before representation execution @Before mark before the target method execution the method body. * @Before which is the entry point expression: * * 6. in the notice access connection details: You can add JoinPoint types of parameters in the notification process, which allows access to the parameters and methods signature method. * * 7. @After a rear facing notice: code executed after the method is executed. * / // by adding a declaration bean @Aspect annotation is a cut! @Aspect @Component public class LoggingAspect { @Before ( "execution (public int . com.atguigu.spring.aop.ArithmeticCalculator * (int, int)) " ) public void beforeMethod (the JoinPoint Joinpoint) { String methodName = joinPoint.getSignature () getName ().; Object [] args = joinPoint.getArgs(); System.out.println("The method " + methodName + " begins with " + Arrays.asList(args)); } @After("execution(* com.atguigu.spring.aop.*.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends"); } } // \src\com\atguigu\spring\aop\Main.java package com.atguigu.spring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl(); // // arithmeticCalculator = // new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy(); // // int result = arithmeticCalculator.add(11, 12); // System.out.println("result:" + result); // // result = arithmeticCalculator.div(21, 3); // System.out.println("result:" + result); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-aop.xml"); ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator"); System.out.println(arithmeticCalculator.getClass().getName()); int result = arithmeticCalculator.add(11, 12); System.out.println("result:" + result); result = arithmeticCalculator.div(21, 3); System.out.println("result:" + result); } } // \src\com\atguigu\spring\helloworld\HelloWorld.java package com.atguigu.spring.helloworld; public class HelloWorld { //字段 private String user; public HelloWorld() { System.out.println("HelloWorld's constructor..."); } //JavaBean 使用 setter 和 getter 来定义属性 public void setUserName(String user) { System.out.println("setUserName:" + user); this.user = user; } public void hello(){ System.out.println("Hello:" + user); } public void init(){ System.out.println("init method..."); } public void destroy(){ System.out.println("destroy method..."); } } // \src\com\atguigu\spring\helloworld\Main.java package com.atguigu.spring.helloworld; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main (String [] args) { // 1. Create IOC container the ClassPathXmlApplicationContext CTX = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" ); // 2. Examples acquired from IOC bean container HelloWorld helloWorld = (HelloWorld) ctx. the getBean ( "the helloWorld" ); // 3. calls the bean's method helloWorld.hello (); the HelloWorld HelloWorld2 = (the HelloWorld) ctx.getBean ( "the helloWorld" ); System.out.println (the helloWorld == HelloWorld2); // 4. Close the container ctx.close (); } } // \src\com\atguigu\spring\ref\Main.java package com.atguigu.spring.ref; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1. 创建 IOC 容器 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-annotation.xml"); UserAction userAction = (UserAction) ctx.getBean("userAction"); userAction.execute(); } } // \src\com\atguigu\spring\ref\UserAction.java package com.atguigu.spring.ref; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserAction { private UserService userService; @Autowired public void setUserService(UserService userService) { this.userService = userService; } public void execute(){ System.out.println("execute..."); userService.addNew(); } } // \src\com\atguigu\spring\ref\UserDao.java package com.atguigu.spring.ref; import org.springframework.stereotype.Repository; @Repository public class UserDao { public void save(){ System.out.println("UserDao's save..."); } } // \src\com\atguigu\spring\ref\UserService.java package com.atguigu.spring.ref; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void addNew(){ System.out.println("addNew..."); userDao.save(); } }
Reproduced in: https: //www.cnblogs.com/c0liu/p/7469186.html