我是 Spring Boot 新手,我想創建一個將值插入資料庫的應用程式
我已經創建了服務、存盤庫、配置和控制器
但我收到了這個錯誤
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);
}
}
uj5u.com熱心網友回復:
從方法中洗掉@Bean注釋addProduct。該注釋應該用于方法的配置類中,這些方法創建 bean 實體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429136.html
下一篇:java-如何在JavaSpringBoot中使用@Component注釋在創建Bean之前使用@Value注釋獲取值
