рдПрд╕реНрдкреЗрдХреНрдЯрдЬ, рд╕реНрдкреНрд░рд┐рдВрдЧ, рдореИрд╡реЗрди

рдореБрдЭреЗ рд╣рдорд╛рд░реЗ рд╕рдореВрд╣ рдореЗрдВ рдкрд░реНрдпрд╛рд╡рд░рдг рдХреЛ рд╕реНрдерд╛рдкрд┐рдд рдХрд░рдиреЗ рдФрд░ рдПрд╕реНрдкреЗрдХреНрдЯреЗрдЬ рдкрд╣рд▓реБрдУрдВ рдХреЗ рдЙрдкрдпреЛрдЧ рдФрд░ рд╡рд╕рдВрдд рдХреЗ рд╕рд╛рде рдЗрд╕рдХреЗ рдПрдХреАрдХрд░рдг рдХреЛ рджрд┐рдЦрд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд╣рд╛ рдЧрдпрд╛ рдерд╛ред

рдпрд╣ рдореБрдЭреЗ рд▓рдЧ рд░рд╣рд╛ рдерд╛ рдХрд┐ рдЖрд╡рд╛рд╕ рднреА рджрд┐рд▓рдЪрд╕реНрдк рд╣реЛ рд╕рдХрддрд╛ рд╣реИред



рдореИрдВ рдпрд╣рд╛рдВ рдЗрд╕ рдмрд╛рд░реЗ рдореЗрдВ рдмрд╛рдд рдирд╣реАрдВ рдХрд░реВрдВрдЧрд╛ рдХрд┐ рдПрд╕реНрдкреЗрдХреНрдЯрдЬ рдХреНрдпрд╛ рд╣реИ, рдХреМрди рдЬрд╛рдирддрд╛ рд╣реИ - рдпрд╣ рдЙрд╕рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧреА рд╣реЛрдЧрд╛, рдореИрдВ рдХреЗрд╡рд▓ рдпрд╣ рдзреНрдпрд╛рди рджреЗрддрд╛ рд╣реВрдВ рдХрд┐ рдкрд╣рд▓реБрдУрдВ рдХреЛ рд╕рдВрдХрд▓рди рдпрд╛ рд░рдирдЯрд╛рдЗрдо рд╕реНрдЯреЗрдЬ рдкрд░ рдХрдХреНрд╖рд╛рдУрдВ рдореЗрдВ рдХреБрдЫ рдХрд╛рд░реНрдпрдХреНрд╖рдорддрд╛ рдЬреЛрдбрд╝рдиреЗ рдХреА рдХреНрд╖рдорддрд╛ рд╣реИ рдЬреЛ рдкрд╣рд▓реЗ рдирд╣реАрдВ рдереАред рдпрд╛ рдХрд┐рд╕реА рдореМрдЬреВрджрд╛ рдХреЛ рд╕рдВрд╢реЛрдзрд┐рдд рдХрд░реЗрдВред



рдЕрдЧрд▓рд╛: рдкреНрд░реЛрдЬреЗрдХреНрдЯ рдХреЙрдиреНрдлрд╝рд┐рдЧрд░реЗрд╢рди рдФрд░ 3 рдЙрджрд╛рд╣рд░рдг рдкрд╣рд▓реВред





рдЪрд▓реЛ рдорд╛рд╡реЗрди рдХреЙрдиреНрдлрд╝рд┐рдЧрд░реЗрд╢рди рдХреЗ рд╕рд╛рде рд╢реБрд░реВ рдХрд░рддреЗ рд╣реИрдВ:

<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>org.habloexample</groupId> <artifactId>aspects</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.3</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <properties> <org.springframework.version>3.0.5.RELEASE</org.springframework.version> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties> </project>
      
      



<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>org.habloexample</groupId> <artifactId>aspects</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.3</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <properties> <org.springframework.version>3.0.5.RELEASE</org.springframework.version> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties> </project>



<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>org.habloexample</groupId> <artifactId>aspects</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.3</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <properties> <org.springframework.version>3.0.5.RELEASE</org.springframework.version> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties> </project>







рдЕрдЧрд▓рд╛, рдЗрдВрдЯреЗрд▓реАрдЬ рдЖрдИрдбреАрдИрдП рдореЗрдВ:

