0

I am trying to run a spring boot application which gets a list of names of people from the database. I am getting the below error :

Description:

Field peopleserviceinterface in com.sample.lombpackdemo.controller.FriendController required a bean of type 'com.sample.lombpackdemo.service.FriendsServiceInterface' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.sample.lombpackdemo.service.FriendsServiceInterface' in your configuration.

package com.sample.lombpackdemo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sample.lombpackdemo.entity.Friends;
import com.sample.lombpackdemo.rowmapper.FriendsRowMapper;
import com.sample.lombpackdemo.service.FriendsServiceInterface;



@CrossOrigin(origins = { "http://localhost:3000"})//to allow access from other servers
@RestController
@RequestMapping("/code")
@ComponentScan(basePackages="com.sample.lombpackdemo.service")
public class FriendController { 
    
    @Autowired
    private FriendsServiceInterface peopleserviceinterface;//autowire the service

    
    @GetMapping("/all")
    public ResponseEntity<List<Friends>> getAllPeople(){
        List <Friends> listOfAllPpl = peopleserviceinterface.getAllFriends();
        System.out.println("Getting all friends"+listOfAllPpl.toString());
        return new ResponseEntity<List<Friends>>(listOfAllPpl,HttpStatus.OK);
        
    }   


  

    

}

The FriendServiceInterface class is as below:

package com.sample.lombpackdemo.service;

import java.util.List;

import org.springframework.stereotype.Component;

import com.sample.lombpackdemo.entity.Friends;

@Component
public interface FriendsServiceInterface {
    

        public List<Friends> getAllFriends();
            
    }

The FriendService class:

package com.sample.lombpackdemo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import com.sample.lombpackdemo.entity.Friends;
import com.sample.lombpackdemo.repos.FriendsRepo;
import com.sample.lombpackdemo.rowmapper.FriendsRowMapper;

@Service
@Profile("devel")//added to disable CORS only on development time
@Configuration
@Component
public class FriendService implements FriendsServiceInterface {

    @Autowired
    private FriendsServiceInterface peopleserviceinterface;
    @Autowired
    private FriendsRepo pplRepo;//should always autowire repository
    @Autowired
    private JdbcTemplate jdbc;

    private static long idCounter = 0;

    //FriendsRowMapper fRowMap=new FriendsRowMapper();
    


    @Override
    public List<Friends> getAllFriends() {
        
        
        String sql="select f_name from list_friends";
        return jdbc.query(sql, new FriendsRowMapper() );
        
    
        
        /*
        List<Friends> ppList= (List<Friends>) fRowMap.findAll();
        try {
            System.out.println("Repository value"+pplRepo.findAll());
        System.out.println("Inside findAll of service class" +ppList.toString() );
        }
        catch(Exception e)
        {
            
        }
        return ppList;
        
        
        // 
        
    */}





    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
                //registry.addMapping("/save-javaconfig").allowedOrigins("http://localhost:3000");
            }
        };
    }


Please let me know what else needs to be changed. I have tried adding Component annotation to FriendService class and ComponentScan annotation to the controller class.

Edited to add JdbcConfig class

package com.sample.lombpackdemo.repos;

//import java.util.logging.Logger;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ComponentScan(basePackages = "com.sample.lombpackdemo")
public class SpringJdbcConfig {
    
    protected final Logger log = LoggerFactory.getLogger(getClass());
    
        @Bean
        public DataSource mysqlDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/person_example");
            dataSource.setUsername("root");
            dataSource.setPassword("subfio");
          
            

            return dataSource;
        }
        
        @Bean(name = "dbProductService")
        @ConfigurationProperties (prefix = "spring.datasource")
        @Primary public DataSource createProductServiceDataSource() 
        { System.out.println("Inside db cofig ");
        return DataSourceBuilder.create().build(); }
    

}






    

    






}
Pisces1719
  • 25
  • 6
  • You really don't need componentscan, if you Add `@SpringBootApplication` at the root package. Add componentScan with a Class annotated with `@Configuration`. Create a new class with `@Configuration`. Change basepackages to `com.sample.lombpackdemo`. Remove `@Configuration` and `@Component` from FriendService. – Shawrup Aug 28 '21 at 18:29
  • I have the JdbcConfig class annotated with @Configuration. Do I need to create another configuration class ? I have edited my question to add the Jdbc Config class – Pisces1719 Aug 28 '21 at 20:43
  • if you have one configuration class, then you do not need another. Does it work ? – Shawrup Aug 29 '21 at 06:57
  • No its showing the same error. – Pisces1719 Aug 29 '21 at 08:25
  • What is the profile you set in application.properties ? If it is not `devel` then you need to remove `@Profile` from FriendService. – Shawrup Aug 29 '21 at 09:19
  • I have removed the Profile annotation from friend service and it is working now – Pisces1719 Aug 30 '21 at 06:19
  • I removed the Profile annotation from FriendService and application has started running. – Pisces1719 Aug 30 '21 at 06:22
  • The application shows values returned from the db on the console but on hitting the localhost from UI, it shows empty arrays.. Is this also due to some missing configuration? – Pisces1719 Aug 30 '21 at 06:26

0 Answers0