Take the 2-minute tour ×

I am moving over some existing xml configuration to Java configuration for Spring. During this process, I have encountered some transferred Java code that throws a checked Exception.

public PoolDataSource myDataSource()
    final PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
    dataSource.setConnectionPoolName("myDataSourcePoolName"); // throws SQLException
    return dataSource;

I was wondering how I should handle it, and whether or not the Spring developers had any 'best practices' in mind.

Should I mark the method with a throws clause (which would propagate up any @Import chain) or should I handle it method with a try-catch block?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

As a general rule, you should simply declare any checked exceptions in the throws clause of the @Bean method.

I am not certain what you're referring to with regard to "propagating up any @Import chain"; the throws clause will of course require any dependent @Bean methods to in turn declare a throws clause containing that exception, but when it comes to actual exception propagation at container bootstrap time, the exception will be handled by the Spring container in just the same way that exceptions thrown from beans configured in Spring XML are handled.@Import should be an orthogonal concern here.

share|improve this answer
Straight from the developer himself. Thanks, you have sufficiently answered my question. That no, there is no 'right' way to do it. –  nicholas.hauschild Sep 4 '12 at 18:23
add comment

Your Answer


