The @[toc] Spring annotations @Component, @Controller, @Service, and @repository are put together for a simple reason. There is nothing fundamentally different between the four annotations, except that the last three are related to some naming conventions. The naming conventions here are like apples called apples, you don’t say apples are pineapples. The benefits are obvious. When you hear someone say apple, you know it’s an apple, not a pineapple. At sign Controller you know this is a Controller presentation layer, at sign Service means this is a Service business layer.

1. The premise of using these four notes

Import the Context constraint and tell Spring to scan the package when creating the container that the annotations must be in its scope

No bean

2. @Component

“Component” translates to “Component.” It reads: “To resist breaking.”

2.1. @Component function

Previous XML configuration:

  <bean id="XXX" class="com.XX.XXX </bean>
Copy the code

The @Component annotation does the same thing as writing a < bean > tag in an XML configuration file to store the current class object in the Spring container.

2.2. @Component property

The @Component attribute mainly refers to the value attribute. The value attribute is used to specify the ID of the bean. When we don’t write, it defaults to lowercase for the current class name. Of course, normally you don’t. Such as:

2.3. @Component Summary

Attribute: value specifies the id of the bean. When we do not write, it defaults to the name of the current class with the first letter lowercase. Note: There is no default value= when there is only one attribute value

@service (“XXX”) or @service (value = “XXX”

Of course, do not rule out the occurrence of similar strange situation, may appear in a strange demand on this situation, obviously Yichun is encountered, of course, if the reader friends have understood what I wrote above, estimate this is very easy to understand!

This is a Service business layer class, and the registered bean has the id XXX. If your Spring container does not have a component with id XXX, you will No doubt run an exception specifying No bean named ‘YYY’. Note that YYY is not XXX.

4, summarize

As for @Controller, @Service, and @Repository, the Spring framework provides us with clear annotations for the use of the third layer, which makes our third layer objects clearer!

In fact, there is no essential difference between these four notes, if you like, you can use these four notes as one note, of course, the boss may not like you.

If this article helped you a little bit, please give it a thumbs up. Your approval is my biggest motivation. Thank you

Finally, if there is insufficient or improper place, welcome to criticize, grateful! If you have any questions welcome to leave a message, absolutely the first time reply!

Welcome to pay attention to my public number, there are some Java learning materials and a large wave of Java electronic books, such as Zhou Zhiming teacher’s in-depth Java virtual machine, Java programming thought, core technology volume, big talk design mode, Java concurrent programming combat….. Are Java bible, don’t say fast on the Tomcat car, how we go! The most important is to discuss technology together, yearning for technology, the pursuit of technology, good come is a friend oh…