Detailed spring 3.0 based on annotation Dependency injection implementation

Source: Internet
Author: User

Introduction: Spring's dependency configuration is loosely coupled to the kernel itself of the spring framework. However, until Spring 3.0, using XML for dependency configuration is almost the only option. The advent of Spring 3.0 has changed this situation by providing a series of annotations to dependency injection, which makes the spring IoC a viable alternative to XML files. This article describes in detail how to use these annotations for dependency configuration management.

Identify a class as a Bean using @Repository, @Service, @Controller, and @Component

Spring has been introduced since version 2.0 to simplify the development of spring. @Repository annotations are among the first to be introduced to identify the classes of the data Access layer (DAO layer) as Spring beans. Simply label the annotation on the DAO class. Also, in order for Spring to be able to scan classes in Classpath and identify @Repository annotations, you need to enable the automatic scanning of beans in the XML configuration file, which can be achieved by <context:component-scan/>. As shown below:

// 首先使用 @Repository 将 DAO 类声明为 Bean
  package bookstore.dao;
  @Repository
  public class UserDaoImpl implements UserDao{ …… }

  // 其次,在 XML 配置文件中启动 Spring 的自动扫描功能
  <beans … >
   ……
  <context:component-scan base-package=”bookstore.dao” />
……
  </beans>

In this way, we no longer need to explicitly use <bean/> for bean configuration in XML. Spring automatically scans for all class files under the specified package and its base-package when the container is initialized, and all classes that are annotated @Repository are registered as spring beans.

Why can @Repository only be labeled on DAO classes? This is because the annotation does not simply recognize the class as a Bean, but it also encapsulates the data access exception that is thrown in the annotated class as the data access exception type for Spring. Spring itself provides a rich and independent data access exception structure that encapsulates the exceptions thrown by different persistence layer frameworks, leaving the exception to the underlying framework.

Spring 2.5 Adds additional three annotations with similar functionality on a @Repository basis: @Component, @Service, @Constroller, which are used at different levels of the software system:

@Component is a generalization concept that simply represents a component (Bean) that can function at any level.

@Service usually works in the business layer, but this feature is the same as @Component.

@Constroller usually works on the control layer, but this function is the same as @Component.

By using @Repository, @Component, @Service, and @Constroller annotations on a class, Spring automatically creates the appropriate Beandefinition object and registers it with the ApplicationContext. These classes become Spring-managed components. These three annotations are used in exactly the same way as @Repository, except for classes that function at different levels of software.

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.