• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Hello world spring aop

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sagar Kale
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Get me the mayor's office! I need to tell him about this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic