How to eagerly inject the authentication manager of spring security - spring

I used spring security. And, I have made below settings for my spring boot project.
spring:
main:
lazy-initialization: true
So, All spring beans are lazy initialization.
In this situation, I want to early inject only the authentication manager.
What should I do?

I think you can use #Lazy(value = false) in your #Component that uses it. As per docs:
If this annotation is not present on a #Component or #Bean definition, eager initialization will occur. If present and set to true, the #Bean or #Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.

Related

How to initialize the dataSource, transactionManager configuration beans later after the server startup?

I am aware of initializing the dataSource and transactionManager beans. But we have a requirement where at the time of server startup, database may not be available so we don't want to initialize these beans at the time of server startup otherwise we used to see the exception in logs.
We are using #Configuration, #EnableJpaRepositories annotation for managing the persistence context.
Can we achieve such kind of configuration in Spring where we want to initialize the dataSource bean at the time of first API request i.e. lazily?
If you are using Spring boot, can exclude as below
#EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})

Spring - what's a bean and what's not?

I'm new to Spring and I'm confused about something basic. Are the classes that are stereotyped (Service, Controller, Repository) treated as beans? I'm confused as to when you actually need to annotate/configure something as a bean and when you don't. Is it for the classes that aren't stereotyped?
Thanks!
From spring documentation:
In Spring, the objects that form the backbone of your application and
that are managed by the Spring IoC container are called beans. A bean
is an object that is instantiated, assembled, and otherwise managed by
a Spring IoC container. Otherwise, a bean is simply one of many
objects in your application. Beans, and the dependencies among them,
are reflected in the configuration metadata used by a container.
Service, Controller, Repository are managed by the Spring IoC container, so they are called beans. You annotate a class as #Serivice, #Controller, #Repository, or more in general #Component when you want spring to manage it: spring will manage the instance of annotated class in regard of the scope you select (not all these scope are always available):
singleton – Return a single bean instance per Spring IoC container
prototype – Return a new bean instance each time when requested
request – Return a single bean instance per HTTP request
session – Return a single bean instance per HTTP session
globalSession – Return a single bean instance per global HTTP
session

When annotating a class with #Component, does this mean it is a Spring Bean and Singleton?

Being fairly new to Spring I have a question about annotating a class. When annotating a class with #Component does this mean this class will be a Spring Bean and by default a singleton?
Yes, that is correct, #Component is a Spring bean and a Singleton.
If the class belongs to the service layer you may want to annotate it with #Service instead
But have in mind that in order for these annotations to be detected, you need to place this line in applicationContext.xml:
<context:component-scan base-package="com.yourcompany" />
About singletons - spring beans are all in singleton scope by default. The only thing you have to have in mind is that you should not store state in field variables (they should only hold dependencies). Thus your application will be thread-safe, and you won't require a new instance of a bean each time. In other words, your beans are stateless.
By default - Yes.
However, you can override this behavior using the #Scope annotation. For example: #Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Spring Standard Bean Injection vs. Autowiring

As far as I understand When Using Dependency Injection all bean are initializing on Start.
<bean id="userPreferences" class="com.foo.UserPreferences">
</bean>
<!-- a singleton-scoped bean injected to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the userPreferences bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
and the configuration above means that userService and userPreferences created when application starts. Is it correct?
When using Autowiring and using <context:component-scan>
public class SimpleUserService{
#Autowired
UserPreferences userPreferences;
//omitted
}
1) Is userPreference created on Application init?
2) What is the default scope for bean injected by autowire and how can we change it?
3) How affects bean creation and bean injection?
Hope I made myself clear.
First of all you should add #Service or #Component to the SimpleUserService class.
1 Yes, the ONE instance of UserPreferences is created at application intialization
2 Default scope is singleton, You can change it with the #Scope annotation (#See Spring Reference: 3.11.4.4 Specifying bean scope)
3 Component scan and XML configuration work in the same way (life cycle)
Maybe you should spend some time in understanding the Spring life cycle. You need to understand that Spring works a bit in this way (not 100% correct):
first it creates a pool of beans
then it injects the properties into the beans
But it does NOT work this way: taking a class, look what references it needs creating this references (recursive) and then creating the class.
If you understand this, then you will also understand, that the #Scope of a bean is defined at the bean declaration/class, but not at the references.
1) Is userPreference created on
Application init?
In either case userPreferences is initialized when Spring Context is loaded. You can change this behavior by adding lazy-init="true" to the bean configuration.
2) What is the default scope for bean
injected by autowire and how can we
change it?
The scope of what is injected is all beans loaded into Spring. If you import an XML configuration from another project, it too would be included. I'm not sure if you can limit your scope.
3) How affects bean creation and bean
injection?
Whether is autowired, or configured via XML, the behavior should be the same. I prefer explicitly defining dependencies over automatic annotations. Then again I also like strongly typed languages.
the configuration above means that userService and userPreferences created when application starts. Is it correct?
Yes
Is userPreference created on Application init?
Yes
What is the default scope for bean injected by autowire and how can we change it?
The default scope is always "singleton". This can be changed either using #Scope with #Bean or the scope XML attribute on <bean>.
How affects bean creation and bean injection?
This isn't a clear question. If you change the bean scope, you change when it gets created (start of application, on each request, on each session, etc). The wiring configuration remains the same, only the lifecycle changes.
The #autowired notation is an obsolete way to say #inject. THe latter is a feature of JavaEE 6.
stackoverflow.com/questions/7142622/what-is-the-difference-between-inject-and-autowired-in-spring-framework-which

how to specify a bean as non lazy with annotations

Does anyone know how to specify a bean as non lazy when using annotations to configure the bean?
In spring 3.0 there is an annotation: #Lazy(false). But note that beans are eager by default.
Beans are not lazy by default. However as far as annotations are concerned it seems like currently annotations do not support it.
http://forum.springsource.org/showthread.php?t=62931
Spring's next version though seem to have something in store
http://jira.springframework.org/browse/SJC-263
Just to set things straight, be known that as to Spring 3.0 and later, beans are by default eagerly initialized.
Excerpt from the #Lazy(false) link in Bozho's answer:
If this annotation is not present on a Component or Bean definition,
eager initialization will occur. If present and set to true, the
Bean/Component will not be initialized until referenced by another
bean or explicitly retrieved from the enclosing BeanFactory. If
present and set to false, the bean will be instantiated on startup by
bean factories that perform eager initialization of singletons.
I tried #EnableScheduling in my Configuration Annotation class and that did the trick.

Resources