Spring注解

博客介绍了注解本身包含处理逻辑,用户可获取并执行相关逻辑。重点阐述了Bean的注解有3种方式,分别是@Autowire(默认byName,可与@Qualifier一起指定类型)、@Resource和@Inject。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注解

注解本身包含处理逻辑,但是用户可以获取注解,并执行相关逻辑。
Bean的注解有以下3种方式:

  1. @Autowire 默认byName,可以与 @Qualifier 一起使用,指定类型
// org.springframework.beans.factory.annotation.Autowired  spring的注解,可以与Qualifier使用指定注入的类型
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
	/**
	 * Declares whether the annotated dependency is required.
	 * <p>Defaults to {@code true}.
	 */
boolean required() default true;
}

2)@Resource

 //javax.annotation.Resource          JSR-250
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
/**
*The JNDI name of the resource.
*/
String name() default "";
String lookup() default "";
Class<?> type() default java.lang.Object.class;
enum AuthenticationType {
           CONTAINER,
           APPLICATION
}
AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
boolean shareable() default true;
String mappedName() default "";
String description() default "";

3)@Inject

 //JSR-330 的标准注解,javax.inject.Inject;
 @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface Inject {
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值