Question

I am writing a standalone java program that call spring data repository for manging enities. I am using mongo db for persistence. I am following stackoverflow posts and some projects from github but when I run my program it fails since the repository is null. I am not expert in spring so it would be helpful if someone could show me the issue with the posted program.

Application-context.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/data/mongo 
    http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">


<mongo:mongo id="mongo" host="monopolyvm3" port="27017" />
<mongo:db-factory dbname="test" mongo-ref="mongo" />
<mongo:db-factory id="mongoDbFactory" dbname="cloud"
    mongo-ref="mongo" />


<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

<bean id="defaultMongoTypeMapper"
    class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
</bean>

<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mappingContext" />
    <property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    <property name="writeConcern" value="SAFE" />
</bean>
<context:component-scan base-package="com.xxxx"></context:component-scan>

<mongo:repositories base-package="com.xxxx.yyyy" />

Repository

public interface SlpRepository extends MongoRepository<Slp, Long> {

}

main program

public class App {

    @Autowired
    static
    SlpRepository slpRepository;
    public static void main(String[] args) {
        ApplicationContext ctx = new GenericXmlApplicationContext("application-context.xml");
        Slp slp = new Slp();
        slp.setClientCount(100000L);
        slp.setPolicyName("testing");
        slp.setSlpName("slp_testing");
        slpRepository.save(slp);

    }


}

Object to store
------------------

@Document(collection="slps")
public class Slp implements Serializable {


    private Long slpId;
    private String slpName;
    private String policyName;
    private Long clientCount;
}
.....all getters and setters

JFYI..I tried to save object using mongotemplate and it works well.

Was it helpful?

Solution

The problem in this example is that you create the context in your App class but never get a handle to the repository from the application context. You cannot auto wire into the same class that holds the application context. What you can try is the get the instance of the repository from the context (i.e ctx.getBean(SlpRepository.class)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top