Spring annotation resolves auto-injection in attributes

Source: Internet
Author: User

@Autowired

Spring 2.5 introduces @Autowired annotations that allow you to annotate class member variables , methods , and constructors , and complete the task of automating assembly . Use the @Autowired to eliminate the set, get method .

Add to the member variable to omit the Get set method.

But first, you have to configure Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in the spring configuration file now.

@Resource Annotations are used to activate the dependency injection of a named resource (named Resource), which is typically converted to an object that is bound to a Jndi context in a Java EE application. Spring does support using @Resource to parse objects through Jndi lookup, by default, with a "bean name" that matches the name provided by the @Resource annotation The spring management object will be injected.

@Resource (name= "DataSource")
Public void Setdatasource (DataSource DataSource) {
this. DataSource = DataSource;
}
Spring passes a reference to the annotated setter method to the spring management object named "DataSource" by the bean.
Private DataSource DataSource;
@Resource
Public void Setdatasource (DataSource DataSource) {
this. DataSource = DataSource;
}
It is also possible to use @Resource annotations directly on a field (field). By not exposing the setter method, the code becomes more compact and provides additional benefits that the domain cannot modify. As will be shown below,@Resource annotations do not even require an explicit string value, and if no value is provided, the domain name will be treated as a default value.
@Resource
Private DataSource DataSource; //Inject the bean named ' DataSource '

Spring annotation resolves auto-injection in attributes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.