spring boot를 사용하여 새로운 프로젝트를 구성하고 있다.
@OneToMany(mappedBy = "configs" , fetch=FetchType.LAZY)
private List<Category> categories;
LAZY Fetch 전략을 사용해서 categories 들을 읽어오는 로직을 사용했는데
아래와 같이 에러가 발생했다.
failed to lazily initialize a collection of role .... could not initialize proxy - no Session
application.yml에 아래와 같은 설정을 추가했지만 동일한 에러 발생
spring.jpa.open-in-view: true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
JAVA Config 파일에 WebMvcConfigurationSupport을 상속받도록 하고
Spring에서 제공하는 OpenEntityManagerInViewInterceptor 를 추가하여 해결
@Configuration
public class AdminWebAppConfig extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor()).addPathPatterns("/**");
}
@Bean
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
return new OpenEntityManagerInViewInterceptor();
}
}
추가내용
spring.jpa.open-in-view: true 설정을 하게 되면 JpaWebConfiguration이 활성화 된다.
( OSIV Interceptor를 registry에 등록하는 소스가 포함되어 있다.)
org.springframework.boot.autoconfigure.orm.jpa.JpaWebConfiguration
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(WebMvcConfigurer.class)
@ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class, OpenEntityManagerInViewFilter.class })
@ConditionalOnMissingFilterBean(OpenEntityManagerInViewFilter.class)
@ConditionalOnProperty(prefix = "spring.jpa", name = "open-in-view", havingValue = "true", matchIfMissing = true)
protected static class JpaWebConfiguration {
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when
// not on the classpath
@Configuration
protected static class JpaWebMvcConfiguration implements WebMvcConfigurer {
private static final Log logger = LogFactory.getLog(JpaWebMvcConfiguration.class);
private final JpaProperties jpaProperties;
protected JpaWebMvcConfiguration(JpaProperties jpaProperties) {
this.jpaProperties = jpaProperties;
}
@Bean
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
if (this.jpaProperties.getOpenInView() == null) {
logger.warn("spring.jpa.open-in-view is enabled by default. "
+ "Therefore, database queries may be performed during view "
+ "rendering. Explicitly configure " + "spring.jpa.open-in-view to disable this warning");
}
return new OpenEntityManagerInViewInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
}
}
}
하지만 내가 생성한 Configuration파일에서는 WebMvcConfigurationSupport를 상속받아서 설정했기 때문에
JpaWebConfiguration 을 추가하는 WebMvcAutoConfiguration이 동작하지 않는다.
WebMvcAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' adminWebAppConfig (OnBeanCondition)
Matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
Configuration을 아래와 같이 변경하면 application.yml에 설정한대로 동작한다.
@Configuration
public class AdminWebAppConfig implements WebMvcConfigurer {
}
또는
@EnableWebMvc
@Configuration
public class AdminWebAppConfig {
}
'java' 카테고리의 다른 글
Springboot change timezone (0) | 2020.04.21 |
---|---|
spring boot cannot find main class (0) | 2020.03.18 |
springboot 핸들바 auto configuration (0) | 2019.10.08 |
openjdk 설치 (0) | 2018.12.10 |
log4j.xml 파일 위치 지정하기 (0) | 2018.04.18 |