如题:
##Application配置文件##
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL">
</property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/play/Bean/Area.hbm.xml</value></list>
</property></bean>
<bean id="AreaDAO" class="com.play.IDaoImpl.AreaDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="IAreaService" class="com.play.IServiceImpl.IAreaServiceImpl">
<property name="iAreaDao">
<ref bean="AreaDAO" />
</property>
</bean>

<bean id="AreaAction" class="com.play.Action.AreaAction">
<property name="areaService">
<ref bean="IAreaService" />
</property>
</bean>

</beans>##Action类##
package com.play.Action;import com.opensymphony.xwork2.ActionSupport;
import com.play.Bean.Area;
import com.play.IService.IAreaService;public class AreaAction extends ActionSupport{
public Area area;
public IAreaService areaService;
public String addArea()
{
areaService.addArea(area);
return "success";
}

public Area getArea() {
return area;
} public void setArea(Area area) {
this.area = area;
} public IAreaService getAreaService() {
return areaService;
} public void setAreaService(IAreaService areaService) {
this.areaService = areaService;
}
}程序运行起来,Action在执行addArea方法的时候,areaService对象为空,这个为什么呢?.是还有哪里需要配置还是什么问题?

解决方案 »

  1.   


    <bean id="IAreaService" class="com.play.IServiceImpl.IAreaServiceImpl">
    <property name="iAreaDao">
    <ref bean="AreaDAO" />
    </property>
    </bean>id="IAreaService" 改为 id="areaService"
      

  2.   

    public void setAreaService(IAreaService areaService) {
    System.out.println("-------------------------") ;//看看 有没有信息出来
    this.areaService = areaService;
    }
      

  3.   

    你的service为空还是dao为空,或者sessionFactory本身就是空?
      

  4.   

    public IAreaService areaService
    属性定义都是private的,不知道是不是这个原因。
    不知道。
      

  5.   


    你要先确定到底哪个为空?一步步,首先,debug,首先确认dao中,dataSource注入成功,再确认service中,dao注入成功。
      

  6.   

    一、你先看下你给了set方法没,二、清理缓存,三、实在不行就删掉重新写,反正你的那里面的东西也不多
      

  7.   

    set方法是有的,但是貌似是没走set方法.
      

  8.   


    汗!你得好好学学Spring的注入...
      

  9.   

    从楼主给的代码看是没问题的。只能一步一步看了~service dao sessionfactory datasource
      

  10.   

     <!--这一行必须配置,不然无法注入bean。 这个配置默认会把annotation-config(注解功能)也一并打开而不必另外配置 -->
     <context:component-scan base-package="com" />
     
     
    如果解决了,告诉我一下。
      

  11.   

    大神能告诉我这一行是写到哪里?applicationContext.xml里面?
      

  12.   

    applicationContext.xml里面,你找个模板照着写,配置文件太容易错了,不要纠结在配置文件上。
      

  13.   

    从配置文件的配置来看是没有问题的,但是你出现的问题可能跟的代码有很大的关系
       1.配置文件中的内容不规范 (spring的规范是第一个字符实现小写,其他的字符随便(随便不是说都可以大写))
       2.类写的不规范
         <bean id="IAreaService" class="com.play.IServiceImpl.IAreaServiceImpl">
          这个表示IServiceImpl的内部类 还是什么 我认为你肯定是包
       把上面的修正了应该就能解决你的问题。spring的注入式没有问题的
         如果以上方法解决了。你再试试给个bean叫一个全大写的 spring也是注入不成功.