Reputation: 3261
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 UserController which is like 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 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
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)}
Upvotes: 4
Views: 28227
Reputation: 3261
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
@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 {
}
Upvotes: 1
Reputation: 7995
It seems that it has not found yur DAO object. I suggest annotating the DAO with @Service
annotation like this:
@Service("dao")
public class DAO {
}
and then injecting it into the class where you are using it with @Autowired
annotation:
@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.
Upvotes: 3