我有一个Spring Boot应用程序(v1.4.3),我的对象使用默认的Jackson Object Mapper序列化,如下所示:
@RequestMapping(value = "diagnosis", method = RequestMethod.GET)
public Object diagnosis(Filter filter, Pageable pageable) {
if (filter) {
return eventRepository.findAll(filter.toSpecification(), pageable)
}
return eventRepository.findAll(pageable)
}
如果我只定义一个数据源(application.yml' spring.datasource ...',从spring开始自动配置),一切正常。
现在我需要配置两个数据源。对于每个数据源,我编写了一个配置类。
UserDatasource
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "xxx.repository.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
class DatasourceConfigurationUser {
@Autowired
private DatasourceSettingsUser datasourceSettingsUser
@Primary
@Bean(name = "userDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource()
dataSource.setDriverClassName(datasourceSettingsUser.driverClassName)
dataSource.setUrl(datasourceSettingsUser.url)
dataSource.setUsername(datasourceSettingsUser.username)
dataSource.setPassword(datasourceSettingsUser.password)
return dataSource
}
/*
* Entity Manager Factory setup.
*/
@Primary
@Bean(name = "userEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean()
factoryBean.setDataSource(dataSource())
factoryBean.setPackagesToScan(["xxx.model.user"] as String[])
factoryBean.setJpaProperties(datasourceSettingsUser.toHibernateProperties())
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter())
return factoryBean
}
@Primary
@Bean(name = "userTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager()
txManager.setEntityManagerFactory(emf)
return txManager
}
}
DataDatasource
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "xxx.repository.data", entityManagerFactoryRef = "dataEntityManager", transactionManagerRef = "dataTransactionManager")
class DatasourceConfigurationData {
@Autowired
private DatasourceSettingsData datasourceSettingsData
@Bean(name = "dataDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource()
dataSource.setDriverClassName(datasourceSettingsData.driverClassName)
dataSource.setUrl(datasourceSettingsData.url)
dataSource.setUsername(datasourceSettingsData.username)
dataSource.setPassword(datasourceSettingsData.password)
return dataSource
}
/*
* Entity Manager Factory setup.
*/
@Bean(name = "dataEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean()
factoryBean.setDataSource(dataSource())
factoryBean.setPackagesToScan(["xxx.model.data"] as String[])
factoryBean.setJpaProperties(datasourceSettingsData.toHibernateProperties())
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter())
return factoryBean
}
@Bean(name = "dataTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager()
txManager.setEntityManagerFactory(emf)
return txManager
}
}
现在我得到令人讨厌的LazyLoadingExceptions,因为在Jackson Mapper序列化期间无法获取Lazy Collections。我想知道为什么这是使用默认的Spring Boot Datasource !!
在数据源配置期间有什么我错过的吗?
编辑1
这适用于服务层。
@Service
@Transactional
class EventService {
@Autowired
private EventRepository eventRepository
def findAll(Pageable pageable) {
Page<Event> page = eventRepository.findAll(pageable)
page.content.each {
it.config.event.size()
it.config.system.size()
it.config.subsystem.size()
}
return page
}
}
但它对每个查询都非常具体: - (