рдкреНрд▓рдЧрдЗрди AspectJ: рд╕рдХреНрд╖рдо рдХрд░реЗрдВ

рдкреНрд▓рдЧрдЗрди рдПрд╕реНрдкреЗрдХреНрдЯ рд╡реАрд╡рд░: рдЕрдХреНрд╖рдо рдХрд░реЗрдВ

рд╕реЗрдЯрд┐рдВрдЧреНрд╕ / рд╕рдВрдХрд▓рди / JavaCompiler: рдЕрдЬрд╛



рдЙрджрд╛рд╣рд░рдг 1, рдПрдиреЛрдЯреЗрдХреНрд╢рдВрд╕ рдХреЗ рд╕рд╛рде рдПрд╕реНрдкреЗрдХреНрдЯрдЬ, рдПрдиреЛрдЯреЗрд╢рди рдХреЗ рд╕рд╛рде рд╕реНрдкреНрд░рд┐рдВрдЧ:

(рдЙрджрд╛рд╣рд░рдг www.javacodegeeks.com/2010/07/aspect-oriented-programming-with-spr2.html рд╕реЗ рд▓рд┐рдпрд╛ рдЧрдпрд╛)



рдПрдХ рд╡рд░реНрдЧ рдмрдирд╛рдПрдБ:



package org.habr.springaspectj.services; import org.springframework.stereotype.Service; @Service("greetingService") public class GreetingService { public static final String HELLO_FROM_GREETING_SERVICE = "Hello from Greeting Service"; public String sayHello() { return HELLO_FROM_GREETING_SERVICE; } }
      
      



package org.habr.springaspectj.services; import org.springframework.stereotype.Service; @Service("greetingService") public class GreetingService { public static final String HELLO_FROM_GREETING_SERVICE = "Hello from Greeting Service"; public String sayHello() { return HELLO_FROM_GREETING_SERVICE; } }



package org.habr.springaspectj.services; import org.springframework.stereotype.Service; @Service("greetingService") public class GreetingService { public static final String HELLO_FROM_GREETING_SERVICE = "Hello from Greeting Service"; public String sayHello() { return HELLO_FROM_GREETING_SERVICE; } }







рдкрд╣рд▓реВ рдмрдирд╛рдПрдБ:



package org.habr.springaspectj.aspects; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class GreetingAspect { private String message; public void setMessage(String message) { this.message = message; } @Around("execution(* org.habr.springaspectj.services.GreetingService.*(..))") public Object advice(ProceedingJoinPoint pjp) throws Throwable { String serviceGreeting = (String) pjp.proceed(); return message + " and " + serviceGreeting; } }
      
      



package org.habr.springaspectj.aspects; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class GreetingAspect { private String message; public void setMessage(String message) { this.message = message; } @Around("execution(* org.habr.springaspectj.services.GreetingService.*(..))") public Object advice(ProceedingJoinPoint pjp) throws Throwable { String serviceGreeting = (String) pjp.proceed(); return message + " and " + serviceGreeting; } }



package org.habr.springaspectj.aspects; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class GreetingAspect { private String message; public void setMessage(String message) { this.message = message; } @Around("execution(* org.habr.springaspectj.services.GreetingService.*(..))") public Object advice(ProceedingJoinPoint pjp) throws Throwable { String serviceGreeting = (String) pjp.proceed(); return message + " and " + serviceGreeting; } }







рд╡рд╕рдВрдд рдХреЗ рд▓рд┐рдП рдкрд░реАрдХреНрд╖рдг-рдкрд╣рд▓реВjxml рдмрдирд╛рдПрдБ:



<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:component-scan base-package="org.habr.springaspectj" /> <bean class="org.habr.springaspectj.aspects.GreetingAspect" factory-method="aspectOf"> <property name="message" value="Hello from Greeting Aspect"/> </bean> <beans>
      
      



<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:component-scan base-package="org.habr.springaspectj" /> <bean class="org.habr.springaspectj.aspects.GreetingAspect" factory-method="aspectOf"> <property name="message" value="Hello from Greeting Aspect"/> </bean> <beans>



