0

我最初将 @Component 用于在 Servlet 中调用的 bean(Student.java)。我将@Component 更改为@Named。我曾经使用以下代码在 Servlet 中获取学生 bean。

Student student = (Student)applicationContext.getBean("student")

在我更改为 @Named 后,不再检测到该 bean。我收到以下错误消息。谁能告诉我有什么问题?我将 javax.inject-1.jar 文件放在 webapps\student-enrollment\WEB-INF\lib\ 下

Context init...
Logger name for context: student-enrollment
Context logger config file: logback-student-enrollment.xml
Adding logger context: student-enrollment to map for context: student-enrollment
2014-09-10 20:46:19.361 Servlet /student-enrollment threw load() exception
javax.naming.NameNotFoundException: Name student is not bound in this Context
        at org.apache.naming.NamingContext.lookup(NamingContext.java:770) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.naming.NamingContext.lookup(NamingContext.java:153) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(DefaultAnnotationProcessor.java:276) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(DefaultAnnotationProcessor.java:187) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1153) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026) ~[catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) [catalina-6.0.36.jar:6.0.36]
        at org.apache.catalina.startup.Embedded.start(Embedded.java:825) [catalina-6.0.36.jar:6.0.36]
        at org.red5.server.tomcat.TomcatLoader.init(TomcatLoader.java:517) [tomcatplugin-1.3.jar:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_43]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_43]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_43]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_43]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java
:1581) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522
) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]

编辑 1

这是我的 Student.java 和 StudentEnrollmentServlet 类的一部分。

@Named(Student.NAMED)
public class Student {


    public static final String NAMED = "student";

    .......

}

@Named(StudentEnrollmentServlet.NAMED)
public class StudentEnrollmentServlet extends HttpServlet {

    public static final String NAMED = "StudentEnrollmentServlet";


    @Resource(name = Student.NAMED)
    private Student student;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {

        processRequest(request, response);
    }

    .......
}
4

1 回答 1

0

看起来你需要给 bean 一个名字

@Named("student")
于 2014-09-10T16:50:14.583 回答