求助,关于SpringMvc的注入问题

蒙奇D丶路飞 2019-05-19 04:02:18
项目结构


controller层代码
@Controller
@RequestMapping("/customer")
public class CustomerController {

@Autowired
private CustomerService customerService;

@Value("${customer.from.type}")
private String Source;
@Value("${customer.dict.industry}")
private String industry;
@Value("${customer.dict.level}")
private String level;

@RequestMapping("/list")
public String list(QueryVo vo,Model model) throws Exception{
//客户来源
List<BaseDict> sourceList =customerService.findDictByCode(Source);

//所属行业
List<BaseDict> industryList =customerService.findDictByCode(industry);

//客户级别
List<BaseDict> levelList=customerService.findDictByCode(level);

if(null !=vo.getCustName()) {
vo.setCustName(new String(vo.getCustName().getBytes("iso8859-1"),"utf-8"));
}


model.addAttribute("fromType", sourceList);
model.addAttribute("industryType", industryList);
model.addAttribute("levelType", levelList);

model.addAttribute("custName", vo.getCustName());
model.addAttribute("custSource", vo.getCustSource());
model.addAttribute("custIndustry", vo.getCustIndustry());
model.addAttribute("custLevel", vo.getCustLevel());
return "customer";
}

}
applicationContext-service.xml配置
<!-- @Service扫描 -->
<context:component-scan base-package="com.huawei.crm.service"></context:component-scan>

springmvc.xml文件配置
<!-- 引入字典资源文件 -->
<context:property-placeholder location="classpath:resource.properties"/>

<!-- @Controller注解扫描 -->
<context:component-scan base-package="com.huawei.crm.controller"></context:component-scan>

<!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.huawei.crm.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>

web.xml文件中的配置
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!--
*.action 代表拦截后缀名为.action结尾的
/ 拦截所有但是不包括.jsp
/* 拦截所有包括.jsp
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- 配置Post请求乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

报的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.huawei.crm.service.CustomerService com.huawei.crm.controller.CustomerController.customerService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.huawei.crm.service.CustomerService] 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)}


已经尝试过将@Autowired修改为@Resource,但是还是报这个错误。

包名是自己瞎取的!!!
...全文
115 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒙奇D丶路飞 2019-05-21
  • 打赏
  • 举报
回复
引用 1 楼 toward_south 的回复:
你customerService代码呢
忘记弄上来了
蒙奇D丶路飞 2019-05-21
  • 打赏
  • 举报
回复
引用 2 楼 鵬程萬裡 的回复:
这个问题可能是是你的Service没有@Service注解导致的
里面有写
toward_south 2019-05-19
  • 打赏
  • 举报
回复
你customerService代码呢

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