Which #controller annotation should I be using with Spring-mvc? - java

org.springframework.stereotype;
or
org.springframework.web.servlet.mvc;
I had been using the stereotype but just noticed I have another option ?

org.springframework.stereotype.Controller
Is an annotation. It indicates that the annotated class is a controller as well as a candidate for auto-detection (like a #Component). This is the annotation you want to use.
org.springframework.web.servlet.mvc.Controller
Is an interface for implementing controllers. In most simple cases you probably don't want to be using this.

Related

Is it possible use #Service/#Component instead of #Controller annotation SpringMVС?

Is it possible use #Service/#Component instead of #Controller annotation in Spring MVC
( I checked this out and it doesn't work) but why?
The #Controller annotation is a specific type that, like all spring annotations, is derived from the #Component annotation. Note that, depending on use case, it may work, and it may not. Controller classes are often used for mapping server requests to responses.
You can view this Geeks for Geeks article for more info on when to use the #Controller annotation. If you have #RequestMapping annotations in your class, you will not get the same behavior with a Component as you will with Controller.

#RolesAllowed interfering with #RequestMapping

In a spring boot app, using annotations only, i want to implement security
I have added an #EnableGlobalMethodSecurity(jsr360Enabled=true) to a configuration class. the class also has an #EnableSecurity anntation
Now, when i add a #RolesAllowed to any #RestController class, be it on method level or level, the startup logs don't list the class at all. instead , there is line:
'Rejected bean name (rest controller class): no URL paths identified'.
Does anyone have an idea what might be causing this?
After #M.Deinum set me on the path, i did some reading and have an explanation. If anyone can elaborate more or correct me on the more technical details, feel very welcome.
Spring proxies classes to wire beans together.
Classes that don't implement an interface are proxied by using CGLib to make a subclass with additional functionality
classes that do implement an interface, spring uses dynamic proxy to access the functionality of the class, but doing so it can only proxy interface methods.
in my case, my controller implements an interface with non-endpoint related methods, so spring ignored all handling methods and the RequestMappingHandlerMapping did not find any methods to bind, because if only 'saw' the interface methods on the class
using #EnableGlobalMethodSecurity(proxyTargetClass=true) forces the use of CGLib, so the full methods are recognised by our mapper.

How make a controller use a specific #ControllerAdvice and not another?

I want to have multiple implementations of #ControllerAdvice for different types of controllers. How do I force a controller to use only the one I want?
I can not use #ControllerAdvice( assignableTypes = MyController.class ) because I am putting the controller advice in a jar that will be pulled in as a dependency and users will create their controllers later.
You can define a specific or list of annotations in your Jar, then in controllerAdvice you can use "annotations" declaration to control the operations. And Controllers that are annotated with this/one of those annotation(s) declared in #ControlledAdvice will be assisted by the #ControllerAdvice annotated class.

What will happen if we interchange #service and #repository annotation in the spring MVC

Why we needs to use #service inside the service Implementation and #repository in the DAO Implementation. There are no problem occur when I interchange the #service and #repository annotation in the spring MVC.
According to documentaion #Repository,#Service,#Controller are all synonyms. They all are just specializations of #Component annotation. So, generally, they can be used one instead of other. But ... you should not do this.
First reason: any of these annotations make clear the role of your component in the application. Shows - is this component belongs to the controller, service, or data layer.
Second reason: some of these annotations processed differently by different Spring modules. For example, Spring Data JPA will process #Repository and will try to replace with implementation any interface marked by this annotation. Spring also will apply automatic exception translation to such classes. Another example: Spring Web MVC processes #Controller, and uses classes marked with it in URL mappings.
Actually, in future versions, some modules of Spring could process #Service in a particular way. Not as simple #Component. That's why documentation advises:
It is also possible that #Repository, #Service, and #Controller may
carry additional semantics in future releases of the Spring Framework.
Thus, if you are choosing between using #Component or #Service for
your service layer, #Service is clearly the better choice.
It depends on what you use for the remainder of the framework. In theory nothing changes as the #Service and #Repository annotations are basically #Component annotations. The same could be said for #Controller or #Endpoint (for Spring Ws and there are more).
However they express an intent of what a class is (a service, a repository) and makes it clear to the user to what layer that class belongs.
However if you also use Spring for transaction managemnt then #Repository is also a trigger for adding exception translation to that class (also see the reference guide).
Although nothing has to break it probably will at some point.

what are #Repository and #Autowired used for. (Spring)

I am learning java for 3 months and sometimes
i can not understand the usage purpose of something.
one topic was dependency injection and spring beans i figured out the finally =)
now i confused with the two annotations #Autowired and #Repository.
First What does Autowiring mean? then
Why should i use them and what is the difference between using them and not using?
Also today i tried to use hibernate in a spring mvc project and i had to search for about 15(cause of class not found errors) jar files beacuse of the dependencies of other jar files used in the project.
is this had to be this way? this makes learning java very hard for the beginners
thanks...
#Repository is an annotation that marks the specific class as a Data Access Object, thus clarifying it's role. Other markers of the same category are #Service and #Controller
#Autowired is an annotation with a completely different meaning: it basically tells the DI container to inject a dependency. More info at http://apollo89.com/java/spring-framework-2.5.3/api/org/springframework/beans/factory/annotation/Autowired.html
Edit
More info at tutorialpoint
or docs.spring.io
Both the annotations have different purposes to be used.
#Autowired: This is same as <bean="xyz" autowire="byType"> you define in the configuration file. The reference variable (dependency) that is annotated with #Autowired, will be injected by Spring container as any matching #Bean found in #Configuration class.
Plus the classes annotated with #Component, #Service, #Repository are too considered as beans so their objects are injected into the matching dependencies.
Spring container scans the beans in the classes you mentioned for "component-scan" or #ComponentScan("xyz").
#Repository: This is also a spring-framework's annotation. When you annotate a class #Repository, spring container understands it's a DAO class and translates all unchecked exceptions (thrown from DAO methods) into Spring DataAccessException.
DAO class is the class where you write methods to perform operations over db.
#Autowired and #Repository are very 2 different concepts.
1.# Repository: This define a class to be a repository, In general term you can use simply #Component but to define specifically, there are 3 more annotations like Controller,service and repository.Mainly 2 advantages:
1.If you have defined(context:component-scan)in servlet.xml to scan the defined package and find its own by spring.
2. More advantages you get from spring like database access error translation, so it is mainly defined to use with class in which you are connecting with database either with hibernate or jdbc.
#Autowired: to inject dependency at run-time by spring, means in a class, autowire a object ,and use it ,so this bean will automatically be made without defining in xml file

Categories

Resources