gpt4 book ai didi

java - 如何从 xml @Autowired(注入(inject))。无法 Autowiring 。未找到 'DAO' 类型的 beans

转载 作者:行者123 更新时间:2023-12-02 09:21:20 24 4
gpt4 key购买 nike

我的 beans.xml

<bean id = "DAO" class = "com.price.compare.service.DAO" scope="singleton" init-method="init">
<constructor-arg index="0" type = "java.lang.String" value="localhost"/>
<constructor-arg index="1" type = "java.lang.String" value="5432"/>
..
</bean>

Java Controller

@Controller
@RequestMapping(value = "/addproduct")
public class NewProductController {
@Autowired
private DAO database;
...

DAO 类

public class DAO {
private final String host;
private final String port;
private final String user;
private final String password;

private Connection connection;

public DAO(String host, String port, String user, String password) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
}


public void init() {
connection = connect();
if(testSelection()) {
System.out.println("Connection to db successfully established");
}
}

@Autowired私有(private) DAO 数据库; <--- 给出消息“无法 Autowiring 。未找到‘DAO’类型的 beans。”

最佳答案

你应该添加 <context:component-scan base-package="packageName"/>进入 beans.xml其中packageName是DAO的包

关于java - 如何从 xml @Autowired(注入(inject))。无法 Autowiring 。未找到 'DAO' 类型的 beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com