简体   繁体   English

了解Spring注释DI

[英]Understanding Spring annotation DI

@Repository @Service @Controller @Component @存储库@服务@控制器@组件

-->only use for spring managed bean (no need weaving) ->仅用于春季托管bean(无需编织)
-->@repository, @Service @controller is actually a @Component , just naming easier for programmer to understand -> @ repository,@ Service @controller实际上是一个@Component,只是命名更易于程序员理解

@Configurable

--->used for non spring managed bean (use with weaving) --->用于非春季托管的bean(用于编织)

@Autowired

--> use for DI for both cases above ->用于以上两种情况的DI

Is my understanding correct? 我的理解正确吗?

All but @Configurable are correct. 除@Configurable之外的所有内容都是正确的。 From the Spring javadocs: 从Spring javadocs:

public @interface Configurable Marks a class as being eligible for Spring-driven configuration. public @interface Configurable将一个类标记为符合Spring驱动的配置。 Typically used with the AspectJ AnnotationBeanConfigurerAspect. 通常与AspectJ AnnotationBeanConfigurerAspect一起使用。

Spring annotations draws different purposes. Spring注释具有不同的用途。 As you know Spring heavily uses proxies in order to provide its funcionality. 如您所知,Spring大量使用代理来提供其功能。 But this funcionality depends on the target annotation . 但是这种功能取决于目标注释

So when you put 所以当你把

@Repository
public class UserRepositoryImpl implements UserRepository {

    public void saveUser(User user) {
        // logic goes here
    }

}

You are saying 你是说

Spring, proxy my UserRepositoryImpl and if it throws any database exception, caught it and re-throw it as a DataAccessException, a generic Spring database exception Spring,代理我的UserRepositoryImpl,如果它抛出任何数据库异常,请捕获它,然后将其作为DataAccessException(一种通用的Spring数据库异常)重新抛出

And so on... 等等...

regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM