自动装配:预计至少有一个bean可以作为此依赖关系的autowire候选者 [英] Autowiring :expected at least 1 bean which qualifies as autowire candidate for this dependency

查看:74
本文介绍了自动装配:预计至少有一个bean可以作为此依赖关系的autowire候选者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道围绕同一主题提出的问题很多。但我似乎无法做任何事情。也可能是我还没有完全理解汽车接线概念。
我的问题:
我能够到达所需的页面,但每当我点击任意按钮执行动作时,我得到Null指针异常,这似乎很明显,因为我不认为spring能够正常映射所需的bean。

Okay, I know that there are many questions asked around the same topic. But I cant seem to make anything work. It also might be the case that I am yet to completely understand the auto wiring concept. My Problem: I am able to get to the required page, but whenever I click on the any button to perform an action I get Null pointer exception which seems obvious as I dont think spring is able to properly map the bean needed.

因此,当我添加@ autowired = true时,它会给我上面给出的异常。
我不确定需要做什么。希望有人可以帮我解决这个问题。也会喜欢一个解释:)
代码:

So, when I add @autowired=true , it gives me the above given exceptionn. I am not sure what needs to be done.. Hope someone can help me out with this. Would love an explanation as well:) Code:

@Entity
@Table(name="userDetails")
public class UserDetailModel {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int user_id;
public String password;
public String user_name;
public String active_status;

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUser_name() {
    return user_name;
}

public void setUser_name(String user_name) {
    this.user_name = user_name;
}

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public String getActive_status() {
    return active_status;
}

public void setActive_status(String active_status) {
    this.active_status = active_status;
}

}

控制器:

@RestController
public class UserDetailController {

 private Logger logger = (Logger) LoggerFactory.getLogger(UserDetailController.class);
 @Autowired(required = true)
private UserRepository userRepository;

@RequestMapping(value="/login", method = RequestMethod.POST)
public @ResponseBody String addNewUser (@RequestBody UserDetailModel user) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    logger.debug("in controller");
    UserDetailModel userDtl = new UserDetailModel();
    userDtl.setUser_id(user.user_id);
    userDtl.setUser_name(user.user_name);
    userDtl.setActive_status(user.active_status);
    userDtl.setPassword(user.password);

    userRepository.save(userDtl);
    return "Saved";
}

}

存储库:

@Repository
public interface UserRepository extends CrudRepository<UserDetailModel,         Long> {}

堆栈追踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springBoot.usl.repo.UserRepository com.springBoot.usl.controller.UserDetailController.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springBoot.usl.repo.UserRepository] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at com.springBoot.usl.controller.WebAppInitializer.main(WebAppInitializer.java:18)

根据回复解决
答案:
我根据Jay和Luay的答案做了一些修改。并在我的ApplicationConfig文件中更改了注释:

Solved based on the responses Answer: I made some modifications based on Jay and Luay's answers. And changed the annotations as follows in my ApplicationConfig file:

@Configuration
@ComponentScan("my.basepackage.*")
@EnableJpaRepositories(basePackages = {"my.basepackage.*"})
@EntityScan("my.basepackage.*")
@EnableAutoConfiguration

希望这对某人有所帮助。

Hope this helps some one.

但我不确定*是否正确的方法。

But I am not sure if * is the right way to go.

推荐答案

我可以在注释方面进行一些更改来运行你的应用程序。

I am able to run your application with some changes on annotation side.

我使用了相同的课程。请参阅下面使用的结构和配置。
目录结构

I have used same classes which are given in question. Please see below structure and configuration used. Directory Structure

我使用了以下软件包并添加了您的课程,

com.rcmutha.usl.controller

com.rcmutha.usl .repository

I have used packages as below and added your classes,
com.rcmutha.usl.controller
com.rcmutha.usl.repository

@SpringBootApplication
@ComponentScan({"com.rcmutha*"})
@EntityScan("com.rcmutha*")
@EnableJpaRepositories("com.rcmutha*")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

完整代码:
点击此处查看代码

这篇关于自动装配:预计至少有一个bean可以作为此依赖关系的autowire候选者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