Spring Annotation : org.springframework.beans.factory.BeanCr



我是springMVC注释的新手,但我得到了这个异常

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.websystique.springmvc.converter.RoleToUserProfileConverter com.websystique.springmvc.configuration.AppConfig.roleToUserProfileConverter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleToUserProfileConverter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.websystique.springmvc.service.UserProfileService com.websystique.springmvc.converter.RoleToUserProfileConverter.userProfileService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.websystique.springmvc.service.UserProfileService] 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)}

AppConfig

package com.websystique.springmvc.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import 
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import 
org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import 
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import com.websystique.springmvc.converter.RoleToUserProfileConverter;

@Configuration
@Component
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc.converter")
public class AppConfig extends WebMvcConfigurerAdapter{

@Autowired
RoleToUserProfileConverter roleToUserProfileConverter;

/**
* Configure ViewResolvers to deliver preferred views.
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
registry.viewResolver(viewResolver);
}
/**
* Configure ResourceHandlers to serve static resources like CSS/ Javascript etc...
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
/**
* Configure Converter to be used.
* In our example, we need a converter to convert string values[Roles] to UserProfiles in newUser.jsp
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(roleToUserProfileConverter);
}

/**
* Configure MessageSource to lookup any validation/error message in internationalized property files
*/
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
/**Optional. It's only required when handling '.' in @PathVariables which otherwise ignore everything after last '.' in @PathVaidables argument.
* It's a known bug in Spring [https://jira.spring.io/browse/SPR-6164], still present in Spring 4.1.7.
* This is a workaround for this issue.
*/
@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
matcher.setUseRegisteredSuffixPatternMatch(true);
}
}

RoleToUserProfileConverter

package com.websystique.springmvc.converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.websystique.springmvc.model.UserProfile;
import com.websystique.springmvc.service.UserProfileService;
**
* A converter class used in views to map id's to actual userProfile objects.
*/
Component
public class RoleToUserProfileConverter implements Converter<Object, UserProfile>{
static final Logger logger = LoggerFactory.getLogger(RoleToUserProfileConverter.class);
@Autowired
UserProfileService userProfileService;
/**
* Gets UserProfile by Id
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
public UserProfile convert(Object element) {
Integer id = Integer.parseInt((String)element);
UserProfile profile= userProfileService.findById(id);
logger.info("Profile : {}",profile);
return profile;
}

我用这个链接尝试了这个网站上给出的许多解决方案还有这个

我已经没有什么想法了,有什么可以帮助的吗?这只是一个基本的web应用程序,我仍然在努力解决这个问题,我已经在组件扫描中添加了确切的包名称,我仍然面临这个问题。

异常指示自动连接嵌套依赖项失败的原因。

No qualifying bean of type [com.websystique.springmvc.service.UserProfileService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.

Spring找不到com.websystique.springmvc.service.UserProfileService类型的bean

您需要通过@Import或通过@ComponentScan添加类型为com.websystique.springmvc.service.UserProfileService的组件。

一个这样的解决方案:

假设组件在包com.websystique.springmvc.service中,您可以按如下方式更改@ComponentScan

例如

...
@ComponentScan({"com.websystique.springmvc.converter", "com.websystique.springmvc.service"})
public class AppConfig extends WebMvcConfigurerAdapter{
...
}

根据UserProfileServicebean所在的位置相应地更改包名称。

相关内容

最新更新