对一个Spring依赖注入问题的解答
2007-08-10 19:43
513 查看
一直以为,我都是一个很能复制代码的人,把别人的代码从一个地方搬到另外一个地方并且能跑起来似乎是我很容易就能办到的事情,但是昨天晚上出了点意外。
Spring的依赖注入我想大家都很熟悉了,不外乎是在WebApplicationContext的环境中定义一大堆bean,然后写清楚它们的依赖引用关系和值的关系,就这样,一个利用配置就可以实现bean关系结构就出来了,就是这么一个简单的配置,我却搞了几个小时,原因是什么呢?
先来给大家看看我的代码配置:
首先定义Spring的contextConfigLocation的读取xml文件是spring-context.xml,在这个文件中指明了一些数据库连接的文件和一些数据源的信息,还有一些bean的依赖关系。由于我采用的是一般大家使用的三层结构,所以还还应该给controller定义一些dao的依赖实现,这部分的spring-context.xml的代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="demoDaoImpl" class="com.springdemo.dao.DemoDaoImpl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jdbcTemplate" ref="jdbcTemplate"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="dispatchCtrl" class="com.springdemo.controller.DispatchCtrl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="demoDao">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="demoDaoImpl"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
定义一个servlet的名称为dispatch,则dispatch-servlet.xml的部分代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="handlerMapping"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultHandler" ref="dispatchController"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="dispatchController" class="com.abchina.springdemo.controller.DispatchCtrl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="methodNameResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="mappings">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="/process">process</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
DispatchCtrl类的简单实现如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class DispatchCtrl extends MultiActionController ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DemoDao demoDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDemoDao(DemoDao demoDao) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoDao = demoDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String des = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Map map = new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(des, map);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ModelAndView process(HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String des = "dispatchView/result";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
logger.debug("demoDao= " + demoDao);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UserModel model = demoDao.getDateById("01000001");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(des, "model", model);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
一切都配置妥当了,就等着运行了,跑的时候郁闷的事情出来了。DispatchCtrl类死活得不到DemoDao的实例,那个该死的demoDao怎么都注不进去......
Spring Framework这个曾经令我钦佩的框架结构第一次让我在小河沟里翻了船,配置的这么好,怎么可能注入不了呢?令我足足郁闷了一天......
都说一个人在极度失望的时候就会看到事情的转机,因为我确实是在开始怀疑是不是我乘公汽没有主动给老大爷让座或者是那天在地上拣到五分钱没有交公而造成的这个莫名其妙的错误的时候,转机出现了。冥冥中我好像看到了相同一个class我定义了两遍,天哪,难道真是这个原因???于是我把spring-context.xml中明为dispatchCtrl的bean定义删掉了,把demoDaoImpl的注入写在了dispatch-servlet.xml的dispatchController bean中,为了方便,我把demoDaoImpl也写在了demoDaoImpl中,再运行,就好了。
这个问题值得好好总结一下,难道是Spring在启动加载类的时候相同的类就不再第二次注入了?暂时不得而解,先记录再此,看看有没有大侠给我个合理的解释。
Spring的依赖注入我想大家都很熟悉了,不外乎是在WebApplicationContext的环境中定义一大堆bean,然后写清楚它们的依赖引用关系和值的关系,就这样,一个利用配置就可以实现bean关系结构就出来了,就是这么一个简单的配置,我却搞了几个小时,原因是什么呢?
先来给大家看看我的代码配置:
首先定义Spring的contextConfigLocation的读取xml文件是spring-context.xml,在这个文件中指明了一些数据库连接的文件和一些数据源的信息,还有一些bean的依赖关系。由于我采用的是一般大家使用的三层结构,所以还还应该给controller定义一些dao的依赖实现,这部分的spring-context.xml的代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="demoDaoImpl" class="com.springdemo.dao.DemoDaoImpl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jdbcTemplate" ref="jdbcTemplate"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="dispatchCtrl" class="com.springdemo.controller.DispatchCtrl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="demoDao">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="demoDaoImpl"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
定义一个servlet的名称为dispatch,则dispatch-servlet.xml的部分代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="handlerMapping"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultHandler" ref="dispatchController"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="dispatchController" class="com.abchina.springdemo.controller.DispatchCtrl">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="methodNameResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="mappings">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="/process">process</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
DispatchCtrl类的简单实现如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class DispatchCtrl extends MultiActionController ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DemoDao demoDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDemoDao(DemoDao demoDao) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoDao = demoDao;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String des = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Map map = new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(des, map);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ModelAndView process(HttpServletRequest request, HttpServletResponse response) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String des = "dispatchView/result";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
logger.debug("demoDao= " + demoDao);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UserModel model = demoDao.getDateById("01000001");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(des, "model", model);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
一切都配置妥当了,就等着运行了,跑的时候郁闷的事情出来了。DispatchCtrl类死活得不到DemoDao的实例,那个该死的demoDao怎么都注不进去......
Spring Framework这个曾经令我钦佩的框架结构第一次让我在小河沟里翻了船,配置的这么好,怎么可能注入不了呢?令我足足郁闷了一天......
都说一个人在极度失望的时候就会看到事情的转机,因为我确实是在开始怀疑是不是我乘公汽没有主动给老大爷让座或者是那天在地上拣到五分钱没有交公而造成的这个莫名其妙的错误的时候,转机出现了。冥冥中我好像看到了相同一个class我定义了两遍,天哪,难道真是这个原因???于是我把spring-context.xml中明为dispatchCtrl的bean定义删掉了,把demoDaoImpl的注入写在了dispatch-servlet.xml的dispatchController bean中,为了方便,我把demoDaoImpl也写在了demoDaoImpl中,再运行,就好了。
这个问题值得好好总结一下,难道是Spring在启动加载类的时候相同的类就不再第二次注入了?暂时不得而解,先记录再此,看看有没有大侠给我个合理的解释。
相关文章推荐
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
- Spring实例:Ioc依赖注入的一个例子
- spring按类型进行依赖注入引起的问题
- 关于spring 注入中循环 ,环形依赖问题的明确
- spring boot项目依赖spring mvc项目注入失败问题
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- 使用spring的aop对Struts2的Action拦截后出现依赖注入为空问题
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
- 在.NET Core中处理一个接口多个不同实现的依赖注入问题
- 在SSH整合中,关于spring依赖注入失败的问题
- 关于spring容器依赖注入接口的小问题
- 关于Spring的依赖注入问题
- 使用spring的aop实现权限拦截后出现依赖注入为空的问题
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- Spring单元测试时出现的一个奇怪问题:找不到要注入的bean
- 关于spring依赖注入的一个知识点
- 解决WebSocket无法依赖注入的问题(Spring)
- Spring一个类包含多个构造函数且带的参数相同所存在的注入类型问题
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- 使用spring的aop实现拦截action后出现依赖注入为空的问题