@Resource注释可以在没有spring-framework的情况下正常使用吗?

时间:2019-05-25 06:52:13

标签: java tomcat

我最近知道@Resource注释和@Autowired注释之间的区别,但是我不知道在发明spring-framework之前将在哪个场景中使用@Resource注释。

我已经在google和stackoverflow上搜索了任何提示,但没有得到任何合理的解释。然后,我尝试创建一个Java Web Project(使用J2EE1.4,jdk1.7,tomcat 7),并使用@Resource批注创建一个Servlet来模拟Spring的自动装配行为,但是遇到一个错误。

在我使用的代码下面:

public class TestServlet extends HttpServlet {


    public void init() throws ServletException {
        Student student=new Student("1");
    }
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("111111111111");
    }

}
public class TestServlet2 extends HttpServlet {
    @Resource
    Student student;
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println(student);
    }

}

首先,我访问TestServlet以创建Student java bean,然后访问TestServlet2将Student对象注入到Student属性。但我遇到如下错误:

javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:115)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:848)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:836)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:538)
    at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:440)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:114)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1133)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:853)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2549)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2538)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

非常感谢!@Powerlord,您的建议对您有很大帮助,可以完美解决我的问题。这是我的解决方案: 我在WebRoot / META-INF中创建文件Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="student" auth="Container"
     type="com.test.Student" factory="org.apache.naming.factory.BeanFactory" id="5">
</Resource>
</Context>
public class TestServlet2 extends HttpServlet {
    @Resource(name="student")
    Student student;
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        System.out.println(student.getId());
    }

}