<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:component-scan base-package="org.habr.springaspectj" /> <bean class="org.habr.springaspectj.aspects.GreetingAspect" factory-method="aspectOf"> <property name="message" value="Hello from Greeting Aspect"/> </bean> <beans>





рд╣рдо рдПрдХ рдкрд░реАрдХреНрд╖рдг рд▓рд┐рдЦ рд░рд╣реЗ рд╣реИрдВ:



package org.habr.springaspectj; import org.habr.springaspectj.services.GreetingService; import org.habr.springaspectj.services.IncrementService; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; public class AspectAnnotationTest { ApplicationContext context ; @Before public void init() { context = new ClassPathXmlApplicationContext("test-aspectj.xml"); } @Test public void testAnnotationService() { GreetingService greetingService = (GreetingService) context.getBean("greetingService"); assertTrue(greetingService.sayHello().contains(GreetingService.HELLO_FROM_GREETING_SERVICE)); assertTrue(greetingService.sayHello().length()>GreetingService.HELLO_FROM_GREETING_SERVICE.length()); } }
      
      



package org.habr.springaspectj; import org.habr.springaspectj.services.GreetingService; import org.habr.springaspectj.services.IncrementService; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; public class AspectAnnotationTest { ApplicationContext context ; @Before public void init() { context = new ClassPathXmlApplicationContext("test-aspectj.xml"); } @Test public void testAnnotationService() { GreetingService greetingService = (GreetingService) context.getBean("greetingService"); assertTrue(greetingService.sayHello().contains(GreetingService.HELLO_FROM_GREETING_SERVICE)); assertTrue(greetingService.sayHello().length()>GreetingService.HELLO_FROM_GREETING_SERVICE.length()); } }



package org.habr.springaspectj; import org.habr.springaspectj.services.GreetingService; import org.habr.springaspectj.services.IncrementService; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; public class AspectAnnotationTest { ApplicationContext context ; @Before public void init() { context = new ClassPathXmlApplicationContext("test-aspectj.xml"); } @Test public void testAnnotationService() { GreetingService greetingService = (GreetingService) context.getBean("greetingService"); assertTrue(greetingService.sayHello().contains(GreetingService.HELLO_FROM_GREETING_SERVICE)); assertTrue(greetingService.sayHello().length()>GreetingService.HELLO_FROM_GREETING_SERVICE.length()); } }







рд╕реЗрд╡рд╛ рдФрд░ рдкрд╣рд▓реВ src / main / java рдореЗрдВ рд╕рдВрдмрдВрдзрд┐рдд рдкреИрдХреЗрдЬ рдореЗрдВ рдЬрд╛рддреЗ рд╣реИрдВ

Src / test / java рдореЗрдВ рдЯреЗрд╕реНрдЯ рдХрд░реЗрдВ



рдкрд░реАрдХреНрд╖рдг рд╕рддреНрдпрд╛рдкрд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ рдореВрд▓ рд╕рдВрджреЗрд╢ рдореЗрдВ рдХреБрдЫ рдЬреЛрдбрд╝рд╛ рдЧрдпрд╛ рд╣реИред

рдЖрдк рдЧреНрд░реАрдЯрд┐рдВрдЧреНрд╕ рд╕реЗрд╡рд░.рд╕реЗ.рд╣реЗрд▓реЛ () рдкреНрд░рд┐рдВрдЯ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ рдФрд░ рджреЗрдЦ рд╕рдХрддреЗ рд╣реИрдВ рдХрд┐ рдПрдХ рд▓рд╛рдЗрди рдЬреЛрдбрд╝реА рдЧрдИ рд╣реИ рдЬреЛ xml рдкрд╣рд▓реВ рдореЗрдВ рджреА рдЧрдИ рд╣реИред



рдЙрджрд╛рд╣рд░рдг 2. рдПрдиреЛрдЯреЗрд╢рди рдХреЗ рдмрд┐рдирд╛, рд╡рд┐рдзрд┐ рдХреЗ рд╡реНрдпрд╡рд╣рд╛рд░ рдореЗрдВ рдмрджрд▓рд╛рд╡ред





рдПрдХ рд╕реЗрд╡рд╛ рдЬреЛрдбрд╝реЗрдВ:



package org.habr.springaspectj.services; public class IncrementService { public int inc(int i){ return i+1; } }
      
      



