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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


two × 2 =



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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close