Debugging Spring Components with Namespace based configuration

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.

About these ads

3 thoughts on “Debugging Spring Components with Namespace based configuration

  1. GoSmarter Post author

    Good question! Nagendran. For loading the security-spring-config.xml we dont need to deploy in a container, we have a good JUnit spring support. We can do something like this,

    @ContextConfiguration(locations = {
    “classpath:/config/security-spring-config.xml”,
    “classpath:/config/property-loader.xml” })
    @RunWith(SpringJUnit4ClassRunner.class)

    Also we need to include the dependency in maven for servlet api as below,


    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
    <scope>provided</scope>
    </dependency>

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s