package org.habr.springaspectj.services; public class IncrementService { public int inc(int i){ return i+1; } }



package org.habr.springaspectj.services; public class IncrementService { public int inc(int i){ return i+1; } }







рдПрдХ рдкрд╣рд▓реВ рдЬреЛрдбрд╝реЗрдВ:



package org.habr.springaspectj.aspects; public aspect DecrementAspect { pointcut incMethod(): execution(public int inc(int)); int around(int number): incMethod() && args(number) { return proceed(number) - 1; } }
      
      



package org.habr.springaspectj.aspects; public aspect DecrementAspect { pointcut incMethod(): execution(public int inc(int)); int around(int number): incMethod() && args(number) { return proceed(number) - 1; } }



package org.habr.springaspectj.aspects; public aspect DecrementAspect { pointcut incMethod(): execution(public int inc(int)); int around(int number): incMethod() && args(number) { return proceed(number) - 1; } }







рд╕реЗрд╡рд╛ рдХреЛ xml рдореЗрдВ рдЬреЛрдбрд╝реЗрдВ (рд╡рд╛рджрд╛ рдХрд┐рдпрд╛ - рдЗрд╕ рдмрд╛рд░ рдПрдиреЛрдЯреЗрд╢рди рдХреЗ рдмрд┐рдирд╛):



<bean name="inc-bean" class="org.habr.springaspectj.services.IncrementService"/>
      
      



<bean name="inc-bean" class="org.habr.springaspectj.services.IncrementService"/>



<bean name="inc-bean" class="org.habr.springaspectj.services.IncrementService"/>







рдЗрдХрд╛рдИ рдкрд░реАрдХреНрд╖рдг рдХреЗ рд▓рд┐рдП рд╡рд┐рдзрд┐ рдЬреЛрдбрд╝реЗрдВ:



@Test public void testBeanAndAJService() { IncrementService service = context.getBean(IncrementService.class); int i = 10; assertEquals(i,service.inc(i)); }
      
      



@Test public void testBeanAndAJService() { IncrementService service = context.getBean(IncrementService.class); int i = 10; assertEquals(i,service.inc(i)); }



@Test public void testBeanAndAJService() { IncrementService service = context.getBean(IncrementService.class); int i = 10; assertEquals(i,service.inc(i)); }







рд╣реБрд░реНрд░реЗ! рдПрдХ рд╡реГрджреНрдзрд┐ рдХрд░реЗрдВ, рд▓реЗрдХрд┐рди рд╕рдВрдЦреНрдпрд╛ рдирд╣реАрдВ рдмрдврд╝рддреА рд╣реИ!



рдиреЛрдЯ: IDEA рдореЗрдВ рдкрд╣рд▓реВ рд╕рдорд░реНрдерди рд╕рд╣реА рдирд╣реАрдВ рд╣реИред рдЙрдкрд░реНрдпреБрдХреНрдд рдкрд╣рд▓реВ рдХреЛ рд▓рд╛рд▓ рд░рдВрдЧ рдореЗрдВ рдЪрд┐рд╣реНрдирд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛, рд▓реЗрдХрд┐рди рдЗрд╕реЗ рдмреБрд╡рд╛рдИ рдХреЗ рд░реВрдк рдореЗрдВ рд╕рдВрдХрд▓рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛ - рдФрд░ рдорд╛рд╡реЗрди рдФрд░ рдЖрдИрдбрд┐рдпрд╛ред



рдЙрджрд╛рд╣рд░рдг 3: рдЧреИрд░-рдореМрдЬреВрджрд╛ рд╡рд┐рдзрд┐ рдЬреЛрдбрд╝рдирд╛





рдкрд┐рдЫрд▓реЗ рдЙрджрд╛рд╣рд░рдг рд╕реЗ рд╕реЗрд╡рд╛, рдкрд╣рд▓реВ рдЬреЛрдбрд╝реЗрдВ:



package org.habr.springaspectj.aspects; public aspect AddingAspect { public String org.habr.springaspectj.services.IncrementService.myCall(String name) { return "Cognac " + name; } }
      
      



package org.habr.springaspectj.aspects; public aspect AddingAspect { public String org.habr.springaspectj.services.IncrementService.myCall(String name) { return "Cognac " + name; } }



