Showing posts with label AspectJ. Show all posts
Showing posts with label AspectJ. Show all posts

Inject Non-Spring Bean

Additional Jar files:

org.springframework.aspects-3.1.0.M1.jar
c:\aspectj-1.6.11\lib\aspectjweaver.jar

Also need make sure JVM AspectJ-enabled.

put -javaagent:C:/aspectj-1.6.11/lib/aspectjweaver.jar
into VM Argument section

The above are just setting environments.

Why/when we want to inject stuff into a non-spring bean?
There are many scenarios that a bean is not instantiated by Spring, but we still want to inject value/references into this bean.

In config:

xmlns:aop="http://www.springframework.org/schema/aop"
...
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
...

<aop:spring-configured/>

<bean id='noSpringBean' class="packageA.NoSpringBean" abstract="true">
<property name="beanName" value="Bolun"/>
<property name="height" value="187"/>
</bean>

NoSpringBean.java

@Configurable("noSpringBean")
public class NoSpringBean
{
...
}

When you test it, you will see a lot of Waving stuff before our codes are really executed, performance wise, this could be really bad.

Adding org.springframework.transaction-3.1.0.M1.jar into path, removed a lot of complains.

Quickly setup a AspectJ project

1. download AJDT for eclipse

Eclipse 3.6 Update Site URL:http://download.eclipse.org/tools/ajdt/36/update
Eclipse 3.5 Update Site URL:http://download.eclipse.org/tools/ajdt/35/update

2. Create a AspectJ project, and you are all set.

You might find some syntax error in aspect class, ignore it.

AspectJ, Spring, and Guice

Recently, I have some time to take a look at these 3 things, mainly on AOP part.

Spring has a very good support for AOP, but the main problem is, all the beans need to be managed by Spring's container, otherwise, AOP won't work. This is ideal if you have a completely Spring-powered project. But it won't do you any good if you are not.

[Not exactly: Spring could inject values into non-Spring bean]

1. define bean in config as abstract plus the configurations, the id, let's see, is "testBean1"
2. add @configurable("testBean1") before the class in .java file.
3. add in config file.

It seems behind the scene, it's still AspectJ is doing all the work.

Guice has the same problem, the Java classes have to be binded by Guice's injector to have AOP work.

That leaves only one choice, AspectJ.
With AspectJ, we could have a very powerful tool to work on big project.

Imagine your boss gives you this task: log the result whenever a method named getMyMoneyBack() gets called. Sound easy? Not so fast, the problem is getMyMoneyBack() exists in thousands of classes, and they are called by millions of times. You really don't want to go to millions of places to put your log code there, do you?

With AspectJ, life is much easier.

You define a point cut like:

pointcut logGetMyMoneyBack() : call (* *.getMyMoneyBack(..))

String around() : logGetMyMoneyBack
{
String result = proceed();
System.out.println(result );
return result;
}
return proceed();