简体   繁体   中英

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 cases 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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