Hi everyone,
I'm very new to Spring and its DI and AOP stuff.
I'm trying to learn AOP as of now and it seems I can't get my project working. Can someone help me?
greeting interface:
greeting implementation:Code:public interface GreetingService { void sayGreeting(); }
main class:Code:public class GreetingServiceImpl implements GreetingService{ private String greeting; public GreetingServiceImpl() { } public GreetingServiceImpl(String greeting) { this.greeting = greeting; } public void sayGreeting() { System.out.println(this.greeting); } public void setGreeting(String greeting){ this.greeting = greeting; } }
I'm trying to weave this class as the aspect object but it seems that its methods are not called before and after the "sayGreeting()" method. I expect an output like :Code:public class HellopApp { public static void main(String[] args){ try{ BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/com/springinaction/chapter01/hello/hello.xml")); GreetingService greetingService = (GreetingService) factory.getBean("greetingService"); greetingService.sayGreeting(); }catch(Exception e){ System.out.println(e.getMessage()); } } }
"Hola que tal?"
"Muy bien, y tu?"
"Muy bien, gracias!"
But what i get is:
"Muy bien, y tu?"
What seems to be the problem with my XML configuration file?Code:public class GreetingMan { public void pre(){ System.out.println("Hola que tal?"); } public void post(){ System.out.println("Muy bien, gracias!"); } }
ThanksCode:<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="greetingService" class="com.springinaction.chapter01.hello.GreetingServiceImpl"> <property name="greeting" value="Muy bien, y tu?" /> </bean> <bean id="greetingMan" class="com.springinaction.chapter01.hello.GreetingMan"/> <aop:config> <aop:aspect ref="greetingMan"> <aop:pointcut id="greetPointCut" expression="execution(* *.sayGreeting(..)) and target(bean)"/> <aop:before method="pre" pointcut-ref="greetPointCut" arg-names="bean"/> <aop:after-returning method="post" pointcut-ref="greetPointCut" arg-names="bean"/> </aop:aspect> </aop:config> </beans>![]()



Reply With Quote