springboot:方法 addProduct 的参数 0 需要类型为 'java.lang.String' 的 bean,但找不到
springboot: Parameter 0 of method addProduct required a bean of type 'java.lang.String' that could not be found
我是 spring 引导新手,我想创建一个向数据库插入值的应用程序
我已经创建了服务、存储库、配置和控制器
但是我收到这个错误
Parameter 0 of method addProduct in com.oshabz.springboot.repositories.ProductRepository required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
我的代码
@Repository
public class ProductRepository {
@Autowired
JdbcTemplate jdbcTemplate;
@Bean
public void addProduct(String name) {
String sql = "INSERT INTO product VALUES (NULL, ?)";
jdbcTemplate.update(sql, name);
}
}
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public void addProduct(String name) {
productRepository.addProduct(name);
}
}
@RestController
@RequestMapping(path="product")
public class ProductController {
@Autowired
ProductService productService;
@PostMapping(path="/add/{name}")
public void addProduct(@PathVariable String name) {
productService.addProduct(name);
}
}
从 addProduct
方法中删除 @Bean
注释。该注释应该用于创建 bean 实例的方法的配置 类。
我是 spring 引导新手,我想创建一个向数据库插入值的应用程序
我已经创建了服务、存储库、配置和控制器
但是我收到这个错误
Parameter 0 of method addProduct in com.oshabz.springboot.repositories.ProductRepository required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
我的代码
@Repository
public class ProductRepository {
@Autowired
JdbcTemplate jdbcTemplate;
@Bean
public void addProduct(String name) {
String sql = "INSERT INTO product VALUES (NULL, ?)";
jdbcTemplate.update(sql, name);
}
}
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public void addProduct(String name) {
productRepository.addProduct(name);
}
}
@RestController
@RequestMapping(path="product")
public class ProductController {
@Autowired
ProductService productService;
@PostMapping(path="/add/{name}")
public void addProduct(@PathVariable String name) {
productService.addProduct(name);
}
}
从 addProduct
方法中删除 @Bean
注释。该注释应该用于创建 bean 实例的方法的配置 类。