Spring 依赖注入空指针
saveUserAction.javapackagecom.OmeetingS.action.user;importcom.OmeetingS.bean.User;imp...
saveUserAction.java
package com.OmeetingS.action.user;
import com.OmeetingS.bean.User;
import com.OmeetingS.service.UserService;
import com.OmeetingS.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
public class saveUserAction extends ActionSupport
{
private User user;
private UserService service;
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public UserService getService()
{
return service;
}
public void setService(UserService service)
{
this.service = service;
}
@Override
public String execute() throws Exception
{
this.service.save(this.user);
return SUCCESS;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/Omeetingdb"></property>
<property name="username" value="root"></property>
<property name="password" value="399113"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/OmeetingS/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.OmeetingS.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userService" class="com.OmeetingS.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="saveUserAction" class="com.OmeetingS.action.user.saveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
</beans>
错误已经找出是service这个变量为null,可是这个依赖注入没发现任何问题,很奇怪 展开
package com.OmeetingS.action.user;
import com.OmeetingS.bean.User;
import com.OmeetingS.service.UserService;
import com.OmeetingS.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
public class saveUserAction extends ActionSupport
{
private User user;
private UserService service;
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public UserService getService()
{
return service;
}
public void setService(UserService service)
{
this.service = service;
}
@Override
public String execute() throws Exception
{
this.service.save(this.user);
return SUCCESS;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/Omeetingdb"></property>
<property name="username" value="root"></property>
<property name="password" value="399113"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/OmeetingS/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.OmeetingS.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userService" class="com.OmeetingS.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="saveUserAction" class="com.OmeetingS.action.user.saveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
</beans>
错误已经找出是service这个变量为null,可是这个依赖注入没发现任何问题,很奇怪 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询