Repository bean not found for service implementation

I'm new in spring boot, I came across this problem where I couldn't run the app TLDR of the error:
Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.
Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.
I have tried debugging it, but I couldn't read and solve the problem, as I've reached with copilot to confirm whether or not I did this right, they even said my structure and annotations seem fine.
33 Replies
JavaBot
JavaBot8mo ago
This post has been reserved for your question.
Hey @circle! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
circle
circleOP8mo ago
ProjectServiceImpl
package dev.vianneynara.portoapi.service;

//...

import java.util.Collection;

@Service
public class ProjectServiceImpl implements ProjectService {

private final ProjectRepository projectRepository;

@Autowired
public ProjectServiceImpl(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

//...
package dev.vianneynara.portoapi.service;

//...

import java.util.Collection;

@Service
public class ProjectServiceImpl implements ProjectService {

private final ProjectRepository projectRepository;

@Autowired
public ProjectServiceImpl(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

//...
ProjectRepository
package dev.vianneynara.portoapi.repository;

//...

@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
/**
* Queries a project by its exact name.
*
* @return Optional {@link Project} with the given name.
* */
Optional<Project> findByName(@NotBlank String name);

/**
* Queries projects that contains the given name.
*
* @return Optional {@link Project} that contains the given name.
* */
Optional<Project> findByNameContaining(@NotBlank String name);

/**
* Queries projects that contains the given tag.
*
* @return {@link Collection} of {@link Project} that contains the given tag.
* */
Collection<Project> findByTagsContainsIgnoreCase(@NotBlank String tag);

/**
* Queries projects that matches the passed id (owner id).
*
* @return {@link Collection} of {@link Project} that contains the given owner id.
* */
Collection<Project> getProjectsByOwnerId(Long id);

/**
* Updates a project by its id, passing another project to acquire the new
* values.
* */
void updateProjectById(Long id, Project project);
}
package dev.vianneynara.portoapi.repository;

//...

@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
/**
* Queries a project by its exact name.
*
* @return Optional {@link Project} with the given name.
* */
Optional<Project> findByName(@NotBlank String name);

/**
* Queries projects that contains the given name.
*
* @return Optional {@link Project} that contains the given name.
* */
Optional<Project> findByNameContaining(@NotBlank String name);

/**
* Queries projects that contains the given tag.
*
* @return {@link Collection} of {@link Project} that contains the given tag.
* */
Collection<Project> findByTagsContainsIgnoreCase(@NotBlank String tag);

/**
* Queries projects that matches the passed id (owner id).
*
* @return {@link Collection} of {@link Project} that contains the given owner id.
* */
Collection<Project> getProjectsByOwnerId(Long id);

/**
* Updates a project by its id, passing another project to acquire the new
* values.
* */
void updateProjectById(Long id, Project project);
}
circle
circleOP8mo ago
response from built in copilot, had to ask it
No description
dan1st
dan1st8mo ago
How are you running the application (which class are you running, which package is that in etc)? Can you show your application.properties?
circle
circleOP8mo ago
Here is the application.properties:
spring.application.name=PortoAPI

# DATASOURCE CONFIGURATION
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/portoapi
spring.datasource.username=something
spring.datasource.password=something
spring.application.name=PortoAPI

# DATASOURCE CONFIGURATION
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/portoapi
spring.datasource.username=something
spring.datasource.password=something
I excluded the database because I haven't used it previously, but commenting the exclusion doesn't affect anything. I run it using .\mvnw spring-boot:run or with -X
circle
circleOP8mo ago
No description
circle
circleOP8mo ago
It looks just fine to me, the PortoApiApplication is the one to run
dan1st
dan1st8mo ago
Can you show the stack trace?
circle
circleOP8mo ago
Do you want me to put it in a log txt?
dan1st
dan1st8mo ago
ideally using a codeblock?
circle
circleOP8mo ago
alright, it might be if u want to debug. Coming. I cut the spring logo part allong with some uh
: Root WebApplicationContext: initialization completed in 905 ms
2024-04-14T17:00:51.609+07:00 WARN 30440 --- [PortoAPI] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController' defined in file [C:\Users\narwa\Developments\__java__\PortoAPI\target\classes\dev\vianneynara\portoapi\controller\ProjectController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'projectServiceImpl' defined in file [C:\Users\narwa\Developments\__java__\PortoAPI\target\classes\dev\vianneynara\portoapi\service\ProjectServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2024-04-14T17:00:51.612+07:00 INFO 30440 --- [PortoAPI] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2024-04-14T17:00:51.623+07:00 INFO 30440 --- [PortoAPI] [ main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-04-14T17:00:51.641+07:00 ERROR 30440 --- [PortoAPI] [ main] o.s.b.d.LoggingFailureAnalysisReporter :
: Root WebApplicationContext: initialization completed in 905 ms
2024-04-14T17:00:51.609+07:00 WARN 30440 --- [PortoAPI] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController' defined in file [C:\Users\narwa\Developments\__java__\PortoAPI\target\classes\dev\vianneynara\portoapi\controller\ProjectController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'projectServiceImpl' defined in file [C:\Users\narwa\Developments\__java__\PortoAPI\target\classes\dev\vianneynara\portoapi\service\ProjectServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2024-04-14T17:00:51.612+07:00 INFO 30440 --- [PortoAPI] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2024-04-14T17:00:51.623+07:00 INFO 30440 --- [PortoAPI] [ main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-04-14T17:00:51.641+07:00 ERROR 30440 --- [PortoAPI] [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.704 s
[INFO] Finished at: 2024-04-14T17:00:51+07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.2.4:run (default-cli) on project PortoAPI: Process terminated with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in dev.vianneynara.portoapi.service.ProjectServiceImpl required a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' that could not be found.


Action:

Consider defining a bean of type 'dev.vianneynara.portoapi.repository.ProjectRepository' in your configuration.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.704 s
[INFO] Finished at: 2024-04-14T17:00:51+07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.2.4:run (default-cli) on project PortoAPI: Process terminated with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
It's starts around the UnsatisfiedDependencyException part
dan1st
dan1st8mo ago
Can you show your pom.xml?
circle
circleOP8mo ago
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.vianneynara</groupId>
<artifactId>PortoAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SimplePortoAPI</name>
<description>SimplePortoAPI</description>
<properties>
<java.version>17</java.version>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.vianneynara</groupId>
<artifactId>PortoAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SimplePortoAPI</name>
<description>SimplePortoAPI</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
dan1st
dan1st8mo ago
Why do you have the spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration in your application.properties?
circle
circleOP8mo ago
I didn't use the database It was raising autoconfig on datasource error
dan1st
dan1st8mo ago
But you need the repository? Well you can't use a repository without a valid datasource
circle
circleOP8mo ago
Not yet, I haven't used it. I wanted to now wait... let me check
dan1st
dan1st8mo ago
well ProjectServiceImpl requires it so due to the existence of ProjectServiceImpl, you need a valid DataSource
circle
circleOP8mo ago
I tried many times now, I have also made the database and table... But the error persists on that problem I might've foudn a clue
circle
circleOP8mo ago
circle
circleOP8mo ago
This is the chained error, which I assume points out to the bottom of the problem
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void dev.vianneynara.portoapi.repository.ProjectRepository.updateProjectById(java.lang.Long,dev.vianneynara.portoapi.model.Project); Reason: Failed to create query for method public abstract void dev.vianneynara.portoapi.repository.ProjectRepository.updateProjectById(java.lang.Long,dev.vianneynara.portoapi.model.Project); No property 'updateProjectById' found for type 'Project'
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void dev.vianneynara.portoapi.repository.ProjectRepository.updateProjectById(java.lang.Long,dev.vianneynara.portoapi.model.Project); Reason: Failed to create query for method public abstract void dev.vianneynara.portoapi.repository.ProjectRepository.updateProjectById(java.lang.Long,dev.vianneynara.portoapi.model.Project); No property 'updateProjectById' found for type 'Project'
updateProjectById is being used in a method in ProjectServiceImpl
dan1st
dan1st8mo ago
What is updateProjectById supposed to be? Why do you think Spring would allow that?
circle
circleOP8mo ago
That is also my question, why did I think like that. I think I misunderstood what they meant
circle
circleOP8mo ago
this works...
No description
circle
circleOP8mo ago
just by commenting it I'm thinking of a PUT method for this that's why i abstracted the method, which I thought may be required
dan1st
dan1st8mo ago
you can also do void save(Project project); I think where the ID is in the project
circle
circleOP8mo ago
Yea, I saw a repo doing this implementation but I couldn't quite understand how it really works yet. I just read the docs on CrudRepository.save(), so even if it's an existing project, I may also want to use that instead?
dan1st
dan1st8mo ago
yes
circle
circleOP8mo ago
Ah, would this be the right implementation?
@Override
public void updateProject(Long id, Project project) {
Project p = projectRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Project with id '" + id + "' could not be found!"));
p.setName(project.getName());
p.setDescription(project.getDescription());
p.setImageUrl(project.getImageUrl());
p.setTags(project.getTags());
p.setProjectUrl(project.getProjectUrl());
projectRepository.save(p);
}
@Override
public void updateProject(Long id, Project project) {
Project p = projectRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Project with id '" + id + "' could not be found!"));
p.setName(project.getName());
p.setDescription(project.getDescription());
p.setImageUrl(project.getImageUrl());
p.setTags(project.getTags());
p.setProjectUrl(project.getProjectUrl());
projectRepository.save(p);
}
dan1st
dan1st8mo ago
yeah you should be able to do something like that and I think you might even do something like
project.setId(id);
projectRepository.save(project);
project.setId(id);
projectRepository.save(project);
though you'd need to test that (especially that this doesn't create a new project with a different ID)
circle
circleOP8mo ago
Hard to see what it does in the background, I can't find how it works underneath, only points out to the interface when i ctrl click it. I believe by doing that .setId then saving it, the .save method saves by looking up whether it exists or not? If it does exist, then it simply overwrite the previous value with the corresponding id?
dan1st
dan1st8mo ago
I think so but you'd need to test it it might be different if you use @GeneratedValue For testing it, you could - enable logging of the SQL statements and check what is executed in the DB - just add an existing one to the DB, change it with that approach and check which elements are in the DB afterwards
JavaBot
JavaBot8mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server