如何从 xml @Autowired(注入)。无法自动连线。未找到"DAO"类型的豆子



My 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
@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");
}
}

@自动连线私有DAO数据库<---给出消息"无法自动连线。找不到'DAO'类型的bean。">

您应该添加<context:component-scan base-package="packageName"/>转换为beans.xml其中packageName是DAO 的包

相关内容

最新更新