您好我有一个Spring Web应用程序,我已经把它建立到控制器的阶段,我可以注入我的DAO,为我服务的罚款。 现在,当我想我注入到服务我的控制器我得到一个错误使用DAO的依赖性进一步压低了一个SessionFactory。 我不想再注入这些导致这将最终导致我最终建立一个数据源,但我有我的DAO数据访问,他们已经知道SessionFactory的。 我失去了一些东西在这里?
这里的示例代码片段:
我的服务:
@Service("productService")
@Transactional
public class ProductServiceImpl implements ProductService {
private ProductDao productDao;
@Autowired
public void setDao(ProductDao productDao) {
this.productDao = productDao;
}
}
我的控制器
@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;
}
}
堆栈跟踪:
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:component-scan base-package="com.phumzile.acme.client" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
</beans>
应用程序,配置:
<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