从Spring上下文访问Spring Test Class对象

时间:2013-05-22 14:23:00

标签: java spring reflection annotations

奇怪的主题继续问题本身:)有没有办法从spring应用程序上下文到达spring测试类?问题详细信息隐藏在" SimpleDaoHandler" class as comment block:)

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-core.xml")
public class DbFuntionTests {

    @TestAnnotation
    private UserSessionDao userSessionDao; 

    @Test
    public void testOracleTimestamp() throws Exception {
        userSessionDao.findAll();
    }
}

初始化spring context后的处理程序类:

@Component
public class SimpleDaoHandler implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;
    private static Logger logger = Logger.getLogger(SimpleDaoHandler.class);

    @Override
    public void afterPropertiesSet() throws Exception {
        //I have the application context now, via method "setApplicationContext"
        // and need to get test class object to get the
        // annotated field "TestAnnotation" because I also need to set this
        // field with my business logic, using reflection.
        // Problem is; test class object is not included in the
        // "applicationContext" as a bean and I don't know how to access my 
        //test class which is already starting the spring context initialization. 
        //I need to get that test class object.
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
    }

有任何线索吗?

2 个答案:

答案 0 :(得分:2)

使用@TestAnnotation创建一些post processor可能会更好吗?您可以查看Spring AutowiredAnnotationBeanPostProcessor,它扫描具有@Autowire注释的字段/方法的bean并对其进行处理。我认为这是你试图实现的东西,而bean post处理器是解决此类问题的最简洁的解决方案(由Spring本身使用)。

请查看下面的示例 - MyAnnotationPostProcessor扫描应用程序上下文中每个bean的MyAnnotation并记录适用的字段。 (当然不是记录你可以采取一些行动;))。请注意,您的注释必须声明RetentionPolicy.RUNTIME;否则,它在运行时将不可见。

public final class MyAnnotationPostProcessor implements BeanPostProcessor {

    private static final Log LOG = LogFactory.getLog(MyAnnotationPostProcessor.class);

    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        final Set<Field> annotatedFields = getAnnotatedFields(bean.getClass());
        for (Field annotatedField : annotatedFields) {
            LOG.info("Post process before initialization " + beanName + "," + annotatedField);
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        final Set<Field> annotatedFields = getAnnotatedFields(bean.getClass());
        for (Field annotatedField : annotatedFields) {
            LOG.info("Post process after initialization " + beanName + "," + annotatedField);
        }
        return bean;
    }

    private Set<Field> getAnnotatedFields(final Class<?> clazz) {
        final Set<Field> annotatedFields = new HashSet<Field>();
        for (Field field : clazz.getDeclaredFields()) {
            if(hasMyAnnotation(field)) {
                annotatedFields.add(field);
            }
        }
        return annotatedFields;
    }

    private boolean hasMyAnnotation(final AccessibleObject ao) {
        return AnnotationUtils.getAnnotation(ao, MyAnnotation.class) != null;
    }
}

测试类和注释:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class MySpringTest {

    @MyAnnotation
    private UserSessionDAO dao;

    @Configuration
    static class TestContext {
        @Bean
        public static MyAnnotationPostProcessor myAnnotationPostProcessor() {
            return new MyAnnotationPostProcessor();
        }
    }

    @Test
    public void test() {

    }
}

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

答案 1 :(得分:0)

感谢Piotrek De,我终于找到了答案。您的注释处理程序类还必须实现BeanPostProcessor类。这样,您就可以获得测试应用程序上下文使用的两种方法:

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    try {
        injectGDao(bean);
    } catch (Exception e) {
        throw new BeanInstantiationException(bean.getClass(), e.getMessage(), e.getCause());
    }
    return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    try {
        injectGDao(bean);
    } catch (Exception e) {
        throw new BeanInstantiationException(bean.getClass(), e.getMessage(), e.getCause());
    }
    return bean;
}

每当测试上下文侦听器到达此方法时,您现在可以访问测试类对象。如前所述,Piotrek De的答案可以找到更详细的答案。