1

I'm fiddling with Springboot but each step forward brings me two steps back it seems.

I wired my simple app together with Springboot but I'm not getting this error:

Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataAccessException

As well as this:

Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration due to org/springframework/dao/DataAccessException not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

I don't use any database. The app is very simple. Here is the main class:

@SpringBootApplication
public class Helloer {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Helloer.class, args);
        A a = ctx.getBean(A.class);
        a.speak();
    }
}

Here are the other two classes A and B:

@Component
public class A {
    @Autowired
    private B b;

    @Value("Covid 19")
    private String calamity;

    public void speak() {
        b.writeToScreen(this.calamity);
    }
}

@Component
public class B {

    public void writeToScreen(String message) {
        System.out.println(message);
    }
}

And here is the config:

@Configuration
public class Config {

    @Bean
    public B b() {
        return new B();
    }
}

That's it. Simple as it can possibly be.

Here is the file structure as well as the build.gradle file content:

enter image description here

can someone please help me get this tiny program running by pointing me in the right direction as to the fix for this issue?

Thank you kindly in advance.

Community
  • 1
  • 1
Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69

2 Answers2

0

I think the error is related to this error message:

This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake).

If I correctly see from your screenshot the Helloer.java is in the default package of your project. You need to move it into ca.company package. Also as you already define the B class as component you don't need to configure another B bean in the Config class.

W-S
  • 516
  • 5
  • 14
  • Sorry, the screenshot does not show default package. They are in one called `ca.company` – Michael May 24 '20 at 16:51
  • Well, I am just guessing as the OP also didn't show it in the code. But I am able to run the code above simply by moving this `Helloer` class out from default package, in this case into the package `ca.company`. – W-S May 24 '20 at 17:40
  • There is a screenshot. You don't have to guess. It's not in the default package. – Michael May 24 '20 at 17:47
0

If you don't require the database autoconfiguration, you can explicitly opt-out of it.

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

See also Disable all Database related auto configuration in Spring Boot

Michael
  • 41,989
  • 11
  • 82
  • 128