1

Below is source code of For Component Interface

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {

and for Spring Controller annotation is as below

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {

Why @Component is added in Controller annotation ? What is its purpose ? What if I remove this line ?

I am clear about below annotation types which are used to create custom annotations.

@Documented Whether to put the annotation in Javadocs
@Retention  When the annotation is needed
@Target     Places the annotation can go    
@Inherited  Whether subclasses get the annotation
3
  • 1
    @Controller is a @Component. The @Component is used as a meta-annotation here so that it can be picked-up using component-scanning. The @Controller is a special component which will have some added functionality. If you remove the @Component annotation component-scan will not detect it anymore. You can also create your own @Component based annotations. Commented Nov 11, 2013 at 17:06
  • @M.Deinum: you should poast that as an answer. Commented Nov 11, 2013 at 17:09
  • ok in this case @Component was need for component-scanning. Similarly if we take some other example of custom annotation. which has some parameters also , how will that behave(while using with meta-annotations like @Component)? One option is we can write our annotation processor to read that. My doubt is that "is there any other behavior also comes up with this ?" if not then without writing annotation processor it should be meaningless Commented Nov 11, 2013 at 17:40

2 Answers 2

4

@Controller is a @Component (just like @Service, @Repository, @Endpoint etc.).

The @Component is used as a meta-annotation here so that it can be picked-up using component-scanning. Next to that the @Controller is a special component which will have some added functionality (Spring MVC takes care of that). If you remove the @Component annotation component-scan will not be able to detect it anymore.

You can also create your own @Component based annotations by simply creating your own annotation and putting @Component on it.

Sign up to request clarification or add additional context in comments.

2 Comments

ok in this case @Component was need for component-scanning. Similarly if we take some other example of custom annotation. which has some parameters also , how will that behave(while using with meta-annotations like @Component)? One option is we can write our annotation processor to read that. My doubt is that "is there any other behavior also comes up with this ?" if not then without writing annotation processor it should be meaningless.
An annotation alone doesn't do a thing, there needs to be something that processes it. An annotation is nothing more then meta-data...
1

@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Read this answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.