Issue
So I was running a Springboot application then I got this error
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type 'sample.ArticledDao' that could not be found.
Action:
Consider defining a bean of type 'sample.ArticledDao' in your configuration.
Then I checked how SpringBoot scans the files and I saw some of the files are ignored
This is how the path looks ( It's not ideal I know) :
This is the code of my Main class :
@SpringBootApplication(scanBasePackages="sample")
@EnableJpaRepositories("sample")
public class Main {
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
}
This is my ArticlesDao class:
public interface ArticledDao {
List<Articles> findAll();
public void deleteArticles(Articles articles);
}
ArticleSercviceImp class
@Component
public class ArticleSericeImp implements ArticlesServices
{
@Autowired
ArticledDao articleDao;
@Override
public List<Articles> findAll() {
return articleDao.findAll();
}
@Override
public void deleteEmployee(Articles art) {
articleDao.deleteArticles(art);
}
ArticleRepository:
@Repository
public class ArticlesRepository implements ArticledDao{
public ArticlesRepository(NamedParameterJdbcTemplate template) {
this.template = template;
}
NamedParameterJdbcTemplate template;
@Override
public List<Articles> findAll() {
return template.query("select * from article", new ArticlesRowMapper());
}
@Override
public void deleteArticles(Articles articles) {
final String sql = "delete from article where articleid=:articleid";
Map<String,Object> map=new HashMap<String,Object>();
map.put("articleid", articles.getId());
template.execute(sql,map,new PreparedStatementCallback<Object>() {
@Override
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return ps.executeUpdate();
}
});
}
}
<?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>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sprinboot_project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Sprinboot_Project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.29.Final</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
I'm new to SpringBoot so I would be very grateful for your help.
Solution
I have added the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
and removed the spring-data
dependency as well (this will bring in all needed transitive dependencies like hibernate etc.).
Then you have excluded the datasource autoconfiguration in your application.properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
However this auto configuration is needed to autoconfigure the JdbcTemplate, which you use in your ArticleRepository class. So you need to remove that line. With those changes i can get at least to the point where it tries to connect to Postgres (which fails in my case because i don't have a running instance).
Answered By - dunni