In spring when to use new keyword and autowired keyword - spring

i am new to spring. i am confused when to use autowired keyword and new keyword.while using the model class in spring we use new keyword rather than Autowired keyword

The new keyword allocated new memory space in the heap for the object. Then the constructor will initialize the state of the object (properties of the object).
The #Autowired asks Spring framework to inject the requests bean by type.
Now, you have built an application which has a controller, service and dao layer. These layers basically doesn't hold state of the application/business data. They wrap functionalities. Business logic/Logic to talk to db , etc. So from Service when you want to call a save(entity) in dao layer you will do -> new DAO().save(entity)...... This is a wastage of memory as the you creating new Object everytime just to use the functionality. An efficient way to do so is -> Create singleton Service instance -> one instance for entire application and then inject them in other places to use the functionality.
Spring handles that for you.. #Service/#REstController/#Component basically tells Spring to create an instance of that class for you - default scope is Singleton. And wherever you mention -> #Autowired, it injects that bean (by type) to the requested class for you to use the functionality.
This is basically inversion of control. Way I keep this is mind is handling the responsibility/control of creating instances and injecting them in places you need is inversed from you to the framework. And also you removing the strong coupling by injecting by type (interface generally) and spring finds the impl of the interface and injects it whereevr you mentioned #Autowired. In case there are multiple impls you need to name each impl (or default name is the class name with firstLetter small case - please confirm this once cause I dont remember correctly) and use #Qualified(name) to mention to spring which impl you want to inject.
Now your model/entity -> These are basically Java beans -> having properties and accesor methods to access those properties. These will hold state of your application. So every time a new User registers you need a new User object to pass around the layers to perform some logic and then save to the DB. So you will create a new Object with new keyword.

The typical use of #Autowire is to automatically fill a property, when initializing a bean, with a singleton dependency. If it is really a dependency that should be initialized once and reused.

to answer your question how to check scope of bean, there is ConfigurableApplicationContext which sub class of ApplicationContext, which again is the subclass of BeanFactory. There is a method on configurableApplicationContext::getBeanFactory which returns you the BeanFactory. You can invoke various methods on it to check the scope of various beans.
Here is the code snippet I wrote:
#Component
public class ScopeTestService {
#Autowired
private ConfigurableApplicationContext applicationContext;
public void testScope() {
String scope = applicationContext.getBeanFactory().getBeanDefinition("scopeTestService").getScope();
System.out.println(scope);
}
}
You can person various permutation combinations to check. You can change using #Scope("scope type") annotation along with #Component.
The result for the above code:
2020-01-29 02:24:56.125 INFO 57500 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-01-29 02:24:57.604 INFO 57500 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-01-29 02:24:58.066 INFO 57500 --- [ main] c.example.demo.JpaTutorialsApplication : Started JpaTutorialsApplication in 7.023 seconds (JVM running for 7.837)
singleton
2020-01-29 02:24:58.077 INFO 57500 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-01-29 02:24:58.082 INFO 57500 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-01-29 02:24:58.098 INFO 57500 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

Related

Injecting an entitymanager in #ApplicationScoped bean

