Spring AOP for objects instantiated outside of the application context

Posted on 2008-09-30
Last Modified: 2013-11-11

I have a simple app. I've got a TaskExecutorService class which instantiated several Runnable's and gives them to the TaskExecutor.

I've managed to wire things with Spring. however what I need is to weave a logger advise before and after the run() method of my Runnable class.

The problem is the task class which implements Runnable is NOT instantiated by Spring, instead it is instantiated by the TaskExecutorService outside the Spring container.

Can somebody tell me how can I weave an advise for objects instantiated OUTSIDE the Spring context? I could not how to make use of ProxyFactory.

Thanks in advance

package springexec;

public class HelloWorldCountDownTask implements Runnable {

    private String name;

    private int count = 4;

    public HelloWorldCountDownTask(String name) { = name;



    public void run() {

        while( count > 0 ){


            if (count == 0){

                System.out.println(name + " says 'Hello World!'");

            } else {

                System.out.println(name + " : " + count);






package springexec;

import org.springframework.core.task.TaskExecutor;

public class TaskExecutorExample {

    private TaskExecutor taskExecutor;

    public TaskExecutorExample(TaskExecutor taskExecutor) {

        this.taskExecutor = taskExecutor;



    public void executeTasks(){

        this.taskExecutor.execute(new HelloWorldCountDownTask("Anna"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Beth"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Charlie"));

        this.taskExecutor.execute(new HelloWorldCountDownTask("Daniel"));



package springexec;

import org.springframework.context.ApplicationContext;


import org.springframework.core.task.TaskExecutor;

public class SyncTaskExecutorExample {

    public static void main(String args[]){

        ApplicationContext ctx = new ClassPathXmlApplicationContext("springexec/exec.xml");


        TaskExecutor taskExecutor = (TaskExecutor) ctx.getBean("syncTaskExecutor",TaskExecutor.class);

        TaskExecutorExample example = new TaskExecutorExample(taskExecutor);




<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""







    <bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

          <property name="corePoolSize" value="5"/>

          <property name="maxPoolSize" value="10"/>

          <property name="queueCapacity" value="25"/>



    <bean id="syncTaskExecutor" class="org.springframework.core.task.SyncTaskExecutor"/>


    <bean id="timerTaskExecutor" class="org.springframework.scheduling.timer.TimerTaskExecutor">

        <property name="delay" value="3000"/>

        <property name="timer" ref="timer"/>



    <bean id="timer" class="java.util.Timer"/>


    <bean id="logger" class="springexec.Logger"/>


Open in new window

Question by:apollonius_tyana
  • 3
LVL 12

Expert Comment

ID: 22766797
If you mean Spring AOP, you cannot use it to work outside the Spring framework/container.  In this case, you could consider using AspectJ.

Author Comment

ID: 22774378
By outside I mean objects that have to be instantiated within new() rather than getBean(). If you look at the example you'll see there's a Runnable class which is instantiated by the Executor. What I need is a logging aspect around the Runnable object. How would I do it in Spring configuration?

Author Comment

ID: 23049174
Do not close the question. No people in EE site able to anwer the question does not mean the question is abondened.
LVL 59

Expert Comment

by:Kevin Cross
ID: 23096641
Do you have to use spring logger?

I would do something like this if I am understanding your requirements correctly:

public void run() {
        // log start of run here

        while( count > 0 ){
            if (count == 0){
                System.out.println(name + " says 'Hello World!'");
            } else {
                System.out.println(name + " : " + count);

        // log end of run here    

Accepted Solution

apollonius_tyana earned 0 total points
ID: 23243474
Actually the issue is not the logging itself. I was wondering if I could inject AOP from Spring configuration. But it seems like it's not possible unless I somehow extend or modify Spring TaskExecutor, which means I have to deal with container issues which I wanted to avoid.  

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 38
Java Filesystem manipulation of Word Docx file (find replace text) 3 67
sites similar to codingbat to improve coding hanson skills 3 41
Java Loop 6 50
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now