问题描述

项目中的资源文件中写了个properties文件,内容这样的

CUST_FROM=002
CUST_INDUSTORY=001
CUST_LEVEL=006

在springmvc配置文件中加载设这样的

<context:property-placeholder location="classpath:resources.properties"/>
<context:component-scan base-package="com.crm.controller"/>

在spring中配置了注解扫描是这样

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

最后在代码中调用

ackage com.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.crm.domain.BaseDict;
import com.crm.service.BaseDictService; @Controller
public class BaseDictController { @Autowired
private BaseDictService baseDictService; **@Value("${CUST_INDUSTORY}")**
private String CUST_INDUSTORY; **@Value("${CUST_LEVEL}")**
private String CUST_LEVEL; **@Value("${CUST_FROM}")**
private String CUST_FROM; @RequestMapping("basedict_list")
public String basedictList(Model model){
List<BaseDict> listFrom = baseDictService.queryBasedict(CUST_FROM);
List<BaseDict> listIndustory = baseDictService.queryBasedict(CUST_INDUSTORY);
List<BaseDict> listLevel = baseDictService.queryBasedict(CUST_LEVEL);
model.addAttribute("fromType", listFrom);
model.addAttribute("industryType", listIndustory);
model.addAttribute("levelType", listLevel); return "list";
} }

问题分析和解决方案

出现这个错误其实说一个spring父子容器的问题 
我在spring中配置的注解扫描,会将带注解的所有的对象进行依赖注入,并完成实例化,我在spring容器中并没有加载我自定义的properties文件,所以spring在依赖注入时在容器中找不到这些属性值,从而spring容器初始化失败。而我的properties文件是在springmvc的配置文件加载的,在springmvc的容器中会存在这些properties属性值,在springmvc中配置了我的控制器controller的扫描,那么该容器实例化我们的控制器会将属性注入到对象中。

怎样解决呢,就是在spring配置中不用让它去实例化我们的cotroller,只让springmvc实例化就可以了。 
所以在springmvc扫描包直接这样

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

在spring配置文件中不要扫描controller这个包即可。

最新文章

  1. Security6:授予权限的思路和一般步骤
  2. flash 定义二维数组
  3. MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常
  4. linux下ssh远程登录服务器入门操作
  5. robotframework笔记1
  6. SDWebImage缓存清理
  7. HW4.2
  8. Javascript绝句欣赏
  9. linux lvm的操作手册_pvcreate_vgcreate_lvcreate_相关
  10. MCS-51系列和80C51系列单片机是否相同
  11. windows程序消息机制(Winform界面更新有关)--转
  12. Linux命令 ls -l s输出内容含义详解
  13. pandas和spark的dataframe互转
  14. php-cgi占用太多cpu资源而导致服务器响应过慢
  15. ajax,分页器
  16. MVC ---- 无法将类型&quot;System.Data.EntityState&quot;隐式转换为&quot;System.Data.Entity.EntityState&quot;
  17. 神奇的负Margin
  18. js 日历插件开发
  19. SwaggerUI用户手册
  20. 「UOJ218」火车管理

热门文章

  1. digitalpersona 开发(系统托盘,监听指纹扫描)
  2. override new 的区别
  3. scala中Map集合的简单使用
  4. 下载mysql出现的问题
  5. dos中文显示乱码怎么办?
  6. duilib库分析4.第二篇UIBase
  7. Object 和Throwable
  8. 重写、super关键字、final关键字、多态、子类型转换、抽象的初步了解
  9. SUMMARY | JAVA中的数据结构
  10. LINUX交换分区