Backend Development 8 min read

Annotation‑Based Bean Management and Dependency Injection in Spring

Spring’s annotation‑based bean registration and dependency injection replace verbose XML by using component scanning with @Component, @Controller, @Service, and @Repository, while @Autowired (or @Qualifier/@Primary) injects beans by type, @Resource supports name‑based injection, and @Value together with @PropertySource loads external configuration, though developers must handle issues such as NoUniqueBeanDefinitionException when multiple candidates exist.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Annotation‑Based Bean Management and Dependency Injection in Spring

Spring simplifies development, but traditional XML bean definitions are verbose. This article introduces annotation‑based bean registration and dependency injection to eliminate XML configuration.

Component scanning is enabled with <context:component-scan base-package="com.wwj.spring.demo"/> , allowing Spring to automatically detect classes annotated with @Component, @Controller, @Service, or @Repository.

These four annotations register a class as a bean; each conveys a specific role (e.g., @Controller for MVC controllers, @Service for service‑layer objects, @Repository for persistence objects).

Example XML bean definition and its annotated equivalent:

<bean id="user" class="com.wwj.spring.demo.User"> <property name="name" value="zs"/> <property name="age" value="22"/> </bean>

@Component public class User { private String name; private Integer age; }

Automatic injection with @Autowired injects beans by type. When multiple candidates exist, @Qualifier or @Primary can be used to disambiguate:

public class User { @Autowired @Qualifier("userServiceImpl") private UserService userService; }

Alternatively, JSR‑250 @Resource supports both name‑ and type‑based injection:

public class User { @Resource private UserService userService; }

Primitive and configuration values can be injected with @Value . Combined with @PropertySource , values can be read from external property files:

@Component @PropertySource("classpath:jdbc.properties") public class User { @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; }

The article also demonstrates injecting system properties and SpEL expressions, and discusses common pitfalls such as NoUniqueBeanDefinitionException when multiple beans of the same type are present.

JavaSpringannotationsdependency injectionAutowiredComponent ScanQualifier
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.