无法配置RootConfig和RootWebMvcConfig

时间:2013-11-22 18:34:47

标签: java spring hibernate spring-mvc jpa

我正在尝试使用两个servlet配置Spring应用程序:

  1. 使用Spring的事务管理的常规Web servlet JPA;
  2. 用于提供REST端点的DispatcherServlet,它也是 需要相同的交易管理。
  3. 我的当前配置与答案 here 相匹配,但有一件事情除外。在RootWebMvcConfig中,我导入了RootConfig。我理解这是我在下面收到的错误消息的原因,但是我想了解如何配置它,以便我的web servlet和REST servlet都可以访问root配置。如果省略导入,则会收到异常,表示通过DispatcherServlet访问REST端点时没有正在进行的会话。

    HHH000436:已注册实体经理工厂名称(默认)。如果实体管理器将被群集或钝化,请为属性'hibernate.ejb.entitymanager_factory_name'指定唯一值

    有关如何配置这个的建议,以便我的web servlet和REST servlet都可以访问root配置吗?

    示例web.xml:

    <context-param>
            <param-name>contextClass</param-name>
            <param-value>
              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          </param-value>
        </context-param>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>my.package.RootConfig</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>mvc-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>my.package.RootWebMvcConfig</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>anotherservlet</servlet-name>
            <servlet-class>my.package.AnotherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>anotherservlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    

    示例配置类:

    @Configuration
    @ComponentScan(basePackages = {"my.package"})
    @Import(value = {PersistenceConfig.class, SecurityConfig.class})
    public class RootConfig {
        ...
    }
    
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = {"my.package.repository"})
    public class PersistenceConfig {
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
            ...
        }
        ...
    
    }
    
    @Configuration
    @ComponentScan(basePackages = {"my.package"})
    @EnableWebMvc
    @Import(value = {RootConfig.class})
    public class RootWebMvcConfig extends WebMvcConfigurerAdapter {
        ...
    }
    

    服务类:

    @Service
    @Transactional(readOnly = true)
    public class MyProjectProvider {
    
        @Autowired
        private EntityToRestPojoMapper mapper; // This is an Orika ConfigurableMapper
        @Autowired
        private MyObjectService service;
    
        public List<MyObjectPojo> getMyObjects() {
            List<MyObject> data = service.findData();       
            List<MyObjectPojo> dataPojos = new ArrayList<MyObjectPojo>();
            mapper.mapAsCollection(data, dataPojos, MyObjectPojo.class);
            return dataPojos;
        }
    }
    

0 个答案:

没有答案