spring 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31185679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:40:01  来源:igfitidea点击:

expected at least 1 bean which qualifies as autowire candidate for this dependency

springspring-boot

提问by Aman

I am very new to spring and trying to developing one application in spring boot.I know this is duplicate question but I didnt find any solution to my problem.. I have a class called UserControllerwhich is like below

我对 spring 非常陌生,并试图在 spring boot 中开发一个应用程序。我知道这是重复的问题,但我没有找到任何解决我的问题的方法.. 我有一个名为UserController的类 ,如下所示

    @RestController
    public class UserController {

        private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
        private final UserService userService;

        DatabaseConnections dataconnections = new DatabaseConnections(); 
       @Autowired
       private DAO dao;      
        @Inject
        public UserController(final UserService userService) {
            this.userService = userService;  
        }

        @RequestMapping(value = "/user", method = RequestMethod.POST)
        public User createUser(@RequestBody @Valid final User user) {
            LOGGER.debug("Received request to create the {}", user);
            return userService.save(user);
        }
       @RequestMapping(value = "/getuser/{id}", method = RequestMethod.GET)
       public JSONObject getUser(@PathVariable String id) {

    return dao.getUsers(id);
    }
    }

I have one more class which is having some function :

我还有一门具有某些功能的课程:

@Service("dao")
    public class DAO {    
      public JSONObject getUsers(@PathVariable String id) {
                Connection dbConnection = null;
                Statement statement = null;
                JSONObject userJSONObject = new JSONObject();
                String selectusers = "SELECT* from emp;
                try {
                    dbConnection = dataconnections.getPostgresConnection(hostname, port, dbname, username, password);
                    statement = dbConnection.createStatement();
                    ResultSet rs = statement.executeQuery(selectusers);
                    while (rs.next()) {
                           --
                        ---
                    }
                    return userJSONObject;
    } 

I want to use the getUsers function in usercontroler class

我想在 usercontroler 类中使用 getUsers 函数

I am getting error below when I try to do by this way.

当我尝试通过这种方式进行操作时,出现以下错误。

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies  
   failed; nested exception is   
org.springframework.beans.factory.BeanCreationException: Could not autowire  
 field: private com.emc.bdma.itaudemo.postgres.dao.DAO   
com.emc.bdma.itaudemo.restclient.controller.UserController.dao; nested   
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:   
No qualifying bean of type [com.emc.bdma.itaudemo.postgres.dao.DAO] 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)} 

采纳答案by Aman

I just solved the issue...

我刚刚解决了这个问题...

@RestController
    public class UserController {

        private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
        private final UserService userService;
   @Autowired
        DatabaseConnections dataconnections 
       @Autowired
       private DAO dao;      
        @Inject
        public UserController(final UserService userService) {
            this.userService = userService;  
        }

        @RequestMapping(value = "/user", method = RequestMethod.POST)
        public User createUser(@RequestBody @Valid final User user) {
            LOGGER.debug("Received request to create the {}", user);
            return userService.save(user);
        }
       @RequestMapping(value = "/getuser/{id}", method = RequestMethod.GET)
       public JSONObject getUser(@PathVariable String id) {

    return dao.getUsers(id);
    }
    }  

There is a databaseConnection class is instantiating...We have to mark the class as @Component

有一个 databaseConnection 类正在实例化...我们必须将该类标记为 @Component

@Component
public class DatabaseConnections {

  public Connection getPostgresConnection(String hostname, String port, String dbname, String username, String password)

And also declare DAo as @Componentor service

并且还声明 DAo 作为@Component或服务

@Service("dao")
@Component
public class DAO {
}

回答by Smajl

It seems that it has not found yur DAO object. I suggest annotating the DAO with @Serviceannotation like this:

它似乎还没有找到你的 DAO 对象。我建议用@Service这样的注释来注释 DAO :

@Service("dao")
public class DAO {
}

and then injecting it into the class where you are using it with @Autowiredannotation:

然后将其注入到您正在使用它的类中进行@Autowired注释:

@Autowire
private DAO dao;

Moreover, you can also autowire an interface in a similar way and then specify which implementation will be used if there are more than one.

此外,您还可以以类似的方式自动装配一个接口,然后指定如果有多个实现将使用哪个实现。

If this is not the case, post the full code of the class calling the dao function, so we can see the whole picture.

如果不是这种情况,把调用dao函数的类的完整代码贴出来,大家可以看看全貌。