I am porting an existing JBOSS JEE application to Quarkus. I am using a number of HV custom validators that require injection.
For that purpose I've defined all custom validators that require injection as bean in my libraries like this:
#ApplicationScoped
public class SomeValidator implements ConstraintValidator<SomeValidation, AnObject> {
#Inject
public BeanUsingEntityManager bean;
Note: It is common code, so it should remain working on JBOSS as well
Next I defined a REST service. The REST service makes use of an application scoped bean like this.
#ApplicationScoped
public class ApplicationContext {
#PersistenceContext( unitName = "A" )
EntityManager em;
#Produces
#EnityManagerA // required qualifier to make datasource unique in JEE context (there are more)
public EntityManager produce() {
return em;
}
// NOTE: quarkus does not allow the #Produces on a field, which is allowed in JBOSS hence the method
#Produces
public BeanUsingEntityManager createBeanUsingEntityManager () {
// some logic that requires the entity manager.
}
}
Now the problem is simplified, but I keep on running into an error message.
Caused by: javax.enterprise.inject.CreationException: Synthetic bean instance for javax.persistence.EntityManager not initialized yet: javax_persistence_EntityManager_b60c51739990fc921960fc78caeb075a811a91a6
- a synthetic bean initialized during RUNTIME_INIT must not be accessed during STATIC_INIT
- RUNTIME_INIT build steps that require access to synthetic beans initialized during RUNTIME_INIT should consume the SyntheticBeansRuntimeInitBuildItem
at javax.persistence.EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.create(EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.zig:167)
at javax.persistence.EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.create(EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.zig:190)
at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
at io.quarkus.arc.impl.AbstractSharedContext.access$000(AbstractSharedContext.java:14)
at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:29)
at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:26)
at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
at javax.persistence.EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.get(EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.zig:222)
at javax.persistence.EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.get(EntityManager_e1903961aa3b05f292293ca76e991dd812f3e90e_Synthetic_Bean.zig:238)
at nl.bro.gm.gmw.dispatch.resources.ApplicationContext_Bean.create(ApplicationContext_Bean.zig:131)
... 59 more
I'm new to Quarkus. So, not sure to how to handle this issue or even if I make the correct assumptions. I can imagine that Quarkus wants to give me a fresh entitymanager each request (which I understand), but that poses a problem for my application scoped beans.
What am I doing wrong here?
So, the full answer is that the EntityManager is created at the runtime init phase whereas the ValidatorFactory (and the ConstraintValidators) are created at static init time.
The Quarkus bootstrap goes static init -> runtime init.
So in your case, you can't access a #Singleton bean which uses the EntityManager during static init as it's not yet available.
Making your bean #ApplicationScoped will create a proxy and avoid this chicken and egg problem.
You will have only one BeanUsingEntityManager for your whole application.
The EntityManager is a bit different because we wrap it and you will get one new EntityManager/Session per transaction, which is what is expected as EntityManagers/Sessions are not thread safe.

Spring - register Beans before everything else

I need to register a series of BeanDefinition(s) before every other Bean gets created. That's because those registered Bean(s) are needed for autowiring and ApplicationContext#getBean calls.
I cannot use #DependsOn, obviously.
Example:
final var beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(...);
beanDefinition.setLazyInit(true);
beanDefinition.setAbstract(false);
beanDefinition.setAutowireCandidate(true);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
registry.registerBeanDefinition("...", beanDefinition);
Which point/interface/lister can I use to obtain this? Keep in mind I need an instance of BeanDefinitionRegistry.
Adding explanation as required.
Those definitions are created from a list of Classes gathered by scanning the classpath. Those classes are not Spring Bean(s) natively, so I need to integrate them into my ApplicationContext. Those classes, however, accepts constructor arguments which are Spring Beans.
That's why I'm setting
beanDefinition.setAutowireCandidate(true);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
Those new registered Beans are there used by other Bean(s) (native Beans).
You are trying to make the solution too complex. If your only goal is to have non #Component annotated classes be detected by component scanning and have them used as Spring Beans simply define a custom includeFilter for the #COmponentScan.
You can use a filter of type ASPECTJ or REGEX to match a package or type.
#ComponentScan(includeFilter = #Filter(type=REGEX, expression="com.foo.bar.*))
Something like that will automatically detect your beans (assuming they are in a packaged being scanned) and create spring beans out of them. If they have a single constructor that will automatically be used to create an instance.
Register a new BeanFactoryPostProcessor or BeanDefinitionRegistryPostProcessor bean in your context. This bean will get invoked after bean definitions are scanned but before actual beans are constructed:
Extension to the standard BeanFactoryPostProcessor SPI, allowing for the registration of further bean definitions before regular BeanFactoryPostProcessor detection kicks in. In particular, BeanDefinitionRegistryPostProcessor may register further bean definitions which in turn define BeanFactoryPostProcessor instances.

spring cron4j scheduling method invoked before bean initialization

I have build a web application and scheduled cron job using cron4j. While executing the cron the run() method is calling and in the run() method all other bean objects are showing null. Hence, I am getting NullPointerException. Below is my sample code.
class Employee{
#autowired
IEmployeeService employeeService;
public void run() {
employeeService.getEmployeeDetails();
}
}
The above example employeeService object getting null and all other bean objects inside getEmployeeDetails(); are getting null and getJdbcTemplate() is also null.
How to initialize bean objects in spring while executing cron using cron4j.
You can use #BeanFactoryPostProcessor annotation to order creation of beans and create/run your job at the end of bean initialization.
The definition of BeanFactoryPostProcessor to bean (configuration metadata) processing. That is to say, the Spring IoC container allows configuration of BeanFactoryPostProcessor metadata in the container before the actual read instantiate any other bean, and may modify it. If you want, you can configure multiple BeanFactoryPostProcessor. You can control the BeanFactoryPostProcessor by setting the'order'attribute of the execution order.
From Spring documentation:
The next extension point that we will look at is the org.springframework.beans.factory.config.BeanFactoryPostProcessor. The semantics of this interface are similar to those of the BeanPostProcessor, with one major difference: BeanFactoryPostProcessor operates on the bean configuration metadata; that is, the Spring IoC container allows a BeanFactoryPostProcessor to read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessors.
Please find more information on Spring docs: http://docs.spring.io/spring/docs/4.1.3.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-factory-extension-factory-postprocessors

