大神发点时间帮我看大神发点时间帮我看看 spring注入的怎么老是空的,我那里搞错了。看 spring注入的怎么老是空的,我那里搞错了。

Gemerl 2014-01-17 05:47:11
大神发点时间帮我看大神发点时间帮我看看 spring注入的怎么老是空的,我那里搞错了。看 spring注入的怎么老是空的,我那里搞错了。

帮忙看看 我struts里面配置有个地方老是报错。。
service 里面注入的dao是空值。
dao里面注入的 hibernateTemplate是空值。
大神帮忙看看spring配置文件 还有我tomcat启动成功没自动给我建表呢?设置了
<prop key="hibernate.hbm2ddl.auto">create</prop>

项目在这 帮忙呀! http://yunpan.cn/QzvTCWvmxgrah 帮忙看看。。
...全文
236 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fw347969680 2014-01-20
  • 打赏
  • 举报
回复
注解的不是很懂,帮顶下。
suciver 2014-01-20
  • 打赏
  • 举报
回复
<!-- <result name="save" type="redirectAction">addUser.action!getALL</result> 本身想重定向到action的但是上面的这行老是报错呀!! --> 动态方法调用应该这么写 <result name="save" type="redirectAction">addUser!getALL.action</result> 不过不建议用这种用,推荐使用以下这种 <result name="save" type="redirectAction"> <param name="actionName">addUser</param> <param name="method">getALL</param> </result>
Gemerl 2014-01-18
  • 打赏
  • 举报
回复
引用 1 楼 suciver 的回复:
楼主还是把关键的spring配置文件以及struts2的配置文件和关键的注入部分的代码贴出来吧 没人会有心情去下载你的项目在仔细的看的
struts配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="web" extends="struts-default">
		<action name="userAction" class="com.ssh.action.UserAction" method="getALL">
			<result name="getALL" type="dispatcher">/index.jsp</result>
			<!-- <result name="save" type="redirectAction">addUser.action!getALL</result> 
			本身想重定向到action的但是上面的这行老是报错呀!!
			 -->
		</action>
		<action name="addUser" class="com.ssh.action.UserAction" method="save">
			<result name="save">/index.jsp</result>
		</action>
	</package>
</struts>   
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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	                    http://www.springframework.org/schema/aop
                    	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                    http://www.springframework.org/schema/tx
     	   				http://www.springframework.org/schema/tx/spring-tx.xsd
	                    http://www.springframework.org/schema/context
                    	http://www.springframework.org/schema/context/spring-context-2.5.xsd"
                    	default-autowire="byName">
	<!-- 开启组件扫描 -->
	<context:component-scan base-package="com.ssh.*"/>
	
	<!-- 开启注解处理器 -->
	<context:annotation-config/>
	<!-- 声明切面 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!-- 数据源 -->
	<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?useUnicode=true&characterEncoding=utf-8">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<!-- 注入hibernate的依赖sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 注入dataSource -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<!-- 扫描hibernate的映射文件 -->
		<property name="mappingLocations">
			<value>classpath:com/ssh/entity/*.hbm.xml</value>
		</property>
		<!-- hibernate的配置文件 -->
		<property name="hibernateProperties">
			<props>
	    		<prop key="hibernate.show_sql">true</prop>
	    		<prop key="hibernate.format_sql">true</prop>
	    		<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			</props>
		</property>
	</bean>
	<!-- 配置事务管理器-->   
 	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    	<property name="dataSource" ref="dataSource"/>   
  	</bean> 
  	<tx:advice id="txAdvice" transaction-manager="txManager">
  		<tx:attributes>
  			<tx:method name="save*" propagation="REQUIRED"/>
  			<tx:method name="get*" read-only="true"/>
  		</tx:attributes>
  	</tx:advice>
  	<aop:config>
  		<aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
  		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
  	</aop:config>
  	<!-- 注入hibernate依赖hibernateTemplate -->
  	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
</beans>
dao层

package com.ssh.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.ssh.entity.User;
@SuppressWarnings("unchecked")
@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public void saveUser(User u) {
		this.getHibernateTemplate().save(u);
	}

	
	@Override
	public List<User> getAllUser() {
		return this.getHibernateTemplate().find("form User");
	}

}
service层

package com.ssh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssh.dao.UserDao;
import com.ssh.entity.User;
@Service("userService")
public class UserServiceImpl implements UserService {
	private UserDao userDao;
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	@Override
	public void saveUser(User u) {
		userDao.saveUser(u);
	}

	@Override
	public List<User> getAllUser() {
		return userDao.getAllUser();
	}

}
action代码

package com.ssh.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.User;
import com.ssh.service.UserService;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private User user;
	private UserService userService;
	private List<User> userList;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public UserService getUserService() {
		return userService;
	}

	public List<User> getUserList() {
		return userList;
	}
	
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public String getAll(){
		userList=userService.getAllUser();
		return "getALL";
	}
	
	public String save() throws Exception {
		userService.saveUser(user);
		return "save";
	}
}

其中action中的userService,service中的userDao,dao中的this.getHibernateTemplate()为空; 另外给我解释一下这几个标签作用是什么

<context:annotation-config/>
<aop:config>
<aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
<aop:aspect></aop:aspect>
</aop:config>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
Gemerl 2014-01-18
  • 打赏
  • 举报
回复
引用 3 楼 u013425421 的回复:
<context:component-scan base-package="com.ssh"/>
这句改成
<context:component-scan base-package="com.ssh.*"/>
改完没有用呀!还是空的。。
Gemerl 2014-01-18
  • 打赏
  • 举报
回复
引用 3 楼 u013425421 的回复:
<context:component-scan base-package="com.ssh"/>
这句改成
<context:component-scan base-package="com.ssh.*"/>
你下载我的ssh看了,谢谢。。等下我试试看。。
abrom01 2014-01-18
  • 打赏
  • 举报
回复
@Autowired一般都写在需要注入的资源上面,例如@Autowired private UserService userService; 关于最后几个标签,简单说下,是SpringAOP相关的配置文件,不懂的话可以百度下,pointcut就是声明的切入点,表明要拦截com.ssh.service包下的所有方法,说白了就是个拦截器
Lavener 2014-01-17
  • 打赏
  • 举报
回复
<context:component-scan base-package="com.ssh"/>
这句改成
<context:component-scan base-package="com.ssh.*"/>
学到了吗 2014-01-17
  • 打赏
  • 举报
回复
你是Struts和spring结合使用的,一般Struts里面的Fileter作为控制器,spring的配置文件是通过listener在web。xml中配置的,之所以你注入是空 很可能是配置文件在服务器启动时根本没有去执行
suciver 2014-01-17
  • 打赏
  • 举报
回复
楼主还是把关键的spring配置文件以及struts2的配置文件和关键的注入部分的代码贴出来吧 没人会有心情去下载你的项目在仔细的看的

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