我在配置 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:IOException 从类路径资源解析 XML 文档 [META-INF/persistence-context.xml];嵌套异常是 java.io.FileNotFoundException:类路径资源 [META-INF/persistence-context.xml] 无法打开,因为它不存在
这些文件位于存储库模块中,所以我的第一个问题是如何让 Spring 找到这些文件?
我也有麻烦将 PersonService 自动装配到我的 Controller 类我忘记在我的 XML 中配置一些东西了吗?
这是错误消息:
[INFO] [talledLocalContainer] 严重:向 org.springframework.web.context.ContextLoaderListener 类的侦听器实例发送上下文初始化事件的异常 [INFO] [talledLocalContainer] org.springframework.beans.factory.BeanCreationException:创建名为“personServiceImpl”的 bean 时出错': 自动装配依赖注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.mywebapp.repositories.repository.PersonRepository com.mywebapp.repositories.services.PersonServiceImpl.personRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到类型 [com.mywebapp.repositories.repository.PersonRepository] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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);
}
}
人事服务.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> {
}
持久性上下文.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>
谢谢