Home > database >  required a bean of type 'java.lang.String' that could not be found
required a bean of type 'java.lang.String' that could not be found

Time:02-22

This the error iam when I tried start my spring boot application getting on console Action: Consider defining a bean of type 'java.lang.String' in your configuration.

My code

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

CodePudding user response:

Here, you are trying to create bean with dependency object of type String in addProduct(). When Spring application starts, it tries to look for Bean of type String. It makes no sense.

So, no need of the @Bean annotation. The beans are usually created in classes annotated with @Configuration.

CodePudding user response:

try removing @Bean over addproduct method

  • Related