工程目录:
这个是controller:
这个是controller:
@Controller public class ItemsController { @Autowired private ItemsService itemsService;//这里无法自动注入 @RequestMapping("/queryItems") public ModelAndView qurryItems() throws Exception { List<ItemsCustom> itemsList = itemsService.findItemsList(null); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("itemsList", itemsList); modelAndView.setViewName("items/itemsList"); return modelAndView; } }
这个是service:
public interface ItemsService { public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
这个是service的实现类:
public class ItemsServiceImpl implements ItemsService { @Autowired private ItemsMapperCustom itemsMapperCustom; public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo); } }
springmvc的配置:
<context:component-scan base-package="com.fww.ssm.controller"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
applicationContext-service的配置:
<bean id="itemsService" class="com.fww.ssm.service.impl.ItemsServiceImpl"/>
web.xml的配置:
<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF\classes\spring\applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
下面是报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name "itemsController": Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fww.ssm.service.ItemsService com.fww.ssm.controller.ItemsController.itemsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fww.ssm.service.ItemsService] 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)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:619)
百度各种。有说要加注解@service和注解@Repository的。都没效果。
解决方案
10
<context:component-scan base-package=”com.fww.ssm.controller”/>
这个改为
<context:component-scan base-package=”com.fww.ssm”/>这个试试
或将Controller 也用bean的形式试试看
这个改为
<context:component-scan base-package=”com.fww.ssm”/>这个试试
或将Controller 也用bean的形式试试看
10
要注入的接口只能有一个实现类。你可以试试@Resource(beanId)
5
<context:component-scan base-package=”com.fww.ssm.controller”
use-default-filters=”false”>
<context:include-filter type=”annotation”
expression=”org.springframework.stereotype.Controller” />
</context:component-scan>
注解扫描
use-default-filters=”false”>
<context:include-filter type=”annotation”
expression=”org.springframework.stereotype.Controller” />
</context:component-scan>
注解扫描
5
实现类没有声明service,长得这样,有印象不。【@Service】
10
这位朋友说的有道理。你应该在service层实现类里面加上@service,将服务层注入,才能引用:
这位朋友说的有道理。你应该在service层实现类里面加上@service,将服务层注入,才能引用
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapperCustom itemsMapperCustom;
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
throws Exception {
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
楼上的亲这个是spring管理事务用的,现在他没有事务的概念,连数据库都没有,不需要这个
5
@Service
public class ItemsServiceImpl implements ItemsService {
加上]@Service试下
public class ItemsServiceImpl implements ItemsService {
加上]@Service试下
5
applicationContext配置文件中有没有加<context:component-scan base-package=”com.fww.ssm”/>