Spring auto configuration for #ControllerAdvice - spring

I'm writting custom starter for global exception handler class annotated with #ControllerAdvice. I can't put this class into configuration class without need of abstract class, but I've seen example where you leave exception handler in it's own class and put it into spring.factories file, but I don't understand how does spring picks it up since in spring docs it is mentioned that spring.factories should contain classes annotated with #Configuration?
Example here:
#ControllerAdvice
#ConditionalOnMissingBean(annotation = [ControllerAdvice::class])
class ExceptionHandler {
...
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
test.configuration.controller.ExceptionHandler
Does it actually work because of this: #ConditionalOnMissingBean(annotation = [ControllerAdvice::class]) and adding it to factories is redundant?

Related

Using #PropertySouce in #Component

I tried to use #PropertySource in a #Component like:
#Component
#PropertySource("somepropertiesfile.properties")
public class Student {
...
}
It worked fine.
I want to understand, what is the different between using #PropertySource with #Component and #PropertySource with #Configuration.
Is there any difference or impact of using #PropertySource with #Component.
Configuration is itself a Component type, look into the #Configuration annotation implementation below.
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Configuration {
}
From API
Component: Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Configuration: Indicates that a class declares one or more #Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
The #Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container. These are same as Spring’s XML configuration. You can use #Bean annotated methods with any Spring #Component, however, they are most often used with #Configuration beans.
Here also you can use #PropertySource in #Component class but these are most suitable for #Configuration classes as it is a configuration related task.
You can refer Doc for detailed information.

How SpringBoot dependency injection works with different type of annotations

I recently started exploring Spring Boot. I see that there are 2 ways to define Beans in Spring Boot.
Define #Bean in the class annotated with #SprinBootApplication
Define #Bean in a class annotated with #Configuration
I am also confused about stereo-type annotation #Repository #Service #Controller etc.
Can someone please explain how dependency-injection works with these annotations?
Yes it is possible.
Either you use #Bean in any of your #Configuration or #SpringBootApplication class or mark the bean classes explicitly with annotations like #Service, #Component #Repository etc.
#Service or #Component
When you mark a class with #Service or #Compoenent and if spring's annotation scanning scope allows it to reach to the package, spring will register the instances of those classes as spring beans.
You can provide the packages to be included/excluded during scan with #ComponentScan
#Bean
#Beans are marked on factory methods which can create an instance of a particular class.
#Bean
public Account getAccount(){
return new DailyAccount();
}
Now in you application you can simply #Autowire Account and spring will internally call its factory method getAccount, which in turn returns an instance of DailyAccount.
There is a simple difference of using #Bean vs #Service or #Compoenent.
The first one makes your beans loosely coupled to each other.
In the #Bean, you have flexibility to change the account implementation without even changing any of the account classes.
Consider if your classes instantiation is a multi-step operation like read properties values etc then you can easily do it in your #Bean method.
#Bean also helps if you don't have source code access to the class you are trying to instantiate.
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
You need to opt-in to auto-configuration by adding the #EnableAutoConfiguration or #SpringBootApplication annotations to one of your #Configuration classes.
You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies. For simplicity, we often find that using #ComponentScan (to find your beans) and using #Autowired (to do constructor injection) works well.
One way is to define #Bean in the class annotated with
#SprinBootApplication
If you see #SprinBootApplication it is combination of many annotation, and one of them is #Configuration. So when you define #Bean in the Main class, it means it's inside #Configuration class.
According to Configuration docs :
Indicates that a class declares one or more #Bean methods and may be
processed by the Spring container to generate bean definitions and
service requests for those beans at runtime.
class annotated with #Configuration
When you define #Bean is a class annotated with #Configuration class, it means it is the part of spring configuration all the Beans define in it all available for Dependency-Injection.
I have also seen some code where neither of the 2 above approaches
have been used and yet dependency injection works fine. I have tried
to research a lot on this but could not find any concrete answer to
this. Is this possible?
I am assuming you are talking about Sterio-type annotation. Every sterio type annotation has #Component, according to docs :
Indicates that an annotated class is a "component". Such classes are
considered as candidates for auto-detection when using
annotation-based configuration and classpath scanning.

spring-aop "None or multiple beans found in Spring context for type class"

I am not able to apply an aspect to my spring rest endpoint components for logging purposes.
All of endpoint classes are implemented like
#Component
#Path("mypath")
public class MyEndpointImpl extends MyEndpoint
{...}
Without aspect everything works fine without any errors. When I try to apply aspect I just get list of errors for each endpoint class like "None or multiple beans found in Spring context for type class **.*EndpointImpl" and no aspect is intercepting endpoints' methods. However everything works fine as if there were no error message and no aspect.
Interesting is when I create e.g. simple filter
#Component
#WebFilter(filterName = "MySimpleFilter", urlPatterns = "/*")
public class SimpleFilter implements javax.servlet.Filter
{...}
in package of pointcut, doFilter method of SimpleFilter is intercepted by the aspect as would expect for all endpoint methods.
What could be a problem here, any ideas?
In my environment the Problem disappeared when adding the following to the application.yml file
spring.aop.proxy-target-class: true

How does SimpleCORSFilter work?

How does SimpleCORSFilter work in this example?
Enabling Cross Origin Requests for a RESTful Web Service.
I only see a declaration of SimpleCORSFilter class but no instance. I tried ctrl+f to search the example page but can't find anywhere this class be instantiated.
How does it work?
I am new to Spring and Java.
So more detail more helpful. Thx.
A main point of Spring is a mechanism called dependency injection. Spring allows you to mark your classes, instance variables and so on with special annotations. Spring will look for those annotations and configure your application according to them.
In your example you annotate your filter with #Component:
#Component
public class SimpleCORSFilter implements Filter
And you annotate your Application class with #SpringBootApplication:
#SpringBootApplication
public class Application
The second annotation (#SpringBootApplication) tells Spring to search through your project for #Component annotations. As you annotated your filter with this, Spring will find your filter and instantiate it automatically. That's how your filter will be created and put to the right place.

Spring MVC #Configuration class constructor

As part of the Spring MVC initialization, I need to run an action (just calling a method on a 3rd party library) once as a setup operation. I'm working in Spring MVC environment where I don't really have control over the web.xml or anything, so I can't add a servlet context listener or anything. I tried making an implementation of a WebApplicationInitializer but it never seems to get called (no idea why though, or how to try and debug that any further).
If I annotate a class with #Configuration it does get created, so I'm wondering if I can use the class's constructor to perform that setup operation (calling a 3rd party setup method). Is this appropriate/safe to do? Are there any other alternatives for this kind of thing? I'm new to Spring, so I might just be missing something that's meant for this kind of thing.
Thanks
Configuration class would be an appropriate place to contain some initialization logic. You can place it in a constructor, method annotated with #PostConstruct or afterPropertiesSet() method if you implement the InitializingBean interface for example. The difference is that the constructor code will be called before the beans in your configuration class are instantiated, so if your initialization code depends on some Spring beans, go with the #PostConstruct / InitializingBean approach.
Example:
#Configuration
public class Config {
#PostConstruct
public void initialize() {
// Run some action
}
}

Resources