如果资源注释

时间:2015-07-15 13:45:23

标签: spring ejb jndi lookup

我们想在单元测试中将EJB-s测试为Spring组件。

使用Resources注释的EJB之一,以便查找这样的JNDI值:

@Resource(lookup = "java:global/myapp/portal/url")
private String portalUrl;

问题是Spring似乎试图按类型查找值,即它在应用程序上下文中查找类型为java.lang.String的bean而不是使用JNDI。

这是我们的代码。

第一个背景:

<bean id="serverMock" class="myapp.JndiServerMock">
</bean>

第二个背景:

<context:annotation-config />
<context:component-scan base-package="myapp">
    <context:include-filter type="annotation" expression="javax.ejb.Stateless" />
    <context:include-filter type="annotation"
        expression="javax.annotation.Resources" />
</context:component-scan>
<bean
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
    <property name="alwaysUseJndiLookup" value="true" />
</bean>

<bean id="clientMock" class="myapp.JndiClientMock">

<bean id="java:global/myapp/portal/url" class="java.lang.String">
    <constructor-arg value="http://localhost:8080" type="java.lang.String"/>
</bean>

JndiServerMock:

public class JndiServerMock {

public JndiServerMock() throws IllegalStateException, NamingException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    builder.bind("java:global/myapp/portal/url", "http://localhost:8080");
    builder.activate();
}

}

JndiClientMock:

公共类JndiClientMock {

public JndiClientMock() throws NamingException {
    Hashtable<String, String> prop = new Hashtable<String, String>();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.springframework.mock.jndi.SimpleNamingContextBuilder");
    InitialContext ic = new InitialContext(prop);
    System.err.println("result = " + ic.lookup("java:global/myapp/portal/url"));
}

}

叫exampleBean:

@Stateless 公共类ExampleBean {

@Resource(lookup = "java:global/myapp/portal/url")
private String portalUrl;

public ExampleBean() {
    System.err.println("portalUrl = " + portalUrl);
}

}

公共类MainApp {

public static void main(String[] args) {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.springframework.mock.jndi.SimpleNamingContextBuilder");

    ApplicationContext applicationContext = 
        new ClassPathXmlApplicationContext("/spring/firstContext.xml", 
            "/spring/secondContext.xml");

    ExampleBean exampleBean = applicationContext.getBean(ExampleBean.class); 
}

}

如果省略“java:global / myapp / portal / url”bean定义,则抛出以下异常:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[java.lang.String]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ javax.annotation.Resource(shareable = true,lookup = java:global / myapp / portal / url,name =,description =,authenticationType = CONTAINER,type = class java.lang.Object,mappedName =)}     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)     在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:457)     在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor $ ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)     在org.springframework.beans.factory.annotation.InjectionMetadata $ InjectedElement.inject(InjectionMetadata.java:169)     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)     ......还有13个

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是你如何尝试在Spring配置中设置JNDI条目 - here's这是一个如何做到这一点的例子。 This提供了实现同样事物的其他方法的示例。但是,如果您希望对单元测试执行此操作,我认为JUnit部分here可能是一种更简洁的方法,但看起来您有很多选择!