2 fallencity2011 fallencity2011 于 2017.01.04 17:18 提问

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
devmiao   Ds   Rxr 2017.01.04 23:53
xiaomingtiande
xiaomingtiande   2017.01.05 09:40

扫描的路径有没有写错?

JE_GE
JE_GE   2017.01.07 16:17

注入为空,就是单词大小写,路径这些的问题

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
Spring通过注解得到的值为null,但getbean()可以得到。
可以加如struts2-spring-plugin-2.3.16.1.jar解决,不同版本对应不同的jar
SPRING中BEAN注入成功,可是启动WEB服务器,调用方法时,BEAN全是空,NullpointerException
首先给大家展示一下问题: http://blog.csdn.net/chiweitree/article/details/8162274 项目是SPRING和IBATIS整合的,项目的目的是当TOMCAT服务器启动时,就去调用SERVLET里面的INIT()方法,而这个INIT()方法里面调用我自己写方法,这个自定义方法所在的类就是靠SPRING来注入的,实例化的。
Spring bean注入为null
最近在学习SSM框架的过程中, 总是遇到一个问题, @Autowired注解自动装配bean总是失败, 所注解的变量值总是为null, 后来通过网上查资料,发现原因如下: 使用了new实例化对象, 而new实例化出来的对象不是由spring bean容器进行管理, 从而导致spring 在自动扫描@Autowired注解时无法注入. 从另一个方面说, spring实现@Autowired注解自动
@Autowired注解getBean为null
直接上代码: debug发现@Autowired的定义的变量都是为null,再看看这个service是否加了注解: 结果发现这个service加了@Service这个注解,那么问题出现在哪呢? 查找进入这个service的入口发现: 使用这个service的时候是new出来的对象,而不是使用@Autowired来获取的,所以导致这个service不能获取spring
spring aop 注解打印log日志
首先要在springmvc的配置里面配置:                     然后写一个LogAnnotationAspect类: package com.ssz.aspectj; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.l
Spring已经添加属性注入了,但是还是报空指针错误 说明
public class DocumentPressUCCImpl  extends TaskUCCImpl implements IDocumentPressUCC 中,已经继承了父类 TaskUCCImpl 配置文件中:   class="a.DocumentPressUCCImpl"  parent="taskUCC" >添加    parent="taskUCC"属性。 
@Resource注入的bean为空浅析
spring在依赖IOC编程方面,提供了两种方式注入Bean,XML配置和使用注解+自动扫描package的方式 1.@Resource应用在字段上,则注入规则是: a.先使用字段名字匹配bean,查找到bean则注入,如果类型不匹配则此时有异常,注入失败 b.如果字段名字没有匹配到Bean则spring会尝试采用字段类型匹配,如果找打bean则注入,如果字段类型是接口则有可能会匹配到多个类
关于spring依赖注入到普通类中为null的情况解决理解方案
最近写了一个读取日志文件并定时保存的功能,但通过spring的依赖注入一直存在注入的实体类为null 的情况,网上查了很多的方法,有说有配置文件中包扫描的问题, 有的说是注解方式(增加@Component注解)的问题,但查了相应的方法都没有解决我的问题。无意中查到一位大神的方法,但还没有弄清楚原理,在这里先记录下来,方便以后的学习并加上自己的理解, 并希望浏览过的大神会给出一些建议和原理思路:
spring给Bean属性注入null值
用于处理null值。Spring会把属性的空参数当作空字符串处理。以下的xml片断将email属性设为空字符串。 这等同于Java代码: exampleBean.setEmail("")。而null值则可以使用元素可用来表示。例如: 上述的配置等同于Java代码:exampleBean.setEmail(null)。
hessian 在spring中的使用 (bean 如 Dao无法注入的问题)
spring 整合 hessian hessain接口实现类无法通过注解注入spring bean