Annotation Injection & XML Injection

Annotation Injection & XML Injection

1. Which is better?

I find some information in the official reference to explain this question, here is the screenshot:



In conclusion, it depends. However, there is a point we should pay attention to:



2. Namespace for annotation

If you want to learn more about xml injection, you can go through my recent articles.

Before we start using annotation injection, we need to add some namespaces to our config.xml:



Things in purple rectangle are basic configurations which are added automatically.

Things in blue rectangle is the part we need to add by ourselves.


3. Context: component-scan

Sometimes we use component-scan more often because it contains all functions provided in annotation-config.




4. Classification of Annotation

We can divide Annotation into  two  types:

  • Use bean :  

@Autowired

@Resource

...

  • Register bean : 

@Bean

@Component

@Repository

@Service

@Configuration

@Controller

...


I find some definitions & examples in the official API (I have made some simplification) which you can see below, and the whole API is here:  Spring 5.0.2 API



  • @Autowired

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

  • Constructors

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

  • Setter methods

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

  • Fields

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    private MovieCatalog movieCatalog;

    // ...
}



  • @Bean

@Bean Methods in @Configuration Classes

Typically, @Bean methods are declared within @Configuration classes. In this case, bean methods may reference other @Bean methods in the same class by calling them directly. This ensures that references between beans are strongly typed and navigable. Such so-called 'inter-bean references' are guaranteed to respect scoping and AOP semantics, just like getBean() lookups would. These are the semantics known from the original 'Spring JavaConfig' project which require CGLIB subclassing of each such configuration class at runtime.

 As a consequence, @Configuration classes and their factory methods must not be marked as final or private in this mode. 

For example:

 @Configuration
 public class AppConfig {

     @Bean
     public FooService fooService() {
         return new FooService(fooRepository());
     }

     @Bean
     public FooRepository fooRepository() {
         return new JdbcFooRepository(dataSource());
     }

     // ...
 }


@Bean Lite Mode

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain oldclass. In such cases, a @Bean method will get processed in a so-called 'lite' mode. In contrast to the semantics for bean methods in @Configuration classes, 'inter-bean references' are not supported in lite mode. 

For example:

 @Component
 public class Calculator {
     public int sum(int a, int b) {
         return a+b;
     }

     @Bean
     public MyBean myBean() {
         return new MyBean();
     }
 }



  • @Component

The @Component  is a universal annotation which could be used on any bean.



Then, we can get it directly through "getBean()".



Notice the red part. It is the id  of this bean. But we have not specified any id  for this bean, all we have done is adding "@Component" to this bean. Under this circumstance, Spring will assign a default id to this bean which only changes the first letter of the class name from uppercase to lowercase. For example, the class name is "Bookstore", and the default id will be "bookstore". 

Of course, we can specify an id .






  • @Required

The @Required  annotation applies to bean property setter methods. This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. It is not widely used. Here is an example from official reference:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Required
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

  • @Repository

Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects".


参考与引用: Official Reference
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值