Skip to content

Spring3

paulcleary edited this page Nov 10, 2015 · 2 revisions

The Spring 3 module provides the following features:

  • A way to inject the request tracer into any Spring bean
  • Support for the @Traced annotation as a way to incorporate tracing into your Spring beans

Adding the dependency

Add a dependency as follows for maven:

    <dependency>
        <groupId>com.comcast.money</groupId>
        <artifactId>money-spring3</artifactId>
        <version>${money.version}</version>
    </dependency>

And here is a dependency for SBT:

    libraryDependencies += "com.comcast.money" %% "money-spring3" % "${money.version}"

Magical Fruits

You need to configure a few beans in order for Money to magically start happening.

  1. You need to configure the DefaultAdvisorAutoProxyCreator. This bean applies advice to any Spring bean method using the @Traced annotation
  2. You need to add money to auto wiring Component scan.

Here is a sample Annotation config:

@Configuration
@ComponentScan(basePackages = {"com.comcast.money.samples.springmvc", "com.comcast.money.spring3"})
public class AppConfig {

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        final DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }
}

Using the @Traced annotation

The @Traced annotation is the most useful feature in order to instrument your spring application.

To use it, simply add the annotation to any public method on a Spring managed bean:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.comcast.money.annotations.Traced;

import static com.comcast.money.japi.JMoney.*;

@Component
public class SampleTraceBean {

    @Traced("SampleTrace")
    public void doSomethingGood() {
       record("something", "good")
    }
}

In the example, we auto-wire our SampleTraceBean class by using the @Component annotation. We start and stop a trace span around the doSomethingGood method by using the @Traced annotation.

Here, we can use the methods on the [Money Java API](https://github.comcast.com/aae/money/wiki/Money In Java) to record notes.

Using the Spring Tracer

Sometimes, you may want to test using mocks, or just generally prefer using dependency injection for everything; we got you covered here.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.comcast.money.annotations.Traced;

@Component
public class SampleTraceBean {

    @Autowired
    private SpringTracer springTracer;

    @Traced("SampleTrace")
    public void doSomethingGood() {

        springTracer.record("foo", "bar");
    }
}

The example above is the exact same thing as the previous example, except we are using the SpringTracer instead of the Money API directly.