Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring isn't injecting beans, what am I doing wrong?
    primarykey
    data
    text
    <p>I'm trying to simply try out Spring, yet I seem to be missing something. It seems to load Spring and the beans just fine, however when it comes to injecting those beans with autowired, it doesn't work. Does anybody have a clue?</p> <p>The part for of web.xml for Spring and the mainServlet:</p> <pre><code> &lt;welcome-file-list&gt; &lt;welcome-file&gt;/login.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;!-- Spring Dependency Injection --&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; </code></pre> <p></p> <pre><code>&lt;!-- Login Filter --&gt; &lt;filter&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.nortal.pirs.presentation.web.LoginFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;secretParameter&lt;/param-name&gt; &lt;param-value&gt;8392&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/MainServlet&lt;/url-pattern&gt; &lt;servlet-name&gt;MainServlet&lt;/servlet-name&gt; &lt;/filter-mapping&gt; &lt;!-- Main Servlet --&gt; &lt;servlet&gt; &lt;servlet-name&gt;MainServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;com.nortal.pirs.presentation.web.MainServlet&lt;/servlet-class&gt; </code></pre> <p></p> <pre><code>&lt;servlet-mapping&gt; &lt;servlet-name&gt;MainServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/main/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>Spring application context file (Though I think I also loaded it with too much unnecessary crap, but it was out of desperation because it wasn't working):</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:annotation-config /&gt; &lt;!-- Turn on AspectJ @Configurable support --&gt; &lt;context:spring-configured /&gt; &lt;context:component-scan base-package="com.nortal.pirs.test.independent" /&gt; &lt;context:component-scan base-package="com.nortal.pirs.businesslogic.logic" /&gt; &lt;context:component-scan base-package="com.nortal.pirs.presentation.vaadin" /&gt; &lt;context:component-scan base-package="com.nortal.pirs.presentation.vaadin.views" /&gt; &lt;context:component-scan base-package="com.nortal.pirs.presentation.web" /&gt; &lt;!-- Turn on @Autowired, @PostConstruct etc support --&gt; &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /&gt; &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt; &lt;/beans&gt; </code></pre> <p>UserManagerLogic (The bean that has to be injected in MainServlet with @autowired later):</p> <pre><code>@Component("UserManager") public class UserManagerLogic implements UserManagerInterface { </code></pre> <p>MainServlet:</p> <pre><code> @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; } } </code></pre> <p>And the outcome is a null pointer exception on the call for userManager of course, which was supposed to be injected by Spring?</p> <pre><code>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) </code></pre> <p>However Spring does load the beans it only doesn't inject them and doesn't throw any error, why is that?</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload