Note that there are some explanatory texts on larger screens.

plurals
  1. POspring @inject not working in Controller
    text
    copied!<p>I am using context:component-scan, still the dependencies are not getting injected. </p> <p>Here is my setup. ConnectionHelper property is not getting injected in the LoginController class. </p> <p>WEB-INF/web.xml</p> <pre><code>&lt;context-param&gt; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/classes/log4j.properties&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;vd&lt;/servlet-name&gt; &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;vd&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>WEB-INF/applicationContext.xml</p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt; &lt;context:component-scan base-package="com.example" /&gt; &lt;/beans&gt; </code></pre> <p>WEB-INF/vd-servlet.xml</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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt; &lt;!-- Configures Handler Interceptors --&gt; &lt;mvc:interceptors&gt; &lt;bean class="com.example.interceptors.SslInterceptor" /&gt; &lt;mvc:interceptor&gt; &lt;mvc:mapping path="/login" /&gt; &lt;bean class="com.example.interceptors.SslInterceptor" /&gt; &lt;/mvc:interceptor&gt; &lt;/mvc:interceptors&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".html" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>com.example.controllers.LoginController.java</p> <pre><code>@Controller public class LoginController { @Inject private ConnectionHelper connectionHelper; //this is null after loading } </code></pre> <p>com.example.connection.ConnectionHelper.java</p> <pre><code>@Component public class ConnectionHelper { } </code></pre> <p>Please tell me what's wrong here...after hours of troubleshooting i can't determine the problem.</p> <p><strong>Update</strong></p> <p>I changed my configuration and moved everything to vd-servlet.xml.</p> <p>Quite miraculously I have found that <code>@Autowired</code> works (Injects Dependencies) with this configuration but <code>@Inject</code> does not.</p> <p>And <code>&lt;mvc:annotation-driven /&gt;</code> is still required even if you use <code>&lt;context:component-scan /&gt;</code>, otherwise @RequestMapping is not detected.</p> <p>vd-servlet.xml</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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt; &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="com.example" /&gt; &lt;mvc:interceptors&gt; &lt;bean class="com.example.interceptors.SslInterceptor" /&gt; &lt;mvc:interceptor&gt; &lt;mvc:mapping path="/login" /&gt; &lt;bean class="com.example.interceptors.SslInterceptor" /&gt; &lt;/mvc:interceptor&gt; &lt;/mvc:interceptors&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".html" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
 

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