At least 1 bean expected that qualifies as an outsourced candidate for this dependency

I am very new to spring and am trying to develop one application in spring boot. I know this is a duplicate question, but I have not found a solution for my problem. I have a class called UserController which is below

    @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 another class that has 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 getUsers function in usercontroler class

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

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

      

+3


source to share


2 answers


I just solved the problem ...

@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 instantiating ... We have to mark the class as @Component



@Component
public class DatabaseConnections {

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

      

And also declare DAo as @Component

or service

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

      

+1


source


It doesn't seem to find the DAO object. I suggest annotating DAO with annotation @Service

like this:

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

      

and then injecting it into the class where you use it with annotation @Autowired

:



@Autowire
private DAO dao;

      

Alternatively, you can also auto-repeat an interface in a similar way and then specify which implementation to use if there are more.

If not, write the complete code for the class that calls the dao function so we can see the whole picture.

+3


source







All Articles