我在Tomcat上使用Spring MVC。当我尝试启动tomcat时,我得到了这个例外:
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.path()[Ljava/lang/String;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:539)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1034)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4913)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5200)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.path()[Ljava/lang/String;
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.createRequestMappingInfo(RequestMappingHandlerMapping.java:257)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.createRequestMappingInfo(RequestMappingHandlerMapping.java:214)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getMappingForMethod(RequestMappingHandlerMapping.java:193)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getMappingForMethod(RequestMappingHandlerMapping.java:53)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$1.matches(AbstractHandlerMethodMapping.java:210)
at org.springframework.web.method.HandlerMethodSelector$1.doWith(HandlerMethodSelector.java:62)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)
at org.springframework.web.method.HandlerMethodSelector.selectMethods(HandlerMethodSelector.java:57)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:207)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:182)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:163)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:133)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 25 more
SRC:
控制器:
@Controller
public class IndexController {
private final Logger logger = LoggerFactory.getLogger(IndexController.class);
private final TaskStorage storage;
@Autowired
public IndexController(TaskStorage storage) {
this.storage = storage;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Map<String, Object> model) {
model.put("tasks", storage.getAllTasks());
return "index";
}
...
服务:
@Service
public class TaskStorage {
public TaskStorage() {
}
public void gitCreateBranch(String key, String branchName) {
ServiceManager.get().git.createBranch(branchName);
}
...
服务经理:
public class ServiceManager {
private static volatile ServiceManager instance;
public final JiraService jira;
public final GitService git;
public final JenkinsService jenkins;
@Autowired
private ServiceConfiguration conf;
private ServiceManager() {
this.jira = new JiraServiceSynchronousImpl(conf);
this.git = new JGitGitServiceImpl(conf);
this.jenkins = new SimpleJenkinsServiceImpl(conf);
}
public static ServiceManager get() {
if (instance == null) {
synchronized (ServiceManager.class) {
if (instance == null) {
instance = new ServiceManager();
}
}
}
return instance;
}
服务配置:
package com.etnetera.dev.storage;
@Component
public class ServiceConfiguration {
public final JiraConfiguration jira;
public final GitConfiguration git;
public final JenkinsConfiguration jenkins;
public final JnpConfiguration jnp;
public ServiceConfiguration() {
this(null, null, null, null);
}
@Autowired
public ServiceConfiguration(JiraConfiguration jira, GitConfiguration git,
JenkinsConfiguration jenkins, JnpConfiguration jnp) {
super();
this.jira = jira;
this.git = git;
this.jenkins = jenkins;
this.jnp = jnp;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
return new PropertySourcesPlaceholderConfigurer();
}
@Component
public static class JenkinsConfiguration {
public final String url;
public final String defaultJobName;
public final String defaultJobNameParam;
public final String userName;
public final String password;
public JenkinsConfiguration() {
this(null, null, null, null, null);
}
@Autowired
public JenkinsConfiguration(
@Value("${jenkins.url}") String url,
@Value("${jenkins.defaultJobName}") String defaultJobName,
@Value("${jenkins.defaultJobNameParam}") String defaultJobNameParam,
@Value("${jenkins.userName}") String userName,
@Value("${jenkins.password}") String password) {
super();
this.url = url;
this.defaultJobName = defaultJobName;
this.defaultJobNameParam = defaultJobNameParam;
this.userName = userName;
this.password = password;
}
public String getJobUrl(String jobName) {
return getJobUrl(jobName, null);
}
}
@Component
public static class GitConfiguration {
public final Git git;
public final String localRepoPath;
public GitConfiguration() throws IOException {
this(null);
}
@Autowired
public GitConfiguration(
@Value("${git.localRepoPath}") String localRepoPath)
throws IOException {
this.git = new Git(new FileRepository(localRepoPath + "/.git"));
this.localRepoPath = localRepoPath;
}
}
@Component
public static class JiraConfiguration {
public final JiraRestClient client;
public final SearchRestClient searchClient;
public final IssueRestClient issueClient;
public final String jiraUser;
public JiraConfiguration() throws URISyntaxException {
this(null, null, null);
}
@Autowired
public JiraConfiguration(@Value("${jira.userName}") String userName,
@Value("${jira.pass}") String pass,
@Value("${jira.url}") String url) throws URISyntaxException {
this.jiraUser = userName;
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI uri = new URI(url);
this.client = factory.createWithBasicHttpAuthentication(uri,
userName, pass);
this.searchClient = client.getSearchClient();
this.issueClient = client.getIssueClient();
}
}
@Component
public static class JnpConfiguration {
public final Map<String, String> serviceUrls;
public final String defaultServiceUrl;
public final String defaultProjectName;
public JnpConfiguration() {
this(null, null);
}
@Autowired
public JnpConfiguration(
@Value("${jnp.defaultServer}") String defaultServiceUrl,
@Value("${jnp.defaultProjectName}") String defaultProjectName) {
super();
this.serviceUrls = null;
this.defaultServiceUrl = defaultServiceUrl;
this.defaultProjectName = defaultProjectName;
}
}
}
网络配置:
package com.etnetera.dev.config;
@EnableWebMvc
@Configuration
@ComponentScan({ "com.etnetera.dev.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Root config:
package com.etnetera.dev.config;
@Configuration
@ComponentScan({ "com.etnetera.dev.storage" })
@PropertySource("classpath:service.config.properties")
public class SpringRootConfig {
}
属性文件位于
/ SRC /主/资源
Gradle脚本:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
//apply plugin: 'jetty'
apply plugin: 'org.akhikhl.gretty'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:+'
}
}
gretty {
port = 8081
contextPath = 'spring4'
servletContainer = 'jetty9'
}
// JDK 7
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
maven { url = 'https://maven.atlassian.com/content/repositories/atlassian-public/'}
mavenLocal()
mavenCentral()
}
dependencies {
compile(
['org.eclipse.jgit:org.eclipse.jgit:4.0.1.201506240215-r'],
['com.atlassian.jira:jira-rest-java-client:2.0.0-m2'],
['javax.servlet:jstl:1.2'],
['org.springframework:spring-webmvc:4.2.1.RELEASE'],
['org.springframework:spring-aspects:4.2.1.RELEASE'],
['ch.qos.logback:logback-classic:1.1.3'])
//include in compile only, exclude in the war
providedCompile 'javax.servlet:servlet-api:2.5'
}
//For Eclipse IDE only
eclipse {
wtp {
component {
//define context path, default to project folder name
contextPath = 'spring4'
}
}
}
我已经在Eclipse项目和Web App Libraries中链接了Gradle依赖项,这似乎是相同的。两者都在版本4.2.1.RELEASE
中有SpringTomcat8 / lib中没有Spring库。
在Eclipse中,Tomcat已经在版本4.2.1.RELEASE中链接了Spring库(在Tomcat 8概述 - &gt;启动配置 - &gt;源代码下找到)
我是Spring的新手,我已经尝试了谷歌搜索解决方案几个小时没有运气。谢谢你的帮助。
答案 0 :(得分:2)
我已经通过清理项目,清理Tomcat和清理Tomcat Work目录来解决了这个问题。
无论如何,我的代码还有另一个问题:
public class ServiceManager {
private static volatile ServiceManager instance;
public final JiraService jira;
public final GitService git;
public final JenkinsService jenkins;
@Autowired
private ServiceConfiguration conf;
private ServiceManager() {
this.jira = new JiraServiceSynchronousImpl(conf);
this.git = new JGitGitServiceImpl(conf);
this.jenkins = new SimpleJenkinsServiceImpl(conf);
}
这是错误的,因为Spring没有自动装配字段在构造函数中使用它。我修好了这个:
public class ServiceManager {
private static volatile ServiceManager instance;
public final JiraService jira;
public final GitService git;
public final JenkinsService jenkins;
@Autowired
private ServiceManager(ServiceConfiguration conf) {
this.jira = new JiraServiceSynchronousImpl(conf);
this.git = new JGitGitServiceImpl(conf);
this.jenkins = new SimpleJenkinsServiceImpl(conf);
}
答案 1 :(得分:1)
我非常怀疑您拥有不同版本的依赖项spring-web
和spring-webmvc
。检查您的pom.xml并确定您使用的是非常相同版本的所有spring依赖项,然后重试
答案 2 :(得分:1)
在堆栈跟踪中查看此部分:
Caused by: java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.path()[Ljava/lang/String;
确保您的pom.xml
中没有包含不同版本Spring的多个jar文件。