GAE + spring linkageError尝试对name进行重复的类定义

时间:2014-08-14 01:14:08

标签: spring google-app-engine spring-data cglib

我正在使用GAE上的Spring 4.0.5开发Web应用程序。 我使用的是spring data jpa 1.3.5

当我在@Service的方法上添加@Transactional标记时,在加载时我收到以下错误:

  

org.springframework.beans.factory.BeanCreationException:创建名称为' protectedSiteService'的bean时出错在文件[/development/workspace_experimental/example_app_engine/war/WEB-INF/classes/com/example/web/controller/ProtectedSiteService.class]中定义:bean的初始化失败;嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class com.example.web.controller.ProtectedSiteService]的CGLIB子类:此问题的常见原因包括使用final类或不可见类;嵌套异常是org.springframework.cglib.core.CodeGenerationException:java.lang.reflect.InvocationTargetException - > null

和警告

  

2014-08-14 02:59:30.090:WARN ::嵌套在org.springframework.beans.factory.BeanCreationException:创建名称为' protectedSiteService'的bean时出错在文件[development / workspace_experimental / example_app_engine / war / WEB-INF / classes / com / example / web / controller / ProtectedSiteService.class]中定义:bean的初始化失败;嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class com.example.web.controller.ProtectedSiteService]的CGLIB子类:此问题的常见原因包括使用final类或不可见类;嵌套异常是org.springframework.cglib.core.CodeGenerationException:java.lang.reflect.InvocationTargetException - > null:   java.lang.LinkageError:loader(com / google / appengine / tools / development / IsolatedAppClassLoader的实例):尝试重复的名称类定义:" com / example / web / controller / ProtectedSiteService $$ EnhancerBySpringCGLIB $$ e2ae408e&# 34;

这是班级

@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();
     }

}

我认为这可能是一个加载配置问题。所以我的web.xml是

<?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>

我的spring.xml是

<?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>

在spring-mvc中我定义了一些bean。

在我的配置包中,我有3个文件:JpaApplicationConfig,SecurityConfig和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));
    }
}

这是我的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();
    }*/

}

我真的不知道错误在哪里,我想我已经加载了两次配置。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。答案是&#34;使用接口&#34;。

我为我的服务创建了一个接口:

public interface ProtectedSiteService {

    public List<OrganizationMember> method(String name);
}

然后我实现了界面:

@Service
public class ProtectedSiteServiceImp implements ProtectedSiteService {

    public ProtectedSiteServiceImp() {

    }

    @Autowired
    private UserRepository userRepo;

     @Transactional
     public List<OrganizationMember> method(String name){

         User user = userRepo.findByEmail(name);
         return user.getOrganizationMemberships();
     }

}

最后,我以这种方式使用服务,使用Autowired注释调用接口:

@Controller
public class ProtectedSiteController {

    ...

    @Autowired
    private ProtectedSiteService service;

    public ProtectedSiteController() {

    }

    @RequestMapping(method = RequestMethod.GET, value="/afterLogin")
    public String afterLogin(HttpServletRequest request, HttpServletResponse response){

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        List<OrganizationMember> memberList = service.method(auth.getName());

        ...

        return redirect;
    }
}