Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] @autowired inside the aspect

Hi, 

I have some error during the invocation of a bean inside an Aspect, and I also would like to have some suggestions about initialization of beans inside an aspect class.

1. I am trying to invoke a bean [2] inside my aspects class [1], but in the end I get the following error [4]. It seems that the @Autowired parameter didn't initialize the bean. My configuration file is in [3]. What am I doing wrong?


2. I want that the bean will be invoked one time during initialization. So, even the `map` function is called multiple times, the bean is already initialized. 
Another solution is, I want (I don't know if it is possible), that the bean will be initialized when the pointcut `map` or `reduce` is invoked by the first time.
Are these requests possible to do?

Thanks,





[1] My aspect class

@Aspect
@Configurable
@Component
public class MapReduceAspects {
    @Autowired
    MedusaDigests digests;

    @Before("execution(* map(..))")
    public void mymap(JoinPoint joinPoint) {
        System.out.println("My Map Execution: " + joinPoint.getArgs() + ":" + joinPoint.getTarget());
        Object[] obj = joinPoint.getArgs();

        if (obj.length > 0) {
            byte[] key = convertToBytes(obj[0]);
            byte[] value = convertToBytes(obj[1]);

            digests.updateDigest(key, value);
        }
    }

    @Before("execution(* reduce(..))")
    public void myreduce(JoinPoint joinPoint) { System.out.println("My Reduce Execution");
       Object[] obj = joinPoint.getArgs();

       if (obj.length > 0) {
           byte[] key = convertToBytes(obj[0]);
           byte[] value = convertToBytes(obj[1]);

           digests.updateDigest(key, value);
       }
    }
}


[1] My aspect

@Component
public class MedusaDigests {
    private final String MD5 = "MD5";
    private MessageDigest mda = null;

    public MedusaDigests() {
        try {
            mda = MessageDigest.getInstance(MD5);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

[3] My beans-aspects.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/co
ntext/spring-context-3.2.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--<aop:include name="mapreduceAspect"/>-->
    <!--<aop:include name="jobClient"/>-->
    <!--</aop:aspectj-autoproxy>-->
    <!--<context:load-time-weaver/>-->
    <context:component-scan base-package="org.apache.hadoop.mapred"/>

    <bean id="mapreduceAspect" class="org.apache.hadoop.mapred.aspects.MapReduceAspects" factory-method="aspectOf" autowire="byType" />
    <bean id="medusaDigests" class="org.apache.hadoop.mapred.MedusaDigests"/>
</beans>

[4] the error that I get
WARNING: job_local138234703_0001
java.lang.Exception: java.lang.NullPointerException
	at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
	at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.NullPointerException
	at org.apache.hadoop.mapred.aspects.MapReduceAspects.mymap(MapReduceAspects.java:50)
	at org.apache.hadoop.mapred.examples.DummyWordCount$Map.map(DummyWordCount.java:32)
	at org.apache.hadoop.mapred.examples.DummyWordCount$Map.map(DummyWordCount.java:1)
	at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
	at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
	at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
	at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Back to the top