Spring MVC configuration problems

Posted by Smek on Stack Overflow See other posts from Stack Overflow or by Smek
Published on 2013-11-04T08:21:13Z Indexed on 2013/11/04 21:54 UTC
Read the original article Hit count: 374

Filed under:
|
|
|

i have some problems with configuring Spring MVC. I made a maven multi module project with the following modules:

/api
/domain
/repositories
/webapp

I like to share the domain and the repositories between the api and the webapp (both web projects). First i want to configure the webapp to use the repositories module so i added the dependencies in the xml file like this:

<dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>domain</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>repositories</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

And my controller in the webapp module looks like this:

package com.mywebapp.webapp;
import com.mywebapp.domain.Person;
import com.mywebapp.repositories.services.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
@Configuration
@ComponentScan("com.mywebapp.repositories")
public class PersonController {

@Autowired
PersonService personservice;

@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
    Person p = new Person();
    p.age = 23;
    p.firstName = "John";
    p.lastName = "Doe";

    personservice.createNewPerson(p);

    model.addAttribute("message", "Hello world!");
    return "index";
}
}

In my webapp module i try to load configuration files in my web.xml like this:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/META-INF/persistence-context.xml, classpath:/META-INF/service-context.xml</param-value>
</context-param>

These files cannot be found so i get the following error:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/persistence-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/persistence-context.xml] cannot be opened because it does not exist

These files are in the repositories module so my first question is how can i make Spring to find these files?

I also have trouble Autowiring the PersonService to my Controller class did i forget to configure something in my XML?

Here is the error message:

[INFO] [talledLocalContainer] SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener [INFO] [talledLocalContainer] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mywebapp.repositories.repository.PersonRepository com.mywebapp.repositories.services.PersonServiceImpl.personRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mywebapp.repositories.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

PersonServiceImple.java:

    package com.mywebapp.repositories.services;
import com.mywebapp.domain.Person;
import com.mywebapp.repositories.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements PersonService{

@Autowired
public PersonRepository personRepository;

@Autowired
public MongoTemplate personTemplate;

@Override
public Person createNewPerson(Person person) {
    return personRepository.save(person);
}
}

PersonService.java

package com.mywebapp.repositories.services;

import com.mywebapp.domain.Person;

public interface PersonService {

Person createNewPerson(Person person);

}

PersonRepository.java:

package com.mywebapp.repositories.repository;

import com.mywebapp.domain.Person;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.math.BigInteger;

@Repository
public interface PersonRepository extends MongoRepository<Person, BigInteger> {
}

persistance-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/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<context:property-placeholder location="classpath:mongo.properties"/>

<mongo:mongo host="${mongo.host}" port="${mongo.port}" id="mongo">
    <mongo:options
            connections-per-host="${mongo.connectionsPerHost}"
            threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
            connect-timeout="${mongo.connectTimeout}"
            max-wait-time="${mongo.maxWaitTime}"
            auto-connect-retry="${mongo.autoConnectRetry}"
            socket-keep-alive="${mongo.socketKeepAlive}"
            socket-timeout="${mongo.socketTimeout}"
            slave-ok="${mongo.slaveOk}"
            write-number="1"
            write-timeout="0"
            write-fsync="true"/>
</mongo:mongo>

<mongo:db-factory dbname="person" mongo-ref="mongo" id="mongoDbFactory"/>

<bean id="personTemplate" name="personTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

<mongo:repositories base-package="com.mywebapp.repositories.repository" mongo-template-ref="personTemplate">
    <mongo:repository id="personRepository" repository-impl-postfix="PersonRepository"  mongo-template-ref="personTemplate" create-query-indexes="true"/>
</mongo:repositories>

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about spring