2 u012380967 u012380967 于 2016.04.29 10:16 提问

@autowire在非jersey服务类中不注入(spring+jersey_mybatis) 5C

使用@autowired可以在jersey的服务类中注入,但是在其它类中就无法注入,显示@autowired的对象为空。

spring.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:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <!-- 自动将包前缀为cc.rinoux.server.*的包中以@Service@Component@Controller标注的类注册为bean
            在jersey中,ResourceConfig的实现类将服务所在的包注册(***packages("cc.rinoux.server.service");***)
            相当于为包中类添加了@Component注解 -->
    <context:component-scan base-package="cc.rinoux.server.*" />    
        <context:component-scan base-package="cc.rinoux.server.mina.handler" /> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
        <!-- 本地数据库配置 -->
 略

    </bean>
    <!-- sqlSessionFactoryBean相当于sqlSrssionFactoryBuilder,在mybatis-spring用法中,
    无需直接使用bean或sqlsessionFactory,session工厂将会被注入到MapperFactoryBean或
    其它扩展了sqlSessionSupport的DAO
     -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configLocation" value="classpath:mybatis.xml"></property>
      <!-- 将mapper包中所有文件和XXXmapper.xml文件对应起来 -->
      <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>
    <!-- dao 自动扫描mapper中的DAO,并注册为bean-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="cc.rinoux.server.mapper" />
    </bean>
    <!--  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />


</beans>

1个回答

u012587693
u012587693   2016.04.29 16:51

报的什么错?贴出来看看

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!