Hi All,
I am learning spring. I started learning spring AOP. I created one class which will print Hello world. Following is code of the class.
package com.test.web;
public class Hello {
private
String greeting;
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
public void sayHello() {
System.out.println(greeting);
}
}
Then I created following class to apply aspect
package com.test.web;
import org.aspectj.lang.ProceedingJoinPoint;
public class Aspect1 {
public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Before Around Method");
Object obj = pjp.proceed();
System.out.println("After arround Method");
return obj;
}
public void afterThrowingMethod() {
System.out.println("After throwing method");
}
public void beforeMethod() {
System.out.println("Before Method");
}
public void afterMethod() {
System.out.println("After method");
}
}
I made configuration in the following 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"
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.xsd"> <bean id="Hello" class="com.test.web.Hello">
<property name="greeting" value="Hello World"></property>
</bean>
<!-- aspects -->
<bean id="aspectId" class="com.test.web.Aspect1" />
<aop:config>
<aop:aspect ref="aspectId">
<aop:pointcut id="pointCutId" expression="execution(* sayHello(..))" />
<aop:before method="beforeMethod" pointcut="execution(* sayHello(..))" />
<aop:after-returning method="afterMethod" pointcut-ref="pointCutId" />
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointCutId" />
</aop:aspect>
</aop:config>
<!-- end aspects -->
</beans>
When I run following class, I expect to see
Before method
Hello World
After method
But I am getting only Hello world.
The aspects are not getting applied. I could not figure out what is reason
package com.test.web;
import javax.sql.DataSource;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.*;
public class Test1 {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("C:\\SpringApp1\\bin\\Hello.xml"));
Hello h = (com.test.web.Hello)factory.getBean("Hello");
h.sayHello();
}
}
The spring jar file version is 2.06
Please help
[ December 07, 2008: Message edited by: Sagar Kale ]
[ December 07, 2008: Message edited by: Sagar Kale ]
[ December 07, 2008: Message edited by: Sagar Kale ]