Sunday, August 26, 2012

Dependency Injection on Stateless Beans

I recently came across a situation where I needed to inject a dependency into a class that, when used, was expected to be stateless. I'm using Spring as the dependency injection container, and this class is instantiated directly from the class, and not through Spring context; thus, I could not use Spring context with basic dependency injection. Did my Google search, but did not find an answer, so I'm posting this because the solution was fairly simple and needed to be shared.

The scenario (assuming you're familiarized with JPA, Spring, and overall dependency injection) 
I have a utility class that is responsible for indexing entities using Lucene. I wanted to add this utility to a JPA listener so that it can create or update the index on the entity in a @PrePersist method. Why this way, and not through a service? Good question. The entity could be persisted directly through a service, directly using Entity Manager, or as a cascade effect when persisting an entity that contains this entity as a property. So, in order to ensure that the index is created/updated, I wanted to provide a listener that would do that prior to saving the entity.

The service class that creates/updates indexes needs to be instantiated by Spring context so that it can have all the indexing dependencies, such as the location of the indexes, so if I wanted a Listener to do this task, I needed to inject this instantiated bean by Spring context into the listener. But, JPA listeners are supposed to be stateless, Entity Manager does not call Spring context to get a reference of an instantiated listener, rather it uses the class specified in the @EntityListeners annotation and instantiates it directly. So, what to do?

Solution
If the search service, the service that does the indexing as well, can be declared as static in the Listener, surely it will be accessible when JPA executes a persist of the entity by any method.


@Component
public class EntityIndexListener
{
    static private SearchService searchService;
    ...




Declaring static is part of the solution, we still need to inject the instantiated class by Spring context to it. For this we create a set method, the same as we would as for any other dependency.


@Autowired(required = true)
@Qualifier("searchService")
public void setSearchService(SearchService searchService)
{
    this.searchService = searchService;
}

This will setup the dependency, assuming autowire is configured. If autowire is not configured, you can define the dependency explicitly. 

One last thing; to ensure that Spring does the injection at startup, declare an init method solely for the purpose of "telling" Spring that this method needs to be instantiated at startup, so that this method can be executed. The init method does not need to do anything.

@PostConstruct
public void init()
{
    logger.info("Initializing Search Service for Listener ["+ searchService +"]");
}

Hope this helps.

Thursday, March 1, 2012

Master the Crucial Non-Technical Skills Every Software Architect Needs

Thousands of software professionals have the necessary technical qualifications to become architects, but far fewer have the crucial non-technical skills needed to get hired and succeed in this role. In today's agile environments, these “soft” skills have grown even more crucial to success as an architect. For many developers, however, these skills don’t come naturally–and they’re rarely addressed in formal training. Now, long-time software architect Dave Hendricksen helps you fill this gap, supercharge your organizational impact, and quickly move to the next level in your career.

In his book, 12 Essential Skills for Software Architects, Hendricksen begins by pinpointing the specific relationship, personal, and business skills that successful architects rely upon. Next, he presents proven methods for systematically developing and sharpening every one of these skills, from negotiation and leadership to pragmatism and vision.

From start to finish, this book’s practical insights can help you get the architect position you want–and thrive once you have it.

The soft skills you need... and a coherent framework and practical methodology for mastering them!

Relationship Skills
  • Gracious Behavior: Your ability to be gracious with others in all circumstances.
  • Communication: Your ability to effectively interact with others.
  • Negotiation: Your ability to get things done.
  • 
Leadership: Your ability to influence what and how things get done.

  • Politics: Your ability to interact in a political marketplace. 


Personal Skills
  • Transparency: Your ability to operate and interact with other people, and to have all of the cards on the table, face up.
  • Passion: Your ability to fully engage in what you are doing.
  • Context Switching: Your ability to quickly and effectively refocus your attention at a moment’s notice to a new context. 

Business Skills
  • Business Knowledge: Your ability to truly understand the language of business and the drivers that come into play when making tough decisions to enable the business to grow.
  • Innovation: Your ability to bring new and innovative ideas forward that aren’t just technically “cool,” but rather facilitate revenue growth and sustained viability.
  • Pragmatism: Your ability to make decisions that are the best ones for the company as a whole, rather than simply promoting the latest and greatest technology as a requirement for the next project.
  • Vision: Your ability to see where the business is going, to help formulate where the business could go, and to help the business safely navigate the treacherous waters of technology projects.

CHANGE is coming ...ugh!

Well today I was discussing with a colleague how some people are resilient to the big C.

And I am speaking about CHANGE.

I feel very identified with those people because, I am very afraid to it. I am more a going with the routine person. I do not like change. But you know what?, I've tried by any means to avoid it and it still happens.

In the workplace, specially in the technology field (I am sure this is something you know by now), you can't afford not to go with the change or stay long enough with a technology that is going to be obsolete in the next three seconds. This last part is an exaggeration, of course.

But what do you do to cope with the times? Here is what I have tried...

Well some people try to keep informed on the latest. Not to be tortured by the speed that everything is happening, but to watch and know what is going on.

Based on that information, you see what is being demanded at the workplace and try to differentiate a long term used skill from a fad.

Then you should measure your preparedness to make the leap. What are your strong skills matching what could be a new arena for you and what are the challenges.

About those challenges, review what you need to do to overcome them, say that you need to learn a new programming language, where you could take classes, how much they cost, can you learn it without a formal class, etc.

And last but not least: Make a plan and take it from the blue print to reality.

I am not saying is going to be a walk in the park, but it is worth it.

Trying this, not only gave me something more to create new opportunities in my resume but did miracles for my self esteem.

I hope this gives you something to think about...