直接粘代码吧
applicationContext.xml 写道
<!-- 加载相关配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:applicationContext.properties</value>
</list>
</property>
</bean>
<!-- DBCP 数据源 配置-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!--JDBC驱动程序类名 -->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<!--连接字符串 -->
<property name="url" value="${jdbc.url}"/>
<!--数据源用户名 -->
<property name="username" value="${jdbc.username}"/>
<!--数据源密码 -->
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate相关 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.xixi.school.model.Player</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.autoCommit">true</prop>
</props>
</property>
</bean>
<!-- 加载其他配置文件-->
<import resource="classpath*:applicationContext-*.xml"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:applicationContext.properties</value>
</list>
</property>
</bean>
<!-- DBCP 数据源 配置-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!--JDBC驱动程序类名 -->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<!--连接字符串 -->
<property name="url" value="${jdbc.url}"/>
<!--数据源用户名 -->
<property name="username" value="${jdbc.username}"/>
<!--数据源密码 -->
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate相关 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.xixi.school.model.Player</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.autoCommit">true</prop>
</props>
</property>
</bean>
<!-- 加载其他配置文件-->
<import resource="classpath*:applicationContext-*.xml"/>
applicationContext-player.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="playerDao" class="com.xixi.school.dao.impl.PlayerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- service -->
<bean id="playerService" class="com.xixi.school.service.impl.PlayerServiceImpl">
<property name="playerDao" ref="playerDao" />
</bean>
</beans>
上面为spring 两个配置文件 <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="playerDao" class="com.xixi.school.dao.impl.PlayerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- service -->
<bean id="playerService" class="com.xixi.school.service.impl.PlayerServiceImpl">
<property name="playerDao" ref="playerDao" />
</bean>
</beans>
/** * */ private static final long serialVersionUID = 1L; private IPlayerService playerService; private Player player; public String savePlayer(){ playerService.savePlayer(player); return SUCCESS; } public Player getPlayer() { return player; } public void setPlayer(Player player) { this.player = player; } public void setPlayerService(PlayerServiceImpl playerService) { this.playerService = playerService; }
上面为struts2 的Action private IPlayerService playerService; 是spring 注入. 项目启动时,能访问到action,对象Player可以得到页面提交值,但playerService一直报NPE错?