夏天的小虫 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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献