1

I am new to Spring Boot and I was just wondering if I need all annotations on my main method that I currently have Here they are

    @Import(ServiceConfiguration.class)
@SpringBootApplication(scanBasePackages = {"com.myproject.rest",})
@EnableJpaRepositories({"com.myproject.dao.jpa"})
@EntityScan(basePackages = "com.myproject.domain.jpa")

The class ServiceConfiguration.class has the following annotations

    @Configuration
@EnableConfigurationProperties({SlackServiceProperties.class})

My database objects have the @Entity annotation, my rest classes have the @RestController annotation and my service classes have the @Component annotation

Just wondering are they all needed or can I exclude any of these annotations?

Thanks

2
  • 1
    I'm also new to Spring Boot, but I think what you have probably looks OK. Here is the start of the @SpringBootAppliaction annotation documentation - docs.spring.io/autorepo/docs/spring-boot/current/reference/html/…
    – Ryan D
    Commented Apr 3, 2017 at 20:10
  • Without seeing the actual packages your application class is in it is hard to tell if you need more or less annotations.
    – M. Deinum
    Commented Apr 4, 2017 at 3:58

3 Answers 3

3

If your main method is located in a top-level package, then all you need is:

@SpringBootApplication

It will automatically scan your sources recursively and pick up any @Bean's, @Component's or @Configuration's

Spring Data JPA will also automatically be configured if you're using this starter:

spring-boot-starter-data-jpa
1

If you structure your code like so:

com.myproject
 - Application.java
com.myproject.configuration
 - ServiceConfiguration.java
 - OtherConfiguration.java
com.myproject.dao.jpa
 - .. your repositories..
com.myproject.domain.jpa
 - .. your @Entity classes...

You ONLY need to annotate your Application class with @SpringBootApplication.

All your @Configuration, repositories, other @Component and @Service classes will be auto scanned and configured by Spring Boot. That means you DON'T need to manually @Import configurations, you don't need @EnableJpaRepositories or @EntityScan.

All you need for Spring Boot to configure JPA is to include the JPA starter:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

You can follow this tutorial: https://spring.io/guides/gs/accessing-data-jpa/

You will see that it requires minimal annotations.

1

I assume it may depend on what you need, as I have not used a few of those in your example. I am not too familiar with all of Spring's "magic" when it comes to behind-the-scenes work it does with annotations. I have a perfectly functioning Spring Boot app with only

@SpringBootApplication
@Configuration
@ComponentScan

In order,

@SpringBootApplication is Spring's way of identifying that this application is from Spring Boot (one consequence, for example, is that there is no web.xml file).

@Configuration tells Spring to look for .properties files on your src path. Here, for example, you can define an "application.properties" file to define your datasource (database information for Spring to use).

@Component tells Spring to look for "Components" when it starts up the application. Pretty much @Controllers, @Service, etc. that may be found throughout your application.

For more accurate and in-depth explanations for many of Spring's annotations, may I direct you to:

http://www.techferry.com/articles/spring-annotations.html and https://dzone.com/refcardz/spring-annotations

These both have excellent descriptions and examples for the annotations.

Edit: @Configuration and @ComponentScan are included in @SpringBootApplication, as Strelok has pointed out in the comments.

Hope that helps.

2
  • You only need @SpringBootApplication, it's a meta annotation that implies all the others.
    – Strelok
    Commented Apr 4, 2017 at 5:58
  • Strelok, you are correct. My application still functions the same without them. I've edited my answer to acknowledge this. Commented Apr 4, 2017 at 20:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.