How to handle checked exceptions in Spring JavaConfig?

I am moving on to some existing xml configuration to Java configuration for Spring. During this process, I came across some transferred Java code that produces verification code Exception.

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

I was wondering how I could handle this, and whether the Spring developers had any “best practices”.

Should I mark the method with a sentence throws(which will propagate any chain @Import), or should I process it with a block try- catch?

+5
source share
1 answer

In general, you should simply declare any checked exceptions in the throwsmethod clause @Bean.

, " @Import"; throws, , @Bean, , , throws, , , Spring , beans, Spring XML. @Import .

+6

All Articles