gpt4 book ai didi

Java SpringMVC Autowiring BeanCreationException

转载 作者:行者123 更新时间:2023-11-28 22:06:15 25 4
gpt4 key购买 nike

我在 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

源代码:

Controller :

@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;
}

}

根配置:

package com.etnetera.dev.config;

@Configuration
@ComponentScan({ "com.etnetera.dev.storage" })
@PropertySource("classpath:service.config.properties")
public class SpringRootConfig {


}

属性文件位于

/src/main/资源

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 应用程序库中链接了 Gradle 依赖项,这似乎是相同的。两者都有 4.2.1.RELEASE 版本的 Spring

Tomcat8/lib 中没有Spring 库

在 Eclipse 中,Tomcat 在版本 4.2.1.RELEASE 中链接了 Spring 库(在 Tomcat 8 概述 -> 启动配置 -> 源代码下找到)

我是 Spring 的新手,我已经尝试谷歌搜索解决方案几个小时但没有成功。感谢您的帮助。

最佳答案

我已经通过清理项目、清理 Tomcat 和清理 Tomcat 工作目录解决了这个问题。

无论如何,我的代码还有另一个问题:

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 不会 Autowiring 字段以在构造函数中使用它。我用这个修复了它:

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);
}

关于Java SpringMVC Autowiring BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com