Spring bean scope as the class of the bean its autowired

I have spring bean called Executor I need this executor to be Injected in two classes one is session scoped and the other is singleton, So I use #Autowired annotation to inject it in both, it works very fine, But for the session bean the executor is only one for all sessions, and I understand this is how it should work.
How can I make the executor take the scope of the bean its injected into ? now all I could do is use two different classes for each.
And I cant realy understand the effect of setting the attribute scoped-proxy in
<context-component-scan /> to targetClass.
EDIT
Here is my try out for prototype scope:
suppose this is my session soped bean
#Component
#Scope("session")
public class WebExecutor(){
#Autowired
private ExecutorService executor;
#Async
public void startCalc(){
executor.start("now");
}
}
And this is my ExecutorService class
#Component
#Scope("prototype")
public class ExecutorService{
private int progress;
public void start(String when){
//do some stuff and increment the progress
}
//getter and setter for progress
}
I also have another component with default singleton scope which autowires the executor as well, so the executor can't be session scoped.
So now I have one class needed to have the scope of the bean it is being autowired in, So I tried the prototype scope.
How this should work in the Web versions is like this
You click a link that begins the execution which calls the method start of the WebExecutor
Then I get the progress from the ExecutorService using the WebParser and pass it back to the user to report the progress.
This was working great before prototype but the ExecutorService was one for all sessions,
so the I used prototype but now the progress is always 0, and when I debugged the Executor I found that it was incrementing but apprentally the WebExecutor could not see the increment.
U can use prototype scope. Then it will inject new instance of Executor when the Bean is constructed. So Executor starts its lifecycle when the bean is created.
scoped-proxy=target-class creates CGLIB wrapper around your bean. So other beans holds reference only to this wrapper, and the right bean with right scope is dynamically injected into this wrapper.
i.e. you don't hold very old expired request scoped bean in your session scoped bean, probably injected when the session was created . Instead new bean for current request is injected every time new request is created. This way you can access beans whith shorter life from beans with longer life i.e. your request scoped bean from your session scoped bean.

Accessing legacy out-of-container instantiated objects from Spring beans

We have a legacy system where something like a Service Locator is used to instantiate and provide all service objects:
class ServiceLocator {
ServiceA serviceA;
ServiceB serviceB;
public ServiceLocator () {
serviceA = ...;
serviceB = ...;
}
public ServiceA getServiceA() {
return serviceA;
}
public ServiceB getServiceB() {
return serviceB;
}
}
(imagine 70 more fields and getters...)
This object is then passed around from class to class to provide access to the service objects.
It is outside the scope of the project to change this design for existing code, but to at least not make things worse, we would like to introduce Spring to progressively instantiate future services with DI similar to Introducing an IoC Container to Legacy Code.
In contrast to the aforementioned situation, we already know how we will access the spring created spring bean objects from our legacy code. Our problem are objects we plan to create with spring, that need any of the service objects created outside of the spring context.
We came up with the following solution:
Create a static accessor for the ServiceLocator and set it in the constructor, load the spring application context object. In the spring configuration create a bean for the ServiceLocator with the static accessor as described in Section 3.3.2.2 in the Spring reference:
<bean id="serviceLocator"
class="ServiceLocator"
factory-method="getInstance"/>
for each Service create another bean using "instance factory method" as described in Section 3.3.2.3:
<bean id="serviceA"
factory-bean="serviceLocator"
factory-method="getServiceA"/>
Create other beans referencing these "dummy beans".
I guess this would work, but creates a lot of seamingly unnessessary pseudo configuration. What I'd rather like is something like this:
"If a bean is referenced and that bean is not explicitly defined, search for a method with the needed signature and name in the ServiceLocator class and use this object."
Is it possible to do so? Are there any entry points into the spring bean instantiation process that I am not aware of and that can be used here? Can I do this by subclassing the spring application context class?
Any help would be greatly appreciated!
You can define a BeanFactoryPostProcessor to populate your application context with beans from ServiceLocator.
In BeanFactoryPostProcessor, use beanFactory.registerSingleton(...) to add a fully instantiated bean, or ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(...) to add a definition (note that some application contexts may not implement BeanDefinitionRegistry, though all typical contexts implement it).

Resources