夏天的小虫 2017-01-04 09:18 采纳率: 0%
浏览 2034

Spring 注解的bean为null,但是log打印看到已经创建好了

建了个小项目测试mongodb,使用spring+Jersey+mongodb

使用Spring注解了controller、service、repository

如下:UserController.class
@Controller
@Path("user")
public class UserController {

Logger mLogger = Logger.getLogger(UserController.class);

@Autowired
IUserService mUserService;

@GET
@Path("/signup")
@Produces({MediaType.APPLICATION_JSON })
public String signup(@QueryParam("phone") String phone,@QueryParam("name") String name,@QueryParam("age") int age,@QueryParam("pw") String pw) throws Exception
{   
}

在启动日志里可以看到userDao,userService等都已经初始化好,但是通过浏览器访问时报mUserService空指针,

2017-01-04 17:06:26,769 DEBUG (org.mongodb.driver.cluster:56) - Updating cluster description to {type=STANDALONE, servers=[{address=192.168.0.116:27017, type=STANDALONE, roundTripTime=0.5 ms, state=CONNECTED}]
2017-01-04 17:06:26,857 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:523) - Eagerly caching bean 'mongoTemplate' to allow for resolving potential circular references
2017-01-04 17:06:26,863 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:478) - Finished creating instance of bean 'mongoTemplate'
2017-01-04 17:06:26,864 DEBUG (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:480) - Autowiring by type from bean name 'userDao' to bean named 'mongoTemplate'
2017-01-04 17:06:26,865 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:478) - Finished creating instance of bean 'userDao'
2017-01-04 17:06:26,865 DEBUG (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:480) - Autowiring by type from bean name 'userService' to bean named 'userDao'
2017-01-04 17:06:26,865 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:478) - Finished creating instance of bean 'userService'
2017-01-04 17:06:26,865 DEBUG (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:480) - Autowiring by type from bean name 'userController' to bean named 'userService'
2017-01-04 17:06:26,865 DEBUG (org.springframework.beans.factory.annotation.InjectionMetadata:86) - Processing injected element of bean 'userController': AutowiredFieldElement for com.mongo.dao.IUserDao com.mongo.controller.UserController.mUserDao
2017-01-04 17:06:26,866 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'userDao'
2017-01-04 17:06:26,866 DEBUG (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:480) - Autowiring by type from bean name 'userController' to bean named 'userDao'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:478) - Finished creating instance of bean 'userController'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'userDao'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'userService'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0'
2017-01-04 17:06:26,867 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'mongo'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.CustomEditorConfigurer#0'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.CustomEditorConfigurer#1'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.CustomEditorConfigurer#2'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.CustomEditorConfigurer#3'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'org.springframework.beans.factory.config.CustomEditorConfigurer#4'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'mongoDbFactory'
2017-01-04 17:06:26,868 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:248) - Returning cached instance of singleton bean 'mongoTemplate'

spring配置文件:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">

<!-- 自动扫描(自动注入) -->


/context:component-scan

<import resource="config/mongo/spring-mongo.xml" />

求解~~~~


  • 写回答

3条回答 默认 最新

  • devmiao 2017-01-04 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向