spring2.5+hibernate3.2+struts2.1.6
整合的时候,我先是用junit测试了一下,能获取到UserService,也能存到数据库了。。
但是从页面访问的时候,在Action中却报错,UseService为空,说明注入失败了。。@Resource
private UserService userService;public UserService getUserService() {
return userService;
}public void setUserService(UserService userService) {
this.userService = userService;
}这说明和spring的配置文件没有关系吧? 因为junit测试能通过。
web.xml文件如下: <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
请帮我分析下,问题可能在什么地方?
没有报错。就是userService为空。当调用userService时,空指针异常。
在线等谢谢了@
整合的时候,我先是用junit测试了一下,能获取到UserService,也能存到数据库了。。
但是从页面访问的时候,在Action中却报错,UseService为空,说明注入失败了。。@Resource
private UserService userService;public UserService getUserService() {
return userService;
}public void setUserService(UserService userService) {
this.userService = userService;
}这说明和spring的配置文件没有关系吧? 因为junit测试能通过。
web.xml文件如下: <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
请帮我分析下,问题可能在什么地方?
没有报错。就是userService为空。当调用userService时,空指针异常。
在线等谢谢了@
spring的配置文件?
而且很多 那请大家看看吧。。
<?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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.test"></context:component-scan> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingLocations" value="classpath:com/test/*/*.hbm.xml">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">
true
</prop>
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
private UserService userService;
public boolean checkUserExit(String username){
User getUser = null;
try {
System.out.println(userService);
getUser = userService.find(username);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(getUser.getName());
if(getUser!=null) {
return true;
}else {
return false;
}
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
你用的是SSH吗 在你的spring配置文件里没看到action的注入
我用的是注解方式 所以在spring的配置文件中没有注入。