gpt4 book ai didi

java - Spring 没有注入(inject) bean,我做错了什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:50 26 4
gpt4 key购买 nike

我正在尝试简单地试用 Spring,但我似乎遗漏了一些东西。它似乎可以很好地加载 Spring 和 bean,但是当涉及到使用 Autowiring 注入(inject)那些 bean 时,它不起作用。有人知道吗?

Spring 和 mainServlet 的 web.xml 部分:

    <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>

Spring 应用程序上下文文件(虽然我认为我也加载了太多不必要的废话,但它是出于绝望,因为它不起作用):

    <?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>

UserManagerLogic(稍后必须使用@autowired 将bean 注入(inject)MainServlet):

@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;
}
}

结果当然是在调用 userManager 时出现空指针异常,这应该由 Spring 注入(inject)?

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)

但是 Spring 确实加载了 bean,它只是不注入(inject)它们并且不抛出任何错误,这是为什么呢?

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

最佳答案

您的 servlet 不是@Configurable。由于它的生命周期不受 Spring 控制,这是您让它 Autowiring 的唯一方法。

哦,你的 servlet 绝对不是 @Service

关于java - Spring 没有注入(inject) bean,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com