봄 각종 조언 예시
9283 단어 자바
package com.dada.test.spring.aop.advisor;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvice implements AfterReturningAdvice
{
public void afterReturning(Object result, Method method,
Object[] parameter, Object target) throws Throwable {
System.out.println("==============after return==============");
System.out.println("result:" + result + ";method:" + method
+ ";parameter:" + parameter + ";target:" + target);
}
}
package com.dada.test.spring.aop.advisor;
import java.util.HashSet;
import java.util.Set;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAroundAdvice implements MethodInterceptor
{
private Set set=new HashSet();
public Object invoke(MethodInvocation method) throws Throwable
{
set.add(method);
System.out.println("============around before==========");
Object result=method.proceed();
System.out.println("============around after==========");
return result;
}
}
package com.dada.test.spring.aop.advisor;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice
{
public void before(Method method, Object[] parameters, Object target) throws Throwable
{
System.err.println("==========before===========");
System.out.println(method.getName());
System.out.println(parameters.length);
System.out.println(target);
}
}
package com.dada.test.spring.aop.advisor;
import org.springframework.aop.ThrowsAdvice;
import com.dada.test.spring.aop.MyException;
public class MyThrowsAdvice implements ThrowsAdvice
{
public void afterThrowing(MyException e)
{ // ,
System.err.println("MyThrowsAdvice:"+e.toString());
}
public void afterThrowing(RuntimeException e)
{
// ,
System.err.print("RuntimeException!");
}
}
package com.dada.test.spring.aop.service;
public interface IService {
String doSth();
void doA();
}
package com.dada.test.spring.aop.service;
import com.dada.test.spring.aop.MyException;
public class MyServiceImpl implements IService{
public String doSth()
{
System.out.println(this);
System.out.println("MyServiceImpl doSth");
return "cindy";
}
public void doA()
{
System.out.println("MyServiceImpl doA");
throw new MyException("111111111");
}
}
beforeAdvice
aroundAdvice
afterAdvice
afterAdvice
throwsAdvice
package com.dada.test.spring.aop;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dada.test.spring.aop.service.IService;
public class TestSpringAop {
public static void main(String[] args) throws Exception
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]
{
"com/dada/test/spring/aop/aop-context.xml"
});
IService ser = (IService) ctx.getBean("invoker");
String rs = ser.doSth();
System.out.println("rs="+rs.toString());
try
{
ser.doA();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}
실행 결 과 는 다음 과 같 습 니 다.
2013 - 7 - 18 23: 21: 06 org. springframework. context. support. AbstractApplications Context prepare새로 고침 정보: 새로 고침 org. springframework. context. support.ClassPathXmlApplicationContext@14ed9ff: display name [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]; startup date [Thu Jul 18 23:21:06 CST 2013]; root of context hierarchy 2013 - 7 - 18 23: 21: 06 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions 정보: class path resource [com / data / test / spring / op / aop - context. xml]2013 - 7 - 18 23: 21: 07 org. springframework. context. support. AbstractApplicationContext 획득 FreshBeanFactory 정보: Bean factory for application context [org. springframework. context. support.ClassPathXmlApplicationContext@14ed9ff]: org.springframework.beans.factory.support.DefaultListableBeanFactory@12152e62013 - 7 - 18 23: 21: 07 org. springframework. beans. factory. support. DefaultListableBeanFactory preInstantiateSingletons 정보: org. springframework. beans. factory. support 에서 싱글 톤 을 미리 설치 합 니 다.DefaultListableBeanFactory@12152e6: defining beans [beforeAdvice,aroundAdvice,afterAdvice,throwsAdvice,servTarget,invoker]; root of factory hierarchy 2013 - 7 - 18 23: 21: 07 org. springframework. aop. framework. ProxyFactory Bean getObject 경고: singleton 대상 이 아 닌 프 록 시 를 사용 하 는 것 은 바람 직 하지 않 은 경우 가 많 습 니 다. 'targetName' 속성 을 설정 하여 프로 토 타 입 프 록 시 를 사용 합 니 다. = = = = = = = = before = = = = = = = = = = = = = doSth 0. com. dada. test. spring. aop. service.MyServiceImpl@6f7ce9 ============around before==========com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9 MyServiceImpl doSth ==============after return============== result:cindy;method:public abstract java.lang.String com.dada.test.spring.aop.service.IService.doSth();parameter:[Ljava.lang.Object;@1dfafd1;target:com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9 ==============after return============== result:cindy;method:public abstract java.lang.String com.dada.test.spring.aop.service.IService.doSth();parameter:[Ljava.lang.Object;@8fce95;target:com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9 ============around after========== rs=cindy ==========before=========== doA 0com.dada.test.spring.aop.service.MyServiceImpl@6f7ce9 ============around before========== MyServiceImpl doA MyThrowsAdvice:com.dada.test.spring.aop.MyException: 111111111 com.dada.test.spring.aop.MyException: 111111111 at com.dada.test.spring.aop.service.MyServiceImpl.doA(MyServiceImpl.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:126) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at com.dada.test.spring.aop.advisor.MyAroundAdvice.invoke(MyAroundAdvice.java:15) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy0.doA(Unknown Source) at com.dada.test.spring.aop.TestSpringAop.main(TestSpringAop.java:21)
after return 이 두 번 나 타 났 습 니 다. 어떤 이유 인지 생각해 보 세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.