开发者

Spring中的@Repository注解源码详解

开发者 https://www.devze.com 2023-11-19 15:01 出处:网络 作者: 生命不息战斗不止(王子晗)
目录@Repository注解@Autowired注解和@Resource区别@Repository注解 不多废话,直接看源码
目录
  • @Repository注解
  • @Autowired注解和@Resource区别

@Repository注解

不多废话,直接看源码

@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);
    }
}

该注解是告诉Sp编程ring,让Spring创建一个名字叫“userServiceNew的UserServiceImpl实例。当Service需要使用Spring创建的名字叫“userServiceNew”的UserServiceIm编程pl实例时,就可以使用@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)

  • @Autowireandroidd作用域:@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
  • @Resource作用域:@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})

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

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

@Service
public class UserServiceNew {
}

启动springboot时就会报错:

Failed to parse conf编程iguration 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 {
}

到此这篇关于Spring中的@Repository注解源码详解的文章就介绍到这了,更多相关@Repository注解内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号