I'm developing a web application with Spring 4.0.5 on GAE. I'm using spring data jpa 1.3.5
When I add @Transactional tag on a method of a @Service, at loading time I receive the following error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'protectedSiteService' defined in file [/development/workspace_experimental/example_app_engine/war/WEB-INF/classes/com/example/web/controller/ProtectedSiteService.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.example.web.controller.ProtectedSiteService]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
and the warning
2014-08-14 02:59:30.090:WARN::Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'protectedSiteService' defined in file [development/workspace_experimental/example_app_engine/war/WEB-INF/classes/com/example/web/controller/ProtectedSiteService.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.example.web.controller.ProtectedSiteService]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null: java.lang.LinkageError: loader (instance of com/google/appengine/tools/development/IsolatedAppClassLoader): attempted duplicate class definition for name: "com/example/web/controller/ProtectedSiteService$$EnhancerBySpringCGLIB$$e2ae408e"
This is the class
@Service
public class ProtectedSiteService {
public ProtectedSiteService() {
}
@Autowired
private UserRepository userRepo;
@Transactional
public List<OrganizationMember> method(String name){
User user = userRepo.findByEmail(name);
return user.getOrganizationMemberships();
}
}
I think that may be a loading configuration problem. So my web.xml is
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
"-//Oracle Corporation//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.example.config.WebMvcConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring basic configurations -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring.xml</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enconding helper filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>dispatcher</servlet-name>
</filter-mapping>
<welcome-file-list>
<welcome-file>public/landingPage.jsp</welcome-file>
</welcome-file-list>
</web-app>
My spring.xml is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Basic Configurations -->
<context:annotation-config/>
<context:component-scan base-package="com.qrtify.config"/>
<!-- SpringMVC -->
<import resource="spring-mvc.xml"/>
</beans>
In spring-mvc I defined some beans.
In my config package I have 3 files: JpaApplicationConfig, SecurityConfig and WebMvcConfig
@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"com.example.web.controller", "com.example.models", "com.example.security"})
public class WebMvcConfig extends WebMvcConfigurerAdapter{
private static final Logger logger = Logger
.getLogger(WebMvcConfig.class.getName());
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
logger.info("ADDING PAGER...");
PageableArgumentResolver resolver = new PageableArgumentResolver();
Pageable pageable = new PageRequest(1, 10);
resolver.setFallbackPageable(pageable);
argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}
}
This is my Jpa Config
@Configuration
@EnableJpaRepositories(basePackages={"com.example.repository"})
@EnableTransactionManagement
public class JpaApplicationConfig {
private static final Logger logger = Logger
.getLogger(JpaApplicationConfig.class.getName());
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
Map<String, String> map = new HashMap<String, String>();
map.put("datanucleus.NontransactionalRead","true");
map.put("datanucleus.NontransactionalWrite","false");
map.put("datanucleus.storeManagerType","rdbms");
map.put("datanucleus.autoCreateSchema" ,"false");
map.put("datanucleus.validateTables" ,"false");
map.put("datanucleus.validateConstraints" ,"false");
map.put("datanucleus.jpa.addClassTransformer" ,"true");
map.put("datanucleus.singletonEMFForName", "true");
LocalContainerEntityManagerFactoryBean lce= new LocalContainerEntityManagerFactoryBean();
lce.setPersistenceProviderClass(org.datanucleus.api.jpa.PersistenceProviderImpl.class);
DriverManagerDataSource dmds = new DriverManagerDataSource();
dmds.setDriverClassName("com.mysql.jdbc.Driver");
dmds.setUrl("jdbc:mysql://localhost:3306/example");
dmds.setUsername("example");
dmds.setPassword("example");
lce.setDataSource(dmds);
lce.setPackagesToScan("com.example.models");
lce.setJpaPropertyMap(map);
lce.setLoadTimeWeaver(new org.springframework.instrument.classloading.SimpleLoadTimeWeaver());
return lce;
}
@Bean
public PlatformTransactionManager transactionManager(){
logger.info("Loading Transaction Manager...");
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
/*@Bean
public PersistenceAnnotationBeanPostProcessor postProcessor(){
return new PersistenceAnnotationBeanPostProcessor();
}*/
}
Really I don't know where is the mistake, I suppose that I'm loading some configuration twice.