Reputation: 585
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
Upvotes: 1
Views: 2759
Reputation: 61
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.
Upvotes: 1
Reputation: 51461
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.
Upvotes: 1
Reputation: 7041
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
Upvotes: 3