wood burning stoves*
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

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() {


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"

<bean id="Hello" class="com.test.web.Hello">
<property name="greeting" value="Hello World"></property>

<!-- aspects -->

<bean id="aspectId" class="com.test.web.Aspect1" />

<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" />

<!-- end aspects -->


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");


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:aspect ref="aspectId">
<aop:after-returning method="beforeMethod" pointcut="execution(* *.*(..))" />

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

Joined: Feb 05, 2001
Posts: 17249

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


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.


[ December 07, 2008: Message edited by: Sagar Kale ]
It is sorta covered in the JavaRanch Style Guide.
subject: Hello world spring aop
Similar Threads
Simple AOP not working
Spring AOP HELP needed
AOP not getting invoked on my Target Bean Method --> Singer.perform() method
Spring MVC AOP Logger
Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed