About Soroosh Sarabadani

My name is Soroosh Sarabadani from Iran. I'm working more than 8 year in development and design Enterprise softwares, with master of software engineering grade.

Spring Configurable Magic

Spring framework has several modules that provide a range of services, many of these modules Can work just for managed Objects (Spring Beans) Some examples about these services are Dependency Injection, Transaction Management, AOP Services , etc. Everything is fine when we are using Objects as Services, so they are managed by by Spring with a specific scope. But sometimes we need our domain objects have these services. Usually domain objects are created with new keyword so they cannot be managed by default with spring.

In my Previous post (How to use Events in Spring 3.x) we had a domain object with name Order. for decoupling between object we used events. But just Managed beans can raise event in Spring framework (Probably every Framework you know and has this feature).

Spring introduce an annotation with name Configurable. Using this annotation on our domain objects make them Managed by spring.
But how does it work: Configurable for its purpose needs AspectJ Compiler, your class needs Byte Code Enhancement in Compile Time or Load Time until can satisfy your requirements.

I want to bring you a simple sample about how to configure and use Configurable power in your applications. It is a good practice to have an Environment Object that all the system can access its properties to catch information about system. For example we need to know the current time of system, simple solution is to use Calendar.getInstance().getTime() or  new Date()

but there are some defects, your code will not be testable for parts that you need to test date assertion ( I will write a series of post bout Test and Testable codes as soon as possible) .

The other problem is when you want your system work with Pseudo clock. as an example your client wants to work with system in holidays as the last non holiday date.

So it is valuable to have a mechanism for these requirements. As a simple solution in this sample i will create An Environment Interface that have one method ( getCurrentTime()). Everywhere in code if i need the time of system i will use this method. Environment interface must be injected in my objects until i can happily use this method. Spring beans do not have any problem to using Environment, but in our domain objects we have to use Configurable Annotation.

If you use Maven you will need to added these dependencies to your pom:

<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.1.RELEASE</version>
 <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.8</version>
</dependency>

To compile your application with maven you can use following Aspectj-maven-plugin configuration :

<build>
 <plugins>
 <plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>aspectj-maven-plugin</artifactId>
 <version>1.4</version>
 <configuration>
 <showWeaveInfo>true</showWeaveInfo>
 <source>1.6</source>
 <target>1.6</target>
 <Xlint>ignore</Xlint>
 <complianceLevel>1.6</complianceLevel>
 <encoding>UTF-8</encoding>
 <verbose>false</verbose>
 <aspectLibraries>
 <aspectLibrary>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aspects</artifactId>
 </aspectLibrary>
 </aspectLibraries>
 </configuration>
 <executions>
 <execution>
 <goals>
 <goal>compile</goal>
 <goal>test-compile</goal>
 </goals>
 </execution>
 </executions>
 <dependencies>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjrt</artifactId>
 <version>1.6.8</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjtools</artifactId>
 <version>1.6.11</version>
 </dependency>
 </dependencies>
 </plugin>
 </plugins>
 </build>

If you use IDE for compiling your code don’t forget to change its compiler to AspectJ. you can find it in your local maven respository in aspectjrt directory.

Assume there is a Product class and we would like to know the date of its creation, and the date if its sale.

@Configurable(preConstruction = true)
 public class Product {
 private final String name;
 private final String description;
 private final Date createDate;
 private Status status;
 private Date saleDate;
 @Autowired
 private Environment environment;
 
public Product(String name, String description) {
 this.name = name;
 this.description = description;
 this.status = Status.PENDING;
 this.createDate = environment.getCurrentDate();
 }
 
public void sell() {
 this.status = Status.SALE;
 this.saleDate = environment.getCurrentDate();
 }
 
public Date getCreateDate() {
 return createDate;
 }
 
public Date getSaleDate() {
 return saleDate;
 }
 
public static enum Status {
 PENDING, SALE;
 }
 }

Product is a very simple class, we used preConstruction=true because our product construction need to use environment.

Environment and its implementations are very simple too:

public interface Environment {
 Date getCurrentDate();
 }
 
public class DefaultEnvironment implements Environment {
 @Override
 public Date getCurrentDate() {
 return new Date();
 }
 }
 
public class MockEnvironment implements Environment {
 private Date date;
 @Override
 public Date getCurrentDate() {
 return this.date;
 }
 
public void setCurrentDate(Date date){
 this.date = date;
 }
 
}

MockEnvironment is created in test packages, because we need this class just in our tests. Instead of using this class you can use some mock library as Mocktio and an extension on it (Springockito). But in this sample our focus is not on them.

Our tests are very simple too:

@ContextConfiguration({"classpath*:context.xml","classpath*:test-context.xml"})
 public class ProductTest extends AbstractJUnit4SpringContextTests {
 final Date time = Calendar.getInstance().getTime();
 @Autowired
 Environment environment;
 
@Before
 public void before() {
 
((MockEnvironment) this.environment).setCurrentDate(time);
 }
 
@Test
 public void created_product_should_have_current_environment_date() {
 final Product product = new Product("", "");
 Assert.assertEquals(time, product.getCreateDate());
 
}
 
@Test
 public void sell_should_set_createDate_to_now(){
 final Product product = new Product("", "");
 product.sell();
 Assert.assertEquals(time, product.getSaleDate());
 
}
 }

You can download source code from : https://github.com/psycho-ir/Spring-Configurable.git
 

Reference: Spring Configurable Magic from our JCG partner Soroosh Sarabadani at the Just another Java blog blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ 2 = four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books