Service injection into Controller (Spring MVC)
- by ThaSaleni
Hi I have a Spring web application, I have built it up to the controller stage and I could inject my Daos, into my Services fine. Now when I want to inject my Service into my controller i get an error for dependency with the Dao and further down the sessionFactory. I don't want to inject these again cause this will ultimately lead me to eventually create a data source but I have my Daos for data access and they already know about sessionFactory. Am I missing something here?
here's the sample code snippets
My Service:
@Service("productService")
@Transactional
public class ProductServiceImpl implements ProductService {
private ProductDao productDao;
@Autowired
public void setDao(ProductDao productDao) {
this.productDao = productDao;
}
My Controller
@Controller
@WebServlet(name="controllerServlet", loadOnStartup=
urlPatterns=...})
public class ControllerServlet extends HttpServlet {
boolean isUserLogedIn =false;
@Autowired
private ProductService productService;
public void setProductService(ProductService productService){
this.productService = productService;
}
Servlet-context
Stack trace
javax.servlet.ServletException: Servlet.init() for servlet mvcServlet threw
exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java: 565)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
root cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controllerServlet': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.phumzile.acme.services.ProductService com.phumzile.acme.client.web.controller.ControllerServlet.productService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.phumzile.acme.services.ProductService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.p ostProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
SERVLET-CONTEXT
<context:component-scan base-package="com.phumzile.acme.client" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
</beans>
APP-CONFIG
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>configuration.properties</value>
</list>
</property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.phumzile.acme" />
<import resource="db-config.xml" />
</beans>
DB-CONFIG
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="idleConnectionTestPeriod" value="10800"/>
<property name="maxIdleTime" value="21600"/>
<property name="driverClass">
<value>${jdbc.driver.className}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.a
nnotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="annotatedClasses">
<list>
<!-- Entities -->
<value>com.phumzile.acme.model.User</value>
<value>com.phumzile.acme.model.Person</value>
<value>com.phumzile.acme.model.Company</value>
<value>com.phumzile.acme.model.Product</value>
<value>com.phumzile.acme.model.Game</value>
<value>com.phumzile.acme.model.Book</value>
<!-- Entities -->
</list>
</property>
<property name="packagesToScan" value="com.phumzile.acme" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.hibernate.dialect
</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:annotation-driven />
</beans>
CONFIGURATION.PROPERTIES
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=root
jdbc.hibernate.dialect=org.hibernate.dialect.MySQLDialect