在MongoTemplate上获取NullPointerException

时间:2015-05-17 03:45:13

标签: spring mongodb mlab

我使用的是Spring + jersey + mongodb(在mongolab上)+ maven

Web服务部分工作正常但是当我尝试连接到数据库时,我在MongoTemplate上得到一个空指针异常我尝试了很多解决方案但没有工作

这是我的代码:

的applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">


    <context:component-scan base-package="com.jersey" />

    <!-- Factory bean that creates the Mongo instance -->
    <mongo:mongo host="ds031972.mongolab.com" port="31972" id="mongo" />
    <mongo:db-factory dbname="proyecto_desarrollo" username="danprat" password="19693058" mongo-ref="mongo" id="mongoDbFactory" />

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
</beans>

这里使用mongoTemplate的dao

public class UserManager {

    public static final String COLLECTION_NAME = "user";

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<User> getUsers() {
        return mongoTemplate.findAll(User.class , COLLECTION_NAME);
    }
}

以防万一这是服务文件

@Path("/users")
public class UserService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getUsers(){

        UserManager userManager = new UserManager();

        return userManager.getUsers();
    }
}

连接uri由mongolab提供:

mongodb://<dbuser>:<dbpassword>@ds031972.mongolab.com:31972/proyecto_desarrollo

我相信MongoTemplate没有正确注射,但不知道为什么我是春天和mongo的新手

修改

这是有效的,这要归功于给出的答案:

@Component
public class UserManager {

    public static final String COLLECTION_NAME = "user";

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<User> getUsers() {
        return mongoTemplate.findAll(User.class , COLLECTION_NAME);
    }
}


@Path("/users")
@Component
public class UserService {

    @Autowired
    UserManager userManager;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getUsers(){


        return userManager.getUsers();
    }
}

的xml:

<context:component-scan base-package="com.jersey.persistence" />
<context:component-scan base-package="com.jersey.services" />
<context:component-scan base-package="com.jersey.commons.entities" />
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo host="ds031972.mongolab.com" port="31972" id="mongo" />
<mongo:db-factory dbname="proyecto_desarrollo" username="danprat" password="19693058" mongo-ref="mongo" id="mongoDbFactory" />

<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

1 个答案:

答案 0 :(得分:0)

我认为您没有设置应用程序上下文来正确扫描像UserManager这样的类。 UserManager似乎没有使用必要的@Component注释进行注释。

另外,您可能没有扫描自己的包装进行注射。我明白了

<context:component-scan base-package="com.jersey" />

您需要添加UserManager所在的包以及您需要扫描的任何其他内容。您可以通过附加以逗号分隔的其他基类来完成此操作。