Java Tapestry-Spring-无法初始化上下文

Java Tapestry-Spring-无法初始化上下文,java,spring,junit,tapestry,Java,Spring,Junit,Tapestry,我在Tapesty/Spring方面相对较新 我正在试着为一个我收到的申请设置测试套装。 应用程序工作正常,Tapesty/Spring/Hibernate都很好 所以我只是创建一个简单的测试 package reservations.services.restaurant; import java.util.Calendar; import java.util.Date; import org.apache.tapestry5.ioc.annotations.Inject; import

我在Tapesty/Spring方面相对较新

我正在试着为一个我收到的申请设置测试套装。 应用程序工作正常,Tapesty/Spring/Hibernate都很好

所以我只是创建一个简单的测试

package reservations.services.restaurant;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mytableguru.backend.reservation.service.ReservationService;
import com.mytableguru.backend.restaurant.enums.DayOfTheWeekEnum;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/test-applicationContext.xml" })
public class RestaurantTableAvailabilityServiceImplTest extends AbstractJUnit4SpringContextTests {

    @Inject
    private ReservationService reservationService;

    @Test
    public void testContext() {
        Assert.assertNotNull(reservationService);
    }
}
使用以下test-applicationContext.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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.mytableguru" />
    <context:property-placeholder location="classpath:database.properties" />
</beans>
但它不会过去

final ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:/test-applicationContext.xml");
我似乎还是会出错

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantFileUploadServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mytableguru.backend.restaurant.service.RestaurantService com.mytableguru.backend.restaurant.service.RestaurantFileUploadServiceImpl.restaurantService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.apache.tapestry5.services.AssetSource com.mytableguru.backend.restaurant.service.RestaurantServiceImpl.assetSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tapestry5.services.AssetSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.apache.tapestry5.ioc.annotations.Inject(), @org.springframework.beans.factory.annotation.Autowired(required=true)}

我想你需要创建一个SpringModuleDef来模拟

与SpringJUnit4ClassRunner不同,使用TapestryIOCJUnit4ClassRunner可能是最简单的(请参见示例)

例如:


免责声明:我从未使用tapestry的spring集成。我猜你的代码中有一个地方对ApplicationContextCustomizer有贡献

文件指出:

如果已将Spring配置为允许基于注释的注入, 然后,您将能够将Tapestry服务注入到Spring中 豆。此功能仅在Spring ApplicationContext中可用 未在外部配置和加载

这里还有一些东西可以尝试:

@RunWith(TapestryIOCJUnit4ClassRunner.class)
@Registry(modules={TapestryModule.class, SpringModule.class, TestModule.class})
public class MyTest {
    public static class TestModule {
        @Contribute(ApplicationContextCustomizer.class)
        public static void addCustomizer(OrderedConfiguration<ApplicationContextCustomizer> config) {
            ApplicationContextCustomizer customizer = new ApplicationContextCustomizer() {
                public void customizeApplicationContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
                    applicationContext.setConfigLocation("classpath:/test-applicationContext.xml");
                }
            };
            config.add("mycustomizer", customizer);
        }
    }


    @ModuleDef
    public static SpringModuleDef createSpringModuleDef() {
        ServletContext sc = new MockServletContext();
        return new SpringModuleDef(sc);
    } 

   // @Inject @Test etc
@RunWith(tapestryocjunit4classrunner.class)
@注册表(模块={TapestryModule.class、SpringModule.class、TestModule.class})
公共类MyTest{
公共静态类测试模块{
@贡献(ApplicationContextCustomizer.class)
公共静态void addCustomizer(OrderedConfiguration配置){
ApplicationContextCustomizer customizer=新的ApplicationContextCustomizer(){
public void自定义应用程序上下文(ServletContext ServletContext,ConfigurableWebApplicationContext applicationContext){
applicationContext.setConfigLocation(“类路径:/test applicationContext.xml”);
}
};
config.add(“mycustomizer”,customizer);
}
}
@模德夫
公共静态SpringModuleDef createSpringModuleDef(){
ServletContext sc=new MockServletContext();
返回新弹簧模块DF(sc);
} 
//注入测试等
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantFileUploadServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mytableguru.backend.restaurant.service.RestaurantService com.mytableguru.backend.restaurant.service.RestaurantFileUploadServiceImpl.restaurantService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaurantServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.apache.tapestry5.services.AssetSource com.mytableguru.backend.restaurant.service.RestaurantServiceImpl.assetSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tapestry5.services.AssetSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.apache.tapestry5.ioc.annotations.Inject(), @org.springframework.beans.factory.annotation.Autowired(required=true)}
@RunWith(TapestryIOCJUnit4ClassRunner.class)
@Registry(modules={TapestryModule.class, HibernateCoreModule.class, SomeOtherModule.class})
public class MyTest {
    @ModuleDef
    public static SpringModuleDef createSpringModuleDef() {
         final ApplicationContext appContext = new XMLApplicationContext("classpath:/test-applicationContext.xml");
         ServletContext sc = new MockServletContext();
         return new SpringModuleDef(sc) {
             protected ApplicationContext locateApplicationContext(ServletContext servletContext) {
                 return appContext;
             }
         };
    }

    @Inject
    private Foo someSpringBean;

    @Inject
    private Bar someTapestryBean;

    ...
@RunWith(TapestryIOCJUnit4ClassRunner.class)
@Registry(modules={TapestryModule.class, SpringModule.class, TestModule.class})
public class MyTest {
    public static class TestModule {
        @Contribute(ApplicationContextCustomizer.class)
        public static void addCustomizer(OrderedConfiguration<ApplicationContextCustomizer> config) {
            ApplicationContextCustomizer customizer = new ApplicationContextCustomizer() {
                public void customizeApplicationContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
                    applicationContext.setConfigLocation("classpath:/test-applicationContext.xml");
                }
            };
            config.add("mycustomizer", customizer);
        }
    }


    @ModuleDef
    public static SpringModuleDef createSpringModuleDef() {
        ServletContext sc = new MockServletContext();
        return new SpringModuleDef(sc);
    } 

   // @Inject @Test etc