Master the 9 Essential SpringBoot Annotations for Backend Development
This article provides a concise yet comprehensive guide to the most common SpringBoot annotations—including @SpringBootApplication, @EnableAutoConfiguration, @ComponentScan, @Service, @Repository, @Component, @RestController, @Bean, @ResponseBody, and @Autowired—explaining their purpose, composition, and practical code examples for building robust Java backend applications.
@SpringBootApplication
The @SpringBootApplication annotation marks the main class of a Spring Boot application and implicitly combines three core annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@SpringBootApplication
public class StartEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(StartEurekaApplication.class, args);
}
}@EnableAutoConfiguration
Enables Spring Boot’s auto‑configuration mechanism, which automatically registers beans based on class‑path contents.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}@ComponentScan
Scans the specified packages for components such as @Controller, @Service, @Repository, and @Component, automatically registering them in the Spring container.
@ComponentScan("com.mikechen.test")
@SpringBootApplication
public class SpringbootApplication { }@Service
Marks a class as a service component, making it eligible for automatic detection and injection.
@Service("courseDAO")
@Scope("prototype")
public class CourseDAOImpl extends HibernateDaoSupport implements CourseDAO {
// implementation details
}@Repository
Designates a class as a data‑access component and enables exception translation for DAO operations.
@Repository
public class UserDaoImpl implements UserDao {
@Override
public int insertUser() {
JdbcTemplate template = new JdbcTemplate();
// implementation details
}
}@Component
A generic stereotype for any Spring-managed component that does not fit other specific stereotypes.
@RestController
Combines @Controller and @ResponseBody, allowing methods to return JSON or other serialized data directly.
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/test")
public Map addGoodsToCartList(Long itemId, Integer num) {
Map map = new HashMap();
map.put("success", true);
map.put("message", "welcome");
return map;
}
}@Bean
Indicates that a method produces a bean to be managed by the Spring container, similar to an XML definition.
@Configuration
public class BeanConfig {
@Bean
public Person person() {
return new Person("mikechen");
}
}@ResponseBody
Signals that the return value of a controller method should be written directly to the HTTP response body, typically as JSON or XML.
@Controller
public class HelloWorld {
@RequestMapping("/hello")
@ResponseBody
public String testSpringBoot() {
return "Hello SpringBoot 2 !";
}
}@Autowired
Enables automatic injection of beans by type, applicable to fields, setter methods, or constructors.
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
