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

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境