我在配置Spring MVC时遇到了一些问题。我用以下模块制作了一个maven多模块项目:
/api
/domain
/repositories
/webapp
我喜欢在api和webapp(两个Web项目)之间共享域和存储库。首先,我想配置webapp以使用存储库模块,所以我在xml文件中添加了依赖项,如下所示:
<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>
我在webapp模块中的控制器如下所示:
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";
}
}
在我的webapp模块中,我尝试在我的web.xml中加载配置文件,如下所示:
<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>
无法找到这些文件,因此出现以下错误:
org.springframework.beans.factory.BeanDefinitionStoreException:从类路径资源[META-INF / persistence-context.xml]解析XML文档的IOException;嵌套异常是java.io.FileNotFoundException:类路径资源[META-INF / persistence-context.xml]无法打开,因为它不存在
这些文件位于存储库模块中,所以我的第一个问题是如何让Spring找到这些文件?
我也无法将PersonService自动装入我的Controller类,但是忘记在我的XML中配置某些内容了吗?
以下是错误消息:
[INFO] [talledLocalContainer] SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常 [INFO] [talledLocalContainer] org.springframework.beans.factory.BeanCreationException:创建名为'personServiceImpl'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.mywebapp.repositories.repository.PersonRepository com.mywebapp.repositories.services.PersonServiceImpl.personRepository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.mywebapp.repositories.repository.PersonRepository]的匹配bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ 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> {
}
持久性-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>
由于