• Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      15
    • Finance
      1
    • Leadership
      5
    • Life Experiences
      8
    • Software
      30
    • System Administration
      2
  • Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
Home » Software

AspectWerkz with Spring dependency injected aspects

Janeve George Posted On February 27, 2012
0
2.1K Views


0
Shares
  • Share On Facebook
  • Tweet It

SoftwareProgramming

Aspect Oriented Programming and Dependency Injection are two very powerful frameworks that would help in eliminating cross-cutting concerns and providing a way of injecting dependencies at run-time. Two most powerful frameworks for Java, through which we can achieve these, are Aspectwerkz and Spring.

Read my previous article – “AspectWerkz – Declaring war on spaghetti code” for an overview of Aspectwerkz.

Now what to do if we want to use the power of Aspects and DI? Spring framework comes with the AOP support. But you may want to use Aspectwerkz instead of Spring AOP for various reasons like prior experience with Aspectwerkz, trying to use Spring with a project that is already using Aspectwerkz, etc…

No matter whatever the reason, this article tries to solve your problem of integrating Aspectwerkz with Spring. Let us first start with some basics of Aspectwerkz’s architecture.

Aspectwerkz’s Container Architecture

Aspectwerkz has a container style architecture. All the aspects are initialized, managed and destroyed by the Aspectwerkz’s container. The following diagram depicts how a normal Aspectwerkz’s container looks like:

Aspectwerkz Container Architecture

Aspectwerkz Container Architecture

Aspectwerkz’s Extensible Aspect Container

An interesting feature of the Aspectwerkz container architecture is the “extensible aspect container”. This feature helps us integrate other frameworks like Spring, AspectJ, etc with Aspectwerkz. The following diagram depicts the extensible aspect container.

Aspectwerkz Extensible Aspect Container

Aspectwerkz Extensible Aspect Container

Let’s Code!!!

After seeing the Aspectwerkz framework, it is time to write some code. First we need a problem statement.

Problem Statement

We need authentication as an aspect that would be weaved when a method is tagged with an annotation @Authenticated. The method arguments will contain the username and password required for authentication. Now which database to connect to the user credentials will be injected using Spring.

The Solution

The Aspect definition file: aop.xml

Let’s start with the Aspect Definition File, aop.xml.

<aspectwerkz>
    <system id="Example">
        <package name="foo.aspects">
            <aspect class="MethodCallAuthenticator"
                    container="foo.aspects.MyContainer"
                    name="bean_MethodCallAuthenticator">
 
                <pointcut name="authenticatedMethods"
                          expression="execution(@foo.aspects.Authenticated * foo.methods.*.*(..))"/>
 
                <advice name="authenticate"
                        type="before"
                        bind-to="authenticatedMethods"/>
            </aspect>
        </package>
    </system>
</aspectwerkz>

You can see that the aspect definition “MethodCallAuthenticator” has been configured with the custom built container “foo.aspects.MyContainer”. We will not be going into the details of the Aspects and how they are weaved in this article. You may download the complete source code.

Aspectwerkz container extension: foo.aspects.MyContainer

Let us look at the implementation of the container class ‘foo.aspects.MyContainer’. The class should extend “org.codehaus.aspectwerkz.aspect.AbstractAspectContainer”.

package foo.aspects;
 
import org.codehaus.aspectwerkz.AspectContext;
import org.codehaus.aspectwerkz.aspect.AbstractAspectContainer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
 
public class MyContainer extends AbstractAspectContainer {
 
    private XmlBeanFactory bean_factory = null;
 
    public MyContainer(final AspectContext aspectContext) {
        super(aspectContext);
    }
 
    protected Object createAspect() {
        if (bean_factory == null) {
            bean_factory = new XmlBeanFactory(new ClassPathResource("aspect-context.xml"));
        }
        System.out.println(bean_factory.getBean(m_aspectContext.getAspectDefinition().getName()));
        return bean_factory.getBean(m_aspectContext.getAspectDefinition().getName());
    }
 
}

There are three important things to note here:

  1. The instance variable bean_factory
  2. Implementing the abstract method “protected Object createAspect()”
  3. The aspect-context.xml loaded from the classpath

The instance variable bean_factory is used to load the Spring bean configuration. The createAspect() is invoked by the aspectwerkz framework to get new aspect instance. In the above code, we are letting the Spring initialize the dependency injected aspect. The bean configuration is defined in the aspect-context.xml file that should be in the application’s classpath.

Spring bean definition file: aspect-context.xml

The aspect-context.xml should be in the application’s classpath.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean name="bean_MethodCallAuthenticator"
          class="foo.aspects.MethodCallAuthenticator"
          id="bean_MethodCallAuthenticator">
 
        <property name="userCredentials">
            <bean class="foo.security.DummyUserCredentials"/>
        </property>
 
    </bean>
 
</beans>

We are done with the basics of integrating Aspectwerkz and Spring. You may download the complete source code.

Post Views: 2,101
0
Shares
  • Share On Facebook
  • Tweet It




Author

Janeve George

A Technology Leader, Software Engineer, and Agile Methodologies enthusiast. Currently, working as Lead Software Development with Zeta Suite. He has more than 1.6 decades of experience spanning different verticals predominated by hosting, cloud, and media delivery technologies.

Which Java collection to use?
Read Next

Which Java collection to use?

  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      15
    • Finance
      1
    • Leadership
      5
    • Life Experiences
      8
    • Software
      30
    • System Administration
      2

  • Popular Posts

  • Recent Posts

    • Java Asynchronous Programming using CompletableFuture - Part 1
    • The Java Stream API
    • Functional Interfaces in Java
    • Online Webinar: Students & New Technology - 28th July 2020
  • Keep In Touch

    Follow us on social media to get latest articles on Programming, System Architecture, Agile Development Methodologies, Product and Project Management, Personal Development, BigData, Robotics, Upcoming Events and more...


Copyright © 2020 | Janeve.Me. All rights Reserved.
Press enter/return to begin your search