package org.habr.springaspectj.aspects; public aspect AddingAspect { public String org.habr.springaspectj.services.IncrementService.myCall(String name) { return "Cognac " + name; } }







рдкрд░реАрдХреНрд╖рдг рдХреЗ рд▓рд┐рдП рд╡рд┐рдзрд┐ рдЬреЛрдбрд╝реЗрдВ:



@Test public void testAddAMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { IncrementService service = context.getBean(IncrementService.class); assertEquals(service.myCall("Hennesy"),"Cognac Hennesy"); }
      
      



@Test public void testAddAMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { IncrementService service = context.getBean(IncrementService.class); assertEquals(service.myCall("Hennesy"),"Cognac Hennesy"); }



@Test public void testAddAMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { IncrementService service = context.getBean(IncrementService.class); assertEquals(service.myCall("Hennesy"),"Cognac Hennesy"); }







рд╕реЗрд╡рд╛ рд╡рд░реНрдЧ рдореЗрдВ myCall рдкрджреНрдзрддрд┐ рдХреА рдЕрдиреБрдкрд╕реНрдерд┐рддрд┐ рдХреЗ рдмрд╛рд╡рдЬреВрдж, IDEA рд╢рд┐рдХрд╛рдпрдд рдирд╣реАрдВ рдХрд░рддрд╛ рд╣реИ рдФрд░ рд╕рдм рдХреБрдЫ рд╕рдВрдХрд▓рд┐рдд рдХрд░рддрд╛ рд╣реИ



рдирд┐рд╖реНрдХрд░реНрд╖:



рдЗрд╕реЗ рд╣рд▓реНрдХреЗ рдврдВрдЧ рд╕реЗ рдбрд╛рд▓рдиреЗ рдХреЗ рдкрд╣рд▓реВ рд╕рдордЭ, рдХреЛрдб рдХреА рдкрдардиреАрдпрддрд╛ рдореЗрдВ рд╡реГрджреНрдзрд┐ рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВ, рдореИрдВ рд╡рд┐рд╢реЗрд╖ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдмрд┐рдирд╛ рдЙрдирдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреА рдЕрдиреБрд╢рдВрд╕рд╛ рдирд╣реАрдВ рдХрд░реВрдВрдЧрд╛ред

рд▓реЗрдХрд┐рди рдЕрдЧрд░ рдЖрдкрдХреЛ рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ рдЬрд╝рд░реВрд░рдд рд╣реИ, рддреЛ рдпреЗ рдЙрджрд╛рд╣рд░рдг рдорджрдж рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред



рдЯрд┐рдкреНрдкрдгрд┐рдпреЛрдВ рдиреЗ рдирд┐рд╖реНрдХрд░реНрд╖ рдмрджрд▓рдиреЗ рдХреА рд╕рд▓рд╛рд╣ рджреА ...

рдкрд╣рд▓реБрдУрдВ рдХреЛ рдХрдИ рдХреНрд╖реЗрддреНрд░реЛрдВ рдореЗрдВ рд▓рд╛рдЧреВ рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдП: рдЬрд░реНрдирд▓рд┐рдВрдЧ, рдСрдбрд┐рдЯрд┐рдВрдЧ рдФрд░ рд▓реЗрдирджреЗрдиред

рдФрд░ рдмрд╣реБрдд рд╕рд╛рд╡рдзрд╛рдиреА рд╕реЗ рдЖрдкрдХреЛ рдЙрдиреНрд╣реЗрдВ рдЕрдиреНрдп рдХреНрд╖реЗрддреНрд░реЛрдВ рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИред рдкрд╣рд▓реБрдУрдВ рдХреЗ рд╕рд╛рде, рдпрд╣ рд╕реБрдирд┐рд╢реНрдЪрд┐рдд рдХрд░рдирд╛ рдЖрд╕рд╛рди рд╣реИ рдХрд┐ рдЖрдк рдХреЛрдб рдореЗрдВ рдПрдХ рдЪреАрдЬрд╝ рдкрдврд╝рддреЗ рд╣реИрдВ, рдФрд░ рдХреБрдЫ рдФрд░ рдХрд╛рдо рдХрд░рддрд╛ рд╣реИред






All Articles