开发者

Understanding Spring annotation DI

@Repository @Service @Controller @Component

-->only use for spring managed bean (no need weaving)

-->@repository, @Service @controller is actually a @Component , just naming easier for programmer to understand

@Configurable

--->used for non spring managed bean (use with weaving)

@Autowired

--> use for DI for both c开发者_Go百科ases above

Is my understanding correct?


All but @Configurable are correct. From the Spring javadocs:

public @interface Configurable Marks a class as being eligible for Spring-driven configuration. Typically used with the AspectJ AnnotationBeanConfigurerAspect.


Spring annotations draws different purposes. As you know Spring heavily uses proxies in order to provide its funcionality. 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

And so on...

regards,

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