34. Spring中常用的注解有哪些?
在Spring框架中,注解被广泛用于配置Bean、管理依赖注入、声明事务以及定义切面等功能。以下是Spring中一些常用的注解及其用途:
1. 组件扫描和自动装配相关注解
@Component
:作用:将类标识为Spring管理的Bean。
示例:
@Component public class MyComponent { // 业务逻辑 }
@Service
:作用:将类标识为服务层的Bean,是
@Component
的特殊化,用于标识服务组件。示例:
@Service public class MyService { // 服务层逻辑 }
@Repository
:作用:将类标识为数据访问层的Bean,是
@Component
的特殊化,用于持久化逻辑,并提供异常转换功能。示例:
@Repository public class MyRepository { // 数据访问逻辑 }
@Controller
:作用:将类标识为控制层的Bean,是
@Component
的特殊化,用于Spring MVC控制器。示例:
@Controller public class MyController { // 处理请求的逻辑 }
@Autowired
:作用:自动注入依赖对象,Spring会根据类型自动查找和装配依赖。
示例:
@Component public class MyComponent { @Autowired private MyService myService; }
@Qualifier
:作用:与
@Autowired
结合使用,用于指定注入哪一个Bean,当有多个候选Bean时使用。示例:
@Autowired @Qualifier("mySpecificBean") private MyService myService;
@Primary
:作用:用于标识当有多个候选Bean时优先选择的Bean。
示例:
@Service @Primary public class MyPrimaryService implements MyService { // 主要实现逻辑 }
@ComponentScan
:作用:指定Spring要扫描的包,以查找
@Component
、@Service
、@Repository
等注解标识的类。示例:
@Configuration @ComponentScan(basePackages = "com.example") public class AppConfig { }
2. 配置类相关注解
@Configuration
:作用:标识一个类为Spring的配置类,替代XML配置文件。
示例:
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
@Bean
:作用:用于方法上,将方法的返回值注册为Spring容器中的一个Bean。
示例:
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
@PropertySource
:作用:指定外部属性文件的位置,并将其加载到Spring的环境中。
示例:
@Configuration @PropertySource("classpath:application.properties") public class AppConfig { }
@Value
:作用:用于注入外部属性文件中的值。
示例:
@Component public class MyComponent { @Value("${my.property}") private String myProperty; }
3. Spring MVC相关注解
@RequestMapping
:作用:用于映射HTTP请求到控制器的方法上。
示例:
@Controller @RequestMapping("/api") public class MyController { @RequestMapping("/greeting") public String greet() { return "Hello, World!"; } }
@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
:作用:用于简化
@RequestMapping
,分别映射GET、POST、PUT、DELETE请求。示例:
@RestController @RequestMapping("/api") public class MyController { @GetMapping("/greeting") public String greet() { return "Hello, World!"; } }
@PathVariable
:作用:用于获取URL路径中的参数。
示例:
@GetMapping("/users/{id}") public User getUserById(@PathVariable("id") Long id) { return userService.findById(id); }
@RequestParam
:作用:用于获取请求参数。
示例:
@GetMapping("/search") public List<User> searchUsers(@RequestParam("name") String name) { return userService.findByName(name); }
@RequestBody
:作用:用于将请求体转换为Java对象。
示例:
@PostMapping("/users") public User createUser(@RequestBody User user) { return userService.save(user); }
@ResponseBody
:作用:用于将控制器方法的返回值作为HTTP响应体,而不是视图。
示例:
@GetMapping("/greeting") @ResponseBody public String greet() { return "Hello, World!"; }
@RestController
:作用:相当于
@Controller
和@ResponseBody
的组合,适用于RESTful Web服务的控制器。示例:
@RestController public class MyRestController { @GetMapping("/greeting") public String greet() { return "Hello, World!"; } }
4. 事务管理相关注解
@Transactional
:作用:声明方法或类中的所有方法都需要事务支持,Spring会自动处理事务的开启、提交和回滚。
示例:
@Service public class MyService { @Transactional public void performTransactionalOperation() { // 事务性操作 } }
5. AOP相关注解
@Aspect
:作用:将类声明为切面,切面可以包含多个通知(Advice)。
示例:
@Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Logging before method execution"); } }
@Before
、@After
、@Around
、@AfterReturning
、@AfterThrowing
:作用:定义切面中的通知类型,分别对应于方法执行前、后、环绕、返回后、抛出异常后执行的逻辑。
示例:
@Aspect @Component public class LoggingAspect { @Around("execution(* com.example.service.*.*(..))") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Before method execution"); Object result = joinPoint.proceed(); System.out.println("After method execution"); return result; } }
总结
Spring提供了大量的注解来简化开发和配置工作。通过这些注解,开发者可以更方便地进行依赖注入、事务管理、AOP、以及Web开发,减少XML配置的复杂性,并使代码更具可读性和可维护性。