日期:2014-05-17  浏览次数:20784 次

SSH整合,SPRING注入失败
如题:
##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.配置文件中的内容不规范 (spring的规范是第一个字符实现小写,其他的字符随便(随便不是说都可以大写))
2.类写的不规范
<bean id="IAreaService" class="com.play.IServiceImpl.IAreaServiceImpl">
这个表示IServiceImpl的内部类 还是什么 我认为你肯定是包
把上面的修正了应该就能解决你的问题。spring的注入式没有问题的
如果以上方法解决了。你再试试给个bean叫一个全大写的 spring也是注入不成功.