JBOSS AS 7中的@Resource注入不起作用 [英] @Resource injection in JBOSS AS 7 doesn't work

查看:73
本文介绍了JBOSS AS 7中的@Resource注入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(已更新)

(UPDATED)

我已经在jboss-as-7.1.1.Final上以独立方式部署了一个Web应用程序,并且我有一个启动类可以按如下所示进行初始化,并且运行良好:

I have deployed a web application on jboss-as-7.1.1.Final as standalone, and I have one startup class to initialize as below which works fine:

@Startup  
@Singleton  
public class StartupBean {  

    @PostConstruct  
    void init() {  
    EmailSenderService emailSenderService = new EmailSenderService();
    emailSenderService.testMail();
    }
}

问题在另一个类中定义如下:

the problem is in the other class defined as following:

@Stateless
public class EmailSenderService {

    @Resource(mappedName="java:jboss/mail/Default")
    private Session mailSession;

    @PostConstruct
    public void testMail(){
        if(mailSession == null){
            System.out.println("NULL"); 
        }
    }
}

启动应用程序服务器时,启动类将调用该方法,但是mailSession属性不会由容器初始化,并且为null.

when starting the application server, the method is called by the startup class, however the mailSession property is not initialized by the container and is null.

我的JBoss standalone.xml中具有以下配置:

I have following configuration in my JBoss standalone.xml:

<subsystem xmlns="urn:jboss:domain:mail:1.0">
    <mail-session jndi-name="java:jboss/mail/Default">
        <smtp-server outbound-socket-binding-ref="mail-smtp">
        </smtp-server>
    </mail-session>
</subsystem>

知道为什么容器未初始化mailSession吗?

Any Idea why mailSession is not initialized by the container?

尽管这是一个Web应用程序,但我需要在HTTP请求之前进行一些初始化,这就是为什么我使用@StartUp类和其他东西的原因.

Although this is a web application, I need to do some initialization prior to HTTP requests, that's why I'm using an @StartUp class and other stuff.

致谢

推荐答案

@Resource批注必须在bean类中使用.在您的情况下,在 EmailSenderService 中定义的注释EJB容器只会忽略它.

The @Resource annotation must be used within a bean class. In your case the annotation defined in EmailSenderService it is just ignored by the EJB Container.

根据ejb 3.1规范:

16.4.1.1使用注释注入简单的环境条目.

16.4.1.1Injection of Simple Environment Entries Using Annotations.

Bean Provider使用Resource批注将 bean类的字段或方法注释为目标用于注入简单的环境条目.

The Bean Provider uses the Resource annotation to annotate a field or method of the bean class as a target for the injection of a simple environment entry.

还请注意,该字段不能为静态.

Also notice that the field must not be static.

16.2.2环境条目的注释.

16.2.2Annotations for Environment Entries.

该字段或方法可以具有任何访问限定符(公共,私有等),但不能为静态.

The field or method may have any access qualifier (public, private, etc.) but must not be static.

这篇关于JBOSS AS 7中的@Resource注入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