当前位置: 首页 > 知识库问答 >
问题:

Spring不是在注射豆子,我做错了什么?

龚俊捷
2023-03-14

我只是想尝试一下Spring,但我似乎错过了什么。它似乎可以装载Spring和豆子,但是当它用autowired注入这些豆子时,它就不起作用了。有人有线索吗?

    <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
    </welcome-file-list>

    <!-- Spring Dependency Injection -->

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

  <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
    </listener-class>
<!-- Login Filter -->
<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.nortal.pirs.presentation.web.LoginFilter</filter-class>
    <init-param>
        <param-name>secretParameter</param-name>
        <param-value>8392</param-value>
    </init-param>
</filter>

<filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/MainServlet</url-pattern>
        <servlet-name>MainServlet</servlet-name>
</filter-mapping>

<!-- Main Servlet -->
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.nortal.pirs.presentation.web.MainServlet</servlet-class>
<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/main/*</url-pattern>
</servlet-mapping>
    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config />

    <!-- Turn on AspectJ @Configurable support -->
    <context:spring-configured />

    <context:component-scan base-package="com.nortal.pirs.test.independent" />
    <context:component-scan base-package="com.nortal.pirs.businesslogic.logic" />
    <context:component-scan base-package="com.nortal.pirs.presentation.vaadin" />
    <context:component-scan base-package="com.nortal.pirs.presentation.vaadin.views" />
    <context:component-scan base-package="com.nortal.pirs.presentation.web" />

    <!-- Turn on @Autowired, @PostConstruct etc support -->
    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    <bean
        class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

</beans>
@Component("UserManager")
public class UserManagerLogic implements UserManagerInterface {
 @Service
public class MainServlet extends HttpServlet {

    @Autowired
    @Qualifier("UserManager")
    private UserManagerInterface userManager;
    Logger log;

    public MainServlet() {
        log = Logger.getLogger(getClass());
    }

    public boolean userLoggedIn(String username, String password) {     
        return SecurityManager.getInstance().credentialsValid(username, password);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

        HttpSession session = ((HttpServletRequest) request).getSession();

        String username = (String) session.getAttribute("username");
        boolean authenticated = (boolean) session.getAttribute("authenticated");

        User user = userManager.getUserByEmail(username);

        WelcomeGenerator welcomeGenerator = new WelcomeGenerator();

        if (authenticated) {
            generateResponse(response, welcomeGenerator.WelcomeMessage(user), "The secret code is " + session.getAttribute("secretParameter"));
        } else {
            generateResponse(response, welcomeGenerator.wrongCredentialsMessage(username), "Secret code is hidden, because authentication failed");
        }
    }

    public void generateResponse(HttpServletResponse response, String welcomeMessage, String additionalData) throws IOException {       
        HtmlGenerator generator = new HtmlGenerator("PIRS");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.write(generator.printHeader());
        out.write(generator.printCenter(welcomeMessage));
        out.write(generator.printCenter(additionalData));
        out.write(generator.printFooter());
    }

    public UserManagerInterface getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManagerInterface userManager) {
        this.userManager = userManager;
    }               
}
java.lang.NullPointerException
    at com.nortal.pirs.presentation.web.MainServlet.processRequest(MainServlet.java:58)
    at com.nortal.pirs.presentation.web.MainServlet.doPost(MainServlet.java:47)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
2013-03-08 03:48:42,834 [localhost-startStop-1] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6a4ac9fb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.config.internalBeanConfigurerAspect,mainController,SecurityManager,**UserManager**,VisitManager,**mainServlet**,org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy

共有1个答案

陶宏浚
2023-03-14

您的servlet不是@configurable。因为它的生命周期不是由Spring控制的,所以这是让它变得自动化的唯一方法。

哦,您的servlet肯定不是@service

 类似资料:
  • 那么现在我们叫bean什么?Bean是Foo类型的对象,引用“Foo”,但是Boo类是否被注释为服务,Foo被注释为存储库,也是Bean吗?我使用spring已经有一段时间了,但是这个基本的问题让我很难过,因为我不知道...

  • 我正在做一个项目,我的意图是运行一个玉米作业,并发送邮件给我的朋友,祝他们生日,我能够从MySQL DB获取电子邮件,并将其与当前日期进行比较,但当涉及到发送电子邮件时,我得到NullPointerException。 我确信应用程序属性没有问题,我在其他项目中也使用了它们,它们的功能正常 这是我得到以下信息的错误

  • } 它正在抛出这些错误。

  • 11:15:57创建表如果不存在tblcompany(companyid INT(11)无符号NOT NULL,custfirst VARCHAR(50),custlast VARCHAR(50),company VARCHAR(50),custphone VARCHAR(50), custemail VARCHAR(50),主键(companyid),索引(companyid),约束外键(com

  • 我试图发布一个新的条目到MongoDB,我知道这个问题是我在服务器文件中调用我的路由的地方,但我不知道如何解决这个问题。我需要能够测试在邮递员的API和张贴到数据库。 我得到以下错误: /users/ryan/desktop/leasequery/operations-backend/node_modules/express/lib/router/index.js:458抛出新的TypeError

  • 我正试图将即时消息添加到现有的应用程序中。但是我不确定应该如何配置socket.io模块。我已经尝试了以下方法: 因为打开一个页面时没有记录connected,所以我假设丢失了一些东西。我的错误在哪里?