struts2.3.16+spring3.2.3+mybatis3框架整合,无法注入?

so_co 发布于 2014/12/04 11:52
阅读 541
收藏 0

项目结构

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"  
    xmlns:context=" http://www.springframework.org/schema/context
    xmlns:aop=" http://www.springframework.org/schema/aop
    xmlns:jdbc=" http://www.springframework.org/schema/jdbc"  
    xmlns:tx=" http://www.springframework.org/schema/tx
    xsi:schemaLocation=" http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
           http://www.springframework.org/schema/jdbc  
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           default-autowire="byName" default-lazy-init="false">
           <!--
           <context:property-placeholder location="classpath:config/jdbc.properties"/>
           -->
           <!-- 采用注解的方式配置bean -->
     <context:annotation-config/>
     <!-- 配置要扫描的包 -->
     <context:component-scan base-package="com.ssm.demo"/>
           <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
           <property name="locations" value="classpath:config/jdbc.properties"></property>
          </bean>
           <!--
          -->
           <!-- 配置数据源 -->
           <!--
           <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
     <value>org.logicalcobwebs.proxool.ProxoolDriver
     </value>
    </property>
    <property name="url">
     <value>proxool.mySpringPool</value>
    </property>
   </bean>
            -->
           <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
            <property name="alias" value="${proxool.alias}"/>
            <property name="driverUrl" value="${proxool.url}"/>
            <property name="driver" value="${proxool.driver}"/>
            <property name="user" value="${proxool.user}"/>
            <property name="password" value="${proxool.password}"/>
            <property name="simultaneousBuildThrottle" value="100"/>
            <property name="maximumConnectionCount" value="10"/>
            <property name="minimumConnectionCount" value="3"/>
            <property name="houseKeepingTestSql" value="${proxool.houseKeepingTestSql}"/>
            <property name="trace" value="true"/>
           </bean>
            <!-- <property name="houseKeepingSleepTime" value="15000"/> -->
           <!-- 配置session工厂sqlSessionFactory -->
           <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
            <property name="mapperLocations" value="classpath*:config/mybatisMapper/*Mapper.xml"></property>
             <!--
             -->
           </bean>
           <!-- 声明事物 -->
           <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
           </bean>
           <!-- 配置mybatis数据处理方式 ,扫描接口 -->
           <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <property name="basePackage" value="com.ssm.demo.mapper"></property>
           </bean>
          
           <!-- 配置事物的传播特性
           <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
             <tx:method name="save*" propagation="REQUIRED"/>
             <tx:method name="del*" propagation="REQUIRED"/>
             <tx:method name="update*" propagation="REQUIRED"/>
             <tx:method name="get*" propagation="REQUIRED"/>
             <tx:method name="count*" propagation="REQUIRED"/>
             <tx:method name="select*" propagation="REQUIRED"/>
            </tx:attributes>
           </tx:advice>
           -->
           <!-- 切面配置
           <aop:config>
            <aop:pointcut expression="execution(* com.ssm.demo.service.*.*(..))" id="method"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="method"/>
           </aop:config>
            -->
</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
  <display-name>ssmDemo</display-name>
  <!-- 设置spring容器加载配置文件的路径 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:config/applicationContext.xml</param-value>
  </context-param>
  <!-- 设置log4j配置文件的路径 -->
  <context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>classpath:config/log4j.properties</param-value>
  </context-param>
  <!--
  <context-param>
  <param-name>proxoolXmlFile</param-name>
  <param-value>classpath:config/proxoolconfig.xml</param-value>
  </context-param>
   -->
  <!-- 加载spring容器的配置 -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 监听log4j的配置 -->
  <listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <!-- 设置字符编码 -->
  <filter>
   <filter-name>characterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>characterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置struts2过滤器  -->
  <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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app> 

action:

 

service:

serviceImpl:

 

UserMapper:

 

为什么注入不进去呢?userSerivce是null?

加载中
0
Candy_Desire
Candy_Desire
你测试下用setter方法注入一下,如果还是注入不了就是Struts配置的问题
so_co
so_co
thank you!搞定了,少了个struts2-spring-plugin-2.3.16.3.jar。
0
Candy_Desire
Candy_Desire
你在Spring的配置文件中添加扫描service包了么?
so_co
so_co
回复 @Candy_Desire : 结构图在下边,你看看!
Candy_Desire
Candy_Desire
回复 @so_co : 你的userService类在com.ssm.demo这个包里么?
so_co
so_co
哥们儿,那我该如何写呢?现在主要是扫描不到啊!无法注入!
Candy_Desire
Candy_Desire
回复 @so_co : 你要扫描所有包当然是可以扫描到的,我的意思是你确保你要自动注入的包能扫描到就好!
so_co
so_co
如果全包扫描呢?难道就扫描不到了吗?
下一页
0
so_co
so_co

这是项目目录@Candy_Desire

0
Candy_Desire
Candy_Desire
报错信息给我看下
so_co
so_co
回复 @Candy_Desire : 那为什么service为null呢?
Candy_Desire
Candy_Desire
回复 @so_co : 这个应该不是注入的问题,不能注入会提示具体哪个类不能注入的信息。
so_co
so_co
java.lang.NullPointerException at com.ssm.demo.action.UserAction.login(UserAction.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
0
so_co
so_co

这是debug中的

0
Candy_Desire
Candy_Desire
嗯,不客气!搞定了就好!
返回顶部
顶部