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

ssh spring不知道问啥无法注入,该怎么处理

2012-03-01 
ssh spring不知道问啥无法注入XML codebean idsql classorg.apache.commons.dbcp.BasicDataSource

ssh spring不知道问啥无法注入

XML code
<bean id="sql" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName"            value="com.microsoft.sqlserver.jdbc.SQLServerDriver">        </property>        <property name="url"            value="jdbc:sqlserver://localhost:1433;databaseName=hr_db">        </property>        <property name="username" value="sa"></property>        <property name="password" value="sa"></property>    </bean>    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="sql" />        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">                    org.hibernate.dialect.SQLServerDialect                </prop>                <prop key="hibernate.show_sql">true</prop>            </props>        </property>        <property name="mappingResources">            <list>                <value>com/ssh/entity/Users.hbm.xml</value></list>        </property></bean>                <!-- dao -->        <bean id="baseDao" class="com.ssh.dao.BaseDao">            <property name="sessionFactory" ref="sessionFactory"></property>        </bean>        <bean id="userDao" class="com.ssh.dao.UsersDao" parent="baseDao">            <property name="baseDao" ref="baseDao"></property>        </bean>                                <!-- biz -->        <bean id="userBiz" class="com.ssh.biz.UsersBiz">            <property name="userDao" ref="userDao"></property>        </bean>                <!-- action -->        <bean name="/users" class="com.ssh.web.action.UsersAction">            <property name="userBiz" ref="userBiz"></property>        </bean>        </beans>


XML code
<struts-config>  <data-sources />  <form-beans >    <form-bean name="usersForm" type="com.ssh.web.form.UsersForm" />  </form-beans>  <global-exceptions />  <global-forwards />  <action-mappings >    <action      attribute="usersForm"      input="/login.jsp"      parameter="o"      name="usersForm"      path="/users"      scope="request"      type="com.ssh.web.action.UsersAction" />  </action-mappings>  <message-resources parameter="com.ssh.web.ApplicationResources" />   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">        <set-property             property="contextConfigLocation"            value="classpath:applicationContext.xml" />  </plug-in>  </struts-config>


java.lang.NullPointerException
at com.ssh.web.action.UsersAction.execute(UsersAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)


at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


[解决办法]
[Quote=引用 楼主 
 <!-- dao -->
<bean id="baseDao" class="com.ssh.dao.BaseDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userDao" class="com.ssh.dao.UsersDao" parent="baseDao">
<property name="baseDao" ref="baseDao"></property>
</bean>

<!-- biz -->
<bean id="userBiz" class="com.ssh.biz.UsersBiz">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
[/Quote]

这段配置 好像有问题把。。。
bean配置的class属性应该是接口实现。。。,那说明你的UsersBiz和UserDao为接口实现。。。
而在使用时的变量应该是注入接口才对啊。。。
private UsersBiz【应该是接口】 userBiz=null;
/*
* Generated Methods
*/
public UsersBiz getUserBiz() {
return userBiz;
}

public void setUserBiz(UsersBiz userBiz) {
this.userBiz = userBiz;
}

你这里UsersBiz竟然是配置里面的接口实现,,而这里应该是接口吧。。。注入应该是基于接口的吧。。。所以无法注入,,注入点都不对,好好检查。
[解决办法]
spring的IOC的方式有几种,你使用的是interface的set注入。所以你的dao和biz应该是有接口存在
如:

Java code
public interface UsersBiz(){}public class UsersBizImpl(){    private UsersDao usersDao;    public void setUsersDao(UsersDao usersDao){        this.usersDao = usersDao;    }}public interface UsersDao{}public class UsersDaoImpl implements UsersDao{}
[解决办法]

<!-- biz -->
<bean id="userBiz" class="com.ssh.biz.UsersBiz">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
应该变为 <bean id="userBiz" class="com.ssh.biz.UsersBizImpl">阿

热点排行