Notes on "58" Spring Summary (2)

Source: Internet
Author: User

Enable annotation configuration: 1.context:annotation-config

This configuration indicates that annotations are enabled.

2.context:component-scan base-package= " .

This configuration represents the annotations under which packages are scanned, which implicitly registers multiple processors that parse the annotations, including the processors used by annotations inside the configuration items. Therefore, after configuration, no more configuration is necessary.

Annotation classes: @Component, @Repository, @Service, @Controller,

@Service: Used to label business layer components.
@Repository: Used to label data access Components, DAO components.
@Controller: Used to label control-level components, such as action in struts.
@Component: A generic component that can be annotated with this annotation when the component is not well categorized.

Autowired and resource recommend the use of resource

@Autowired can label member variables, methods, and constructors to accomplish the task of automating Assembly.

Implementation of the class1--Label the member variable: Public  class Userserviceimpl implements iuserservice {    @Autowired PrivateUserdao Userdao, ...} Implementation of the class2--Labeling The method: Public  class Userserviceimpl implements iuserservice {    PrivateUserdao Userdao;@Autowired      Public void Setuserdao(Userdao Userdao) { This. Userdao = Userdao; }}
Required,qualifier and Autowrite mate Autowrite are assembled by default by type

If there is more than one Userdao type of bean in the spring context, a Beancreationexception exception is thrown, and if the Userdao type of bean does not exist in the spring context, Beancreationexception exceptions are also thrown. We can use @qualifier with @autowired to solve these problems.

1) Multiple Userdao instances may exist
Java代码:    @Autowired    publicvoidsetUserDao(@Qualifier("userDao") UserDao userDao) {       this.userDao = userDao;    }这样,Spring会找到id为userDao的bean进行装配。
2) Userdao instances may not exist
Java代码:    @Autowiredfalse)    publicvoidsetUserDao(UserDao userDao) {       this.userDao = userDao;    }
Resource

The function of the @Resource is equivalent to @autowired, but @autowired is automatically injected by Bytype, and @resource is automatically injected by ByName by default. It is important to @Resource that there are two properties, namely name and type,spring, which resolves the name property of the @resource annotation to the bean, while the type attribute resolves to the bean.

@Resource Assembly Order

If name and type are specified at the same time, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found;

If name is specified, a bean matching the name (ID) from the context is assembled, and an exception is thrown if it is not found;

If type is specified, an exception is thrown when a unique bean of type matching is found in the context, cannot be found, or multiple occurrences are found;

If neither name is specified and type is not specified, the assembly is automatically byname (see 2), and if there is no match, the fallback is matched to an original type (Userdao) and automatically assembled if matched.

Reference:
http://jiaxiaoyuan1204.blog.163.com/blog/static/65553152011350147451/
http://yedward.net/?id=339

My QR code is as follows, welcome to exchange discussion

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

Notes on "58" Spring Summary (2)

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.