建了个小项目测试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" />
求解~~~~