我很新兴春天并尝试在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);
}
}
我还有一个具有某些功能的课程:
@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;
}
我想在usercontroler类中使用getUsers函数
当我尝试这样做时,我收到错误。
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)}
答案 0 :(得分:3)
似乎没有找到你的DAO对象。我建议使用@Service
注释来注释DAO,如下所示:
@Service("dao")
public class DAO {
}
然后将其注入到使用@Autowired
注释的类中:
@Autowire
private DAO dao;
此外,您还可以以类似的方式自动装配接口,然后指定在有多个实现时将使用哪个实现。
如果不是这种情况,请发布调用dao函数的类的完整代码,以便我们可以看到整个图片。
答案 1 :(得分:1)
我刚刚解决了这个问题...
@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);
}
}
有一个databaseConnection类正在实例化......我们必须将该类标记为@Component
@Component
public class DatabaseConnections {
public Connection getPostgresConnection(String hostname, String port, String dbname, String username, String password)
并将DAo声明为@Component
或服务
@Service("dao")
@Component
public class DAO {
}