In addition to the @Component annotation, Spring 2.5 defines several annotations with special semantics: @repository, @Service, and @Controller. In the current Spring version, these three annotations are equivalent to @Component, but from the naming of the annotation class, it’s easy to see that they correspond to the persistence layer, the business layer, and the control layer (The Web layer), respectively.
@service is used to annotate business layer components
@Controller is used to annotate control-layer components (such as actions in Struts)
@Repository is used to annotate data access components, known as DAO components
@Component refers to a Component. This annotation is used when a Component is difficult to categorize
By using the @Service, @Controller, @Repository, and @Component annotations on the class, Spring automatically creates the appropriate BeanDefinition object and registers it with the ApplicationContext. These classes become Spring managed components. These annotations are used in exactly the same way as @Repository, except that they apply to classes at different software levels.
When a Bean is automatically detected, its Bean name is generated according to that scanner’s BeanNameGenerator policy. By default, @Component, @repository, @Service, and @Controller with the name attribute are given the name of the Bean. If the annotation does not contain a name value or other component found by the custom filter, the default Bean name will Bean unqualified class name starting with lower case. If you do not want to use the default bean naming policy, you can provide a custom naming policy. First implement the BeanNameGenerator interface and verify that a default no-argument constructor is included. You then provide a fully qualified class name when configuring the scanner.
@Service public class UserServiceImpl implements UserService { } @Controller public class UserController { } @Repository public class UserDaoImpl implements UserDao { } @Component public class UserUtil { }Copy the code
Injection pattern
Inject the DAO implementation class into the service interface of the action. Do not inject the new class. Spring will inject the DAO implementation class automatically. Then the @autoWired property does not require getter() and setter() methods, and Spring will inject them automatically.
The @autowired annotation precedes the interface so that it can be injected by the container, as in:
@Autowired
@Qualifier("cn")
private User user;
Copy the code
When an interface has two implementation classes, you must use @qualifier to specify which implementation class to inject, otherwise you can omit it and just write @AutoWired.
V Source code address
Github.com/toutouge/ja…
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \