Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建Springframework JdbcTemplate的扩展类时抛出bean错误_Java_Spring - Fatal编程技术网

Java 创建Springframework JdbcTemplate的扩展类时抛出bean错误

Java 创建Springframework JdbcTemplate的扩展类时抛出bean错误,java,spring,Java,Spring,我正在尝试创建并扩展org.springframework.jdbc.core.JdbcTemplate类 public class CustomJdbcTemplate extends JdbcTemplate 我创建这个customJdbcTemplate是为了在执行查询时执行一些监视功能 现在我得到一个编译错误 *************************** APPLICATION FAILED TO START *************************** Desc

我正在尝试创建并扩展org.springframework.jdbc.core.JdbcTemplate类

public class CustomJdbcTemplate extends JdbcTemplate
我创建这个customJdbcTemplate是为了在执行查询时执行一些监视功能

现在我得到一个编译错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.myservice.repository.DataRepository required a bean of type 'com.myservice.repository.CustomJdbcTemplate' that could not be found.
Action:

    Consider defining a bean of type 'com.myservice.repository.CustomJdbcTemplate' in your configuration.

我在这里错过了什么?我应该在CustomJdbcTemplate类中添加注释吗?

正如错误消息所说,您需要定义CustomJdbcTemplate类型的bean。只有豆子可以注射到其他豆子中。仅仅向项目中添加一个类并不能定义bean。在类上使用
@Bean
-注释的方法,或classspath扫描和
@Component
注释,或XML定义。。。简而言之,像在其他类中定义bean一样,我添加了@Component(“JdbcTemplateWithMonitor”)。这会导致“通过构造函数参数1表示的未满足的依赖关系”;错误如果构造函数有参数,则需要使用Autowired对其进行注释,并且参数也必须是bean。或者您需要通过使用
@bean
-注释方法或使用XML定义bean来显式地传递它。这是标准的spring DI材料。阅读关于如何定义和注入bean的文档。正如错误消息所说,您需要定义CustomJdbcTemplate类型的bean。只有豆子可以注射到其他豆子中。仅仅向项目中添加一个类并不能定义bean。在类上使用
@Bean
-注释的方法,或classspath扫描和
@Component
注释,或XML定义。。。简而言之,像在其他类中定义bean一样,我添加了@Component(“JdbcTemplateWithMonitor”)。这会导致“通过构造函数参数1表示的未满足的依赖关系”;错误如果构造函数有参数,则需要使用Autowired对其进行注释,并且参数也必须是bean。或者您需要通过使用
@bean
-注释方法或使用XML定义bean来显式地传递它。这是标准的spring DI材料。阅读有关如何定义和注入bean的文档。