@Repository详解

39 篇文章 0 订阅
36 篇文章 0 订阅

不多废话,直接看源码

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

分析如下

  • @Component代表@Repository可以把一个类组件加入到IOC容器中

  • @Target,target注解决定MyAnnotation注解可以加在哪些成分上,如加```xml

    ElementType.TYPE // 作用在类身上
    ElementType.Filed) //作用到属性身上
    ElementType.METHOD //作用到方法身上

  • @Retention注解决定MyAnnotation注解的生命周期

    生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用

    source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略,如果只是做一些检查性的操作,
    比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解

    class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期,如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解

    runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在,如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解

    这3个生命周期分别对应于:Java源文件(.java文件) —> .class文件 —> 内存中的字节码。

此时我们差不多把该注解剖析完了,该注解还有一个字段value,value其实是在java程序动态运行时去告诉Spring创建一个名字为xxx的组件实例,比如

@Repository(value="userServiceNew")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

该注解是告诉Spring,让Spring创建一个名字叫“userServiceNew的UserServiceImpl实例。当Service需要使用Spring创建的名字叫“userServiceNew”的UserServiceImpl实例时,就可以使用@Resource(name = “UserServiceNew”)注解告诉Spring,Spring把创建好的UserServiceImpl注入给Service即可。

@Repository(value="userServiceNew")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

案例如下

@RestController
@RequestMapping("/testBoot")
public class UserController {
 
    @Resource(name = "userServiceNew")
    private UserService userService;
 
    @RequestMapping("getUser/{id}")
    public Object GetUser(@PathVariable int id){
        return userService.Sel(id);
    }
}

@Autowired注解和@Resource区别

  • 作用范围不相同(field,setter,constructor,method’s param)

    @Autowired作用域:@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})

    @Resource作用域:@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})

  • 策略不同,前者默认按类型操作,如果找到多个再按组件名字查找,或者通过@Qualifier判断,有@Qualifier修饰那么直接按后者默认按名字查找,即使没有指定名字也会安装注解作用的对象名来匹配,按默认组件名没有查找到再按类型查找

如果我另一个包出现了同名的类

@Service
public class UserServiceNew {
}

启动springboot时就会报错:

Failed to parse configuration class [com.sobot.demo7.Demo7Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ‘userServiceNew’ for bean class [com.sobot.demo7.service.UserServiceNew] conflicts with existing, non-compatible bean definition of same name and class [com.sobot.demo7.service.UserServiceImpl]

SpringMVC的Controller 应该是采用类似键值对(key/value)的映射方式处理的。而当中的键,默认是用cotroller的类名(非全类名)作为键。这样,如果不同包下面的两个Contoller 重名的话,就会导致SpringMVC的容器管理中的controller map中的key重复了。所以我们可以通过重命名来解决这个问题,比如

@Service(value = "userSerivce")
public class UserServiceNew {
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值