Home » Java » Enterprise Java » Spring Configurable Magic

About Soroosh Sarabadani

Soroosh Sarabadani
A worm who loves to have JAVA and share his findings with others.

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:


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


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;
 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 {

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 {
 public Date getCurrentDate() {
 return new Date();
public class MockEnvironment implements Environment {
 private Date date;
 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:

 public class ProductTest extends AbstractJUnit4SpringContextTests {
 final Date time = Calendar.getInstance().getTime();
 Environment environment;
 public void before() {
((MockEnvironment) this.environment).setCurrentDate(time);
 public void created_product_should_have_current_environment_date() {
 final Product product = new Product("", "");
 Assert.assertEquals(time, product.getCreateDate());
 public void sell_should_set_createDate_to_now(){
 final Product product = new Product("", "");
 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.
(+1 rating, 1 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
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 our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Binh Nguyen Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
Binh Nguyen
Binh Nguyen

Thanks, nice post