Sumit Jain
Sumit Jain

Reputation: 1528

spring @inject not working in Controller

I am using context:component-scan, still the dependencies are not getting injected.

Here is my setup. ConnectionHelper property is not getting injected in the LoginController class.

WEB-INF/web.xml

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>vd</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>



<servlet-mapping>
    <servlet-name>vd</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

WEB-INF/applicationContext.xml

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

<context:component-scan base-package="com.example" />

</beans>

WEB-INF/vd-servlet.xml

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

<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <bean class="com.example.interceptors.SslInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/login" />
        <bean class="com.example.interceptors.SslInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html" />
</bean>
</beans>

com.example.controllers.LoginController.java

@Controller
public class LoginController {

@Inject
private ConnectionHelper connectionHelper;    //this is null after loading

}

com.example.connection.ConnectionHelper.java

@Component
public class ConnectionHelper {

}

Please tell me what's wrong here...after hours of troubleshooting i can't determine the problem.

Update

I changed my configuration and moved everything to vd-servlet.xml.

Quite miraculously I have found that @Autowired works (Injects Dependencies) with this configuration but @Inject does not.

And <mvc:annotation-driven /> is still required even if you use <context:component-scan />, otherwise @RequestMapping is not detected.

vd-servlet.xml

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

<mvc:annotation-driven />
<context:component-scan base-package="com.example" />

<mvc:interceptors>
    <bean class="com.example.interceptors.SslInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/login" />
        <bean class="com.example.interceptors.SslInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html" />
</bean>
</beans>

Upvotes: 2

Views: 4101

Answers (3)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

Agree with @Alex, just that the file where you are missing <context:component-scan/> or <context:annotation-config/> is not applicationContext.xml file but vd-servlet.xml file.

Either <context:annnotation-config/> or <context:component-scan/> will register a AutowiredAnnotationBeanPostProcessor responsible for handling @Autowired, @Resource, @Inject annotations

Upvotes: 1

Roy
Roy

Reputation: 3624

You need to add getters and setters to the LoginController. Another option is to make connectionHelper a protected variable. This is how Spring "sees" the property to be injected.

In LoginController:

@Inject
private ConnectionHelper connectionHelper;

public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}

public void setConnectionHelper(ConnectionHelper connectionHelper) {
    this.connectionHelper = connectionHelper;
}

Upvotes: 0

Alex Barnes
Alex Barnes

Reputation: 7218

I believe that your applicationContext.xml is missing:

<context:annotation-config>

this registers Springs post processors for annotation based configuration.

Upvotes: 0

Related Questions