Performance Zone is brought to you in partnership with:

Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Debugging Spring Components with Namespace based configuration

01.09.2013
| 1816 views |
  • submit to reddit

Sometimes it hard to debug a Spring component especially if it is Namespace based standard spring components. In this blog I will explain how to debug a spring security component. Consider the below code:

<authentication-manager>
<authentication-provider>
<user-service id="userService">
<user name="${some-user}" password="${some-password}"
authorities="secure-access" />
</user-service>
</authentication-provider>
</authentication-manager>

In the above component if there is a problem with authorization, how do you know what value is being passed? How do you know which class is this and where to put the debug break point? If you notice “user-service” is just another bean with Spring id as “userService”. Namespace based configuration is a syntactic sugar for bean definition. Spring JUnit test and autowiring capability comes to your rescue. Define a simple test function as below:

@Autowired
@Qualifier("userService")
private Object userService;
@Test
public void testUserService(){
assertNotNull(userService);
logger.debug(userService);
}

Run the above test, it will show as green. The logger message will print InMemoryDaoImpl class. Once you know that this is the object you can modify the test as below and further debug:

@Autowired
@Qualifier("userService")
private InMemoryDaoImpl userService;
@Test
public void testUserService(){
assertNotNull(userService);
UserMap map = userService.getUserMap();
//Assuming that krishna is set as dynamic property for some-user
UserDetails user = map.getUser("krishna");
assertNotNull(user);
}

This is one way to debug it, but there might be few other ways. Please suggest me.

I hope this helped.


Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)