小编Kha*_*kri的帖子

@Scheduled + Hibernate -> LazyInitializationException

我在 Spring Boot 2.0.5 下,使用 Spring Data JPA

我有一堂这样的课程(为了理解):

@Component
public class Synchronizer {

    @Autowired
    private MyService myService; 

    @Transactional
    public void synchronizeAuto() {
        List<MyTest> tests = myService.getTests();
        tests.get(0).getMyLazyObject().getName();
    }
}
Run Code Online (Sandbox Code Playgroud)

配置在这里(还有其他配置文件我省略了):

@Configuration
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class SpringAsyncConfiguration implements AsyncConfigurer, SchedulingConfigurer {

    @Autowired
    private AppConfigProperties appConfigProperties;

    @Autowired
    private Synchronizer synchronizer;

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(appConfigProperties.getThreadpoolCorePoolSize());
        executor.setMaxPoolSize(appConfigProperties.getThreadpoolMaxPoolSize());
        executor.setQueueCapacity(appConfigProperties.getThreadpoolQueueCapacity());
        executor.setThreadNamePrefix("threadPoolExecutor-");
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AsyncExceptionHandler();
    }

    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

spring hibernate lazy-loading scheduler lazy-initialization

5
推荐指数
1
解决办法
3162
查看次数