首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

spring注入为空,各位大大帮小弟我看一下吧

2012-02-29 
spring注入为空,各位大大帮我看一下吧ApplicationContext-Commons.xmlXML code?xml version1.0 encodi

spring注入为空,各位大大帮我看一下吧
ApplicationContext-Commons.xml

XML code
<?xml version="1.0" encoding="utf-8"?><!-- beans是Spring配置文件的根元素,并且指定了Schema信息 --><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans                      http://www.springframework.org/schema/beans/spring-beans.xsd                      http://www.springframework.org/schema/tx                      http://www.springframework.org/schema/tx/spring-tx.xsd                      http://www.springframework.org/schema/aop                      http://www.springframework.org/schema/aop/spring-aop.xsd">    <!-- 定义数据源Bean,使用C3P0数据源实现 com.mchange.v2.c3p0.ComboPooledDataSource-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">        <property name="driverClass" value="com.mysql.jdbc.Driver" />        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/kds" />        <property name="user" value="root" />        <property name="password" value="123" />        <property name="initialPoolSize" value="10" />        <property name="minPoolSize" value="5" />        <property name="maxPoolSize" value="30" />        <property name="acquireIncrement" value="5" />        <property name="maxStatements" value="0" />    </bean>    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="mappingResources">            <list>                <value>com/kds/pojo/xml/User.hbm.xml</value>                <value>com/kds/pojo/xml/Authority.hbm.xml</value>                <value>com/kds/pojo/xml/Contract.hbm.xml</value>                <value>com/kds/pojo/xml/Contracttype.hbm.xml</value>                <value>com/kds/pojo/xml/Landmark.hbm.xml</value>                <value>com/kds/pojo/xml/Orders.hbm.xml</value>                <value>com/kds/pojo/xml/Product.hbm.xml</value>                <value>com/kds/pojo/xml/Project.hbm.xml</value>                <value> com/kds/pojo/xml/Projectuserrelation.hbm.xml</value>                <value>com/kds/pojo/xml/Rightrolerelation.hbm.xml</value>                <value>com/kds/pojo/xml/Role.hbm.xml</value>                <value>com/kds/pojo/xml/Suit.hbm.xml</value>                <value>com/kds/pojo/xml/Suitproductrelation.hbm.xml</value>                <value>com/kds/pojo/xml/Suitproductrelationinorder.hbm.xml</value>                <value>com/kds/pojo/xml/Task.hbm.xml</value>                <value>com/kds/pojo/xml/Taskuserrelation.hbm.xml</value>            </list>        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>                <prop key="hibernate.generate_statistics">true</prop>                <prop key="hibernate.connection.release_mode">auto</prop>                <prop key="hibernate.autoReconnect">true</prop>                <prop key="hibernate.show_sql">true</prop><!-- 显示sql -->                <prop key="hibernate.format_sql">true</prop><!-- 将sql脚本进行进行格式化后再输出 -->            </props>        </property>        <property name="dataSource" ref="dataSource" />    </bean>    <!--设置事务管理  -->    <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory">            <ref local="sessionFactory" />        </property>    </bean>    <!-- 配置事务的传播特性 ,指定事务管理器-->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <!-- 配置详细的事务语义 -->        <tx:attributes>            <tx:method name="create*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="*" read-only="true" />        </tx:attributes>    </tx:advice></beans> 



ApplicationContext-Actions.xml
XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">        <bean id="projectManagerAction" class="com.kds.action.ProjectManagerAction" scope="prototype">        <property name="pmService" ref="ProjectManagerService" />    </bean>        <bean id="contractManagerAction" class="com.kds.action.ContractManagerAction" scope="prototype">        <property name="pmService" ref="ProjectManagerService" />        <property name="cmService" ref="ContractManagerService" />    </bean>        <bean id="roleAndRightAction" class="com.kds.action.RoleAndRightAction" scope="prototype">        <property name="pmService" ref="ProjectManagerService" />        <property name="cmService" ref="ContractManagerService" />        <property name="pcService" ref="ProjectCustomerService" />    </bean>    </beans>


ApplicationContext-Service.xml
XML code
<?xml version="1.0" encoding="GBK"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">        <!-- 定义业务逻辑组件 -->    <bean id="projectTemplate" abstract="true" lazy-init="true">        <property name="projectDao" ref="projectDao" />        <property name="landmarkDao" ref="landmarkDao" />        <property name="taskDao" ref="taskDao" />        <property name="userDao" ref="userDao" />        <property name="roleDao" ref="roleDao" />        <property name="taskuserrelationDao" ref="taskuserrelationDao" />        <property name="projectuserrelationDao" ref="projectuserrelationDao" />    </bean>        <bean id="projectManagerService" class="com.kds.service.impl.ProjectManagerService" parent="projectTemplate" />    <bean id="projectCustomerService" class="com.kds.service.impl.ProjectCustomerService" parent="projectTemplate"/>            <bean id="contractTemplate" abstract="true" lazy-init="true">        <property name="userDao" ref="userDao"/>        <property name="contractDao" ref="contractDao"/>        <property name="contracttypeDao" ref="contracttypeDao" />        <property name="productDao" ref="productDao" />    </bean>        <bean id="contractManagerService" class="com.kds.service.impl.ContractManagerService" parent="contractTemplate" />    </beans> 



ApplicationContext-Daos.xml
XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">    <!-- 配置DAO组件模板 -->    <bean id="daoTemplate" abstract="true" lazy-init="true">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <bean id="projectDao" class="com.kds.dao.impl.ProjectDAO" parent="daoTemplate"/>    <bean id="landmarkDao" class="com.kds.dao.impl.LandmarkDAO" parent="daoTemplate"/>    <bean id="taskDao" class="com.kds.dao.impl.TaskDAO" parent="daoTemplate"/>    <bean id="userDao" class="com.kds.dao.impl.UserDAO" parent="daoTemplate"/>        <bean id="roleDao" class="com.kds.dao.impl.RoleDAO" parent="daoTemplate"/>    <bean id="taskuserrelationDao" class="com.kds.dao.impl.TaskuserrelationDAO" parent="daoTemplate"/>    <bean id="projectuserrelationDao" class="com.kds.dao.impl.ProjectuserrelationDAO" parent="daoTemplate"/>    <bean id="contractDao" class="com.kds.dao.impl.ContractDAO" parent="daoTemplate" />    <bean id="contracttypeDao" class="com.kds.dao.impl.ContracttypeDAO" parent="daoTemplate" />    <bean id="productDao" class="com.kds.dao.impl.ProductDAO" parent="daoTemplate"/>    </beans>



[解决办法]
你看看 你写的对不对??

 <property name="cmService" ref="ContractManagerService" />
你这个地方 大写?
<bean id="contractManagerService" class="com.kds.service.impl.ContractManagerService" parent="contractTemplate" />

</beans>

 在定义 ref 进行 。注入的时候 他会按照 名词 来查找的。。你并没有 指定 spring 以什么方式 装配 。

他会首先映射 到 名字查找。。。ref="ContractManagerService" 和 <bean id="contractManagerService" 

完全不一样。。当然包空指针了。。。
[解决办法]
探讨

引用:
还有我给你 提醒一下。。你看看 你自己的 头像 结帖率 。。。低的吓人。。。

下次如果你的结帖率 一直是这样。。你就别打算 在csdn 混了。。。

因为前几天我发过一次问题贴,一共只发过一次,没人有人留言……很抱歉,刚刚无满意结贴了。以后我会注意,谢谢提醒。

[解决办法]
web.xml中的配置
XML code
<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:applicationContext-*.xml</param-value>    </context-param> 

热点排行