I am just trying to start the application on intellij but the issue is coming as above. Checked for dependencies in pom.xml file but all is fine with the log:
Log:
2023-12-24T17:22:38.411+05:30 WARN 15340 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messageController' defined in file [/Users/rohitkumar/Documents/projects/PeerMessenger/target/classes/com/peermessenger/controller/MessageController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'messageService' defined in file [/Users/rohitkumar/Documents/projects/PeerMessenger/target/classes/com/peermessenger/service/MessageService.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.peermessenger.repository.MessageRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2023-12-24T17:22:38.412+05:30 INFO 15340 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.peermessenger.repository.MessageRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.peermessenger.service.MessageService required a bean of type 'com.peermessenger.repository.MessageRepository' that could not be found.
Action:
Consider defining a bean of type 'com.peermessenger.repository.MessageRepository' in your configuration.
Process finished with exit code 1
My Application file looks like:
package com.peermessenger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PeerMessengerApplication {
public static void main(String[] args) {
SpringApplication.run(PeerMessengerApplication.class, args);
}
}
MessageRepository:
package com.peermessenger.repository;
import com.peermessenger.model.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findByReceiverId(Long receiverId);
@Query("SELECT m FROM Message m WHERE m.receiver.username = :receiverUsername AND m.read = false")
List<Message> findUnreadMessagesForUser(@Param("receiverUsername") String receiverUsername);
}
I added @ComponentScan("com.peermessenger.repository") in the application driver class and it worked.