File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Hello world spring aop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Hello world spring aop" Watch "Hello world spring aop" New topic
Author

Hello world spring aop

Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
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 ]
Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
I tried following aswell, but I get only Hello World

<aop:config>
<aop:aspect ref="aspectId">
<aop:after-returning method="beforeMethod" pointcut="execution(* *.*(..))" />
</aop:aspect>

</aop:config>
Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
I tried with Spring 2.5.6 version as well, it is not working, there may some small mistake which I am not able to figure out.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, there is not interface.

Spring AOP creates dynamic proxies based on the interfaces that the class implements. In your Hello class, there is no interface that is being implemented, hence Spring AOP cannot create a Proxy for you.

Also, just another reason why you should be coding to interfaces.

However, you can have Spring AOP use CGLib to generate proxy subclasses of Hello. I just would recommend coding to interfaces, and getting used to doing that while learning Spring. You will only be helping yourself here following that best practice.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
Hi Mark,

Thank you very much for the reply.


I made following changes and it worked.


1) Class Test1


instead of

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("C:\\SpringApp1\\bin\\Hello.xml"));


I used


public static ApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"Hello1.xml"});

public static void main(String[] args) {




2) Instead of

<aop :pointcut id="pointCutId" expression="execution(* sayHello(..))" />



<aop :pointcut id="pointCutId"
expression="execution(* com.test.web.Hello*.*(..))"/>





Also I used interface Hello and implementation class HelloImpl.

it worked.


Then I removed interface, used concrete class and added CGLib in classpath, worked fine, thank you.




Regards


Sagar
[ December 07, 2008: Message edited by: Sagar Kale ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hello world spring aop