concept dependency in category aspectj

appears as: dependencies, dependency, dependency, dependencies
AspectJ in Action, Second Edition

This is an excerpt from Manning's book AspectJ in Action, Second Edition.

Ideally, you should write a test for your aspect so you can run it automatically. Developing such tests, however, require injecting a mock or stub object in the CacheAspect instance to set its cache property. Then we can control and observe interaction with the cache property to verify correctness. We haven’t yet examined the mechanism to access an aspect instance that you need to in order to inject dependencies. You’ll learn that mechanism in chapter 6, where you’ll improve this aspect and write tests for it.

  • Locator— Introduce a locator class (commonly known as a service locator) with a method to obtain a reference to a shared object. At the application or container level, you register various objects with the locator. Doing so removes the hard-coded dependency between the Order class and a pricing strategy implementation.
    private transient PricingStrategy pricingStrategy
    = (PricingStrategy) Context.get("pricingStrategy"); Such an arrangement makes it easier to swap the implementation by registering a different object with the locator. But the domain objects now depend on the locator class—Context, in turn, adds a dependency on the environment that supplies a locator. Compared with the hard-coded dependencies approach, such an arrangement doesn’t fare much better with unit testing. Unit tests need to somehow coerce the Context object into returning the expected mock or stub object, because you may need to set the pricingStrategy to a different mock or stub object in each test.
  • Dependency injection— Add the necessary structure to domain objects to enable DI. For example, you can add a setter for each injected dependency. Then, you let a container inject dependencies upon instantiating the object. With this setup, the object has no knowledge of the mechanics of the DI. You can swap one service for another by modifying the configuration. For unit testing, you can set the dependencies directly by calling setters from the test code, without needing any special mechanism.
  • 16.1.2. Injecting dependencies with Spring and AspectJ

    Spring uses the AspectJ weaver to provide a prebuilt solution to inject dependency into any objects—even those that are instantiated outside of Spring. An aspect injects dependencies whenever an instance of a specified class is instantiated or deserialized. Figure 16.1 shows how an aspect injects dependencies into domain objects.

    Figure 16.1. Domain object DI through an aspect. Upon creating domain objects, the aspect performs DI on them. The aspect itself has dependencies injected into it, which it injects into the domain objects.

    You can avoid the performance problem and the object’s dependency on Spring using domain interface-based DI. This requires writing aspects, but if you’ve come this far in reading this book, it should be of no big concern to you!

    A way to look at the Order and LineItem classes is that they’re clients of the injected dependencies. The classes may indicate this relationship by implementing specific interfaces. A typical interface would include setter methods that set dependencies. This style allows using domain-specific interfaces instead of the Spring-specific @Configurable annotation. Listing 16.7 shows the interface that a client of PricingStrategy would implement.

    Listing 16.7. Interface to be implemented by PricingStrategy clients
    package ajia.domain; import ... public interface PricingStrategyClient { void setPricingStrategy(PricingStrategy pricingStrategy); }

    Spring includes GenericInterfaceDrivenDependencyInjectionAspect that implements the core logic to inject dependencies. You can extend this aspect to apply DI on classes that implement your domain specific interface(s). For example, listing 16.8 shows the Order class that implements the PricingStrategyClient interface.

    Listing 16.8. Order class implementing domain-specific interface
    package ajia.domain; import ... public class Order extends DomainEntity implements PricingStrategyClient { ... private transient PricingStrategy pricingStrategy; ... public void setPricingStrategy(PricingStrategy pricingStrategy) { this.pricingStrategy = pricingStrategy; } }

    Notice that you no longer need a Spring-specific annotation in this class. Listing 16.9 shows an aspect that injects dependencies into the object of the classes that implement PricingStrategyClient.

    Listing 16.9. Aspect to inject dependencies into object of PricingStrategyClient type

    This code implements what we may call the dependency distribution pattern. You inject the aspect with dependencies and the aspect will distribute those dependencies into other objects. Let’s see the parts of the aspect in more details.

    The PricingStrategyDependencyInjectionAspect subaspect extends GenericInterfaceDrivenDependencyInjectionAspect and binds its generic parameter to PricingStrategyClient. This indicates that objects of the PricingStrategyClient type and its subclasses will be configured by the subaspect. Notice how simple it is to write the subaspect.
    The configure() method provides a definition for the abstract method in the base aspect. The base aspect will call this method whenever a configurable object matching the generic parameter type is either created or deserialized. The method calls the appropriate setter to inject the dependency. In your case, there is only one dependency to set. But you may configure the object in whatever way appropriate. Note that, dependency injection is done by direct method calls instead of reflection, thus offering better performance.
    The aspect includes a setter to declare a dependency that it will distribute to the advised objects. Alternatively, you can mark the pricingStrategy field with @Autowired and avoid the setter.

    Of course, you’ll need to configure each aspect to inject the dependency into it. Listing 16.10 shows the bean definition corresponding to the aspect.

    Listing 16.10. Dependency injection into the aspect
    <beans ...> <bean id="pricingStrategy" class="ajia.domain.SimplePricingStrategy"/> <bean class="ajia.domain.PricingStrategyDependencyInjectionAspect" factory-method="aspectOf"> <property name="pricingStrategy" ref="pricingStrategy"/> </bean> </beans>

    You don’t need a prototype bean defined in the application context, because each DI aspect knows how to inject dependencies.