注解驱动的依赖注入(DI)是Spring框架中一种简洁且强大的依赖注入方式,通过使用注解来标识和管理Bean及其依赖关系。注解驱动的DI使得配置更加简洁,代码更加清晰。
常用注解
@Component
:标识一个类为Spring管理的组件。@Service
:标识一个类为服务层组件,功能上与@Component
相同,但语义上更明确。@Repository
:标识一个类为数据访问层组件,功能上与@Component
相同,但语义上更明确。@Controller
:标识一个类为Spring MVC控制器,功能上与@Component
相同,但语义上更明确。@Autowired
:用于自动注入依赖对象,可以标注在构造器、字段或Setter方法上。@Qualifier
:与@Autowired
一起使用,用于指定注入的Bean的名称。@Value
:用于注入属性值。@Configuration
:标识一个类为Spring配置类。@ComponentScan
:用于指定Spring扫描的包,以发现和注册Bean。
示例代码
以下是一个使用注解驱动的DI的示例:
Java代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
// 定义一个组件
@Component
public class MyBean {
public void doSomething() {
System.out.println("Doing something...");
}
}
// 定义一个服务层组件
@Service
public class MyService {
private final MyBean myBean;
@Autowired
public MyService(MyBean myBean) {
this.myBean = myBean;
}
public void performAction() {
myBean.doSomething();
}
}
// 定义一个配置类
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
// 主类
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = context.getBean(MyService.class);
myService.performAction();
}
}
在这个示例中:
MyBean
类使用@Component
注解标识为Spring管理的组件。MyService
类使用@Service
注解标识为服务层组件,并通过构造器注入MyBean
,使用@Autowired
注解标识构造器。AppConfig
类使用@Configuration
注解标识为Spring配置类,并使用@ComponentScan
注解指定要扫描的包。- 在
Main
类中,通过AnnotationConfigApplicationContext
加载配置类AppConfig
,并获取MyService
的Bean实例,调用其方法。
使用@Qualifier
注解
当有多个相同类型的Bean时,可以使用@Qualifier
注解指定要注入的Bean的名称。
示例代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component("bean1")
public class MyBean1 {
public void doSomething() {
System.out.println("Doing something in MyBean1...");
}
}
@Component("bean2")
public class MyBean2 {
public void doSomething() {
System.out.println("Doing something in MyBean2...");
}
}
@Service
public class MyService {
private final MyBean1 myBean1;
private final MyBean2 myBean2;
@Autowired
public MyService(@Qualifier("bean1") MyBean1 myBean1, @Qualifier("bean2") MyBean2 myBean2) {
this.myBean1 = myBean1;
this.myBean2 = myBean2;
}
public void performAction() {
myBean1.doSomething();
myBean2.doSomething();
}
}
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = context.getBean(MyService.class);
myService.performAction();
}
}
在这个示例中:
MyBean1
和MyBean2
类分别使用@Component
注解标识为Spring管理的组件,并指定了名称bean1
和bean2
。MyService
类通过构造器注入MyBean1
和MyBean2
,并使用@Qualifier
注解指定要注入的Bean的名称。
总结
注解驱动的依赖注入是Spring框架中一种简洁且强大的依赖注入方式,通过使用注解来标识和管理Bean及其依赖关系。常用的注解包括@Component
、@Service
、@Repository
、@Controller
、@Autowired
、@Qualifier
、@Value
、@Configuration
和@ComponentScan
。注解驱动的DI使得配置更加简洁,代码更加清晰,是现代Spring应用程序开发中常用的方式。