I have a third party lib which is exposing declarative service. But I have a spring bean class.
How do I inject this declarative service into my spring bean class?
You must declare this service also as a bean so it can be managed by spring.
Related
Is there any chance that CDI and spring dependency injection could co-exist. The problem started when I tried using JpaRepository interface.
I cannot remove beans.xml as it breaks a common jar implementation which uses CDI. I tried #Vetoed but it then fails to autowire my JpaRepository interface in a service class. Does it mean that #Vetoed prevents the bean from being managed by spring also?
I am looking for an idea so that i could use CDI to manage some beans and spring to manage the rest.
I can't find this information anywhere.
Can someone explain how spring boot 'decides' what the right scope is?
Are the beans all singletons?
Spring Boot doesn't decide anything about the bean scope, this is plain Spring framework functionality. Default bean scope is singleton scope (meaning, one instance of that bean in the application).
Here is the official documentation:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes
Default scope for a Spring Bean in singleton.
In this example on spring dependency injection here
Whne the final test class is run, after this line:
MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory
.getBean("mySpringBeanWithDependency");
Which implementation of writer class will get injected? The test class still is responsbile for creating an actual implementation and injecting it before calling business methods on the Writer. Is it true?
In both the annotation-based example and the XML-based example, Spring will inject the NiceWriter bean into the MySpringBeanWithDependency bean.
For the annotation example, this is because the NiceWriter class is annotated with #Service (while the Writer class is not) and Spring will discover this via classpath scanning and autowire it into MySpringBeanWithDependency.
For the XML example, this is because the NiceWriter class is used to define the bean with id writer, which is referenced as the "writer" property of the bean with id mySpringBeanWithDependency.
In both cases, the MySpringBeanWithDependency bean is instantiated with dependencies injected via Spring, and is thus ready to use. It is not responsible for managing its IWriter dependency. This is why dependency injection (DI for short) usually goes hand-in-hand with inversion-of-control (IoC for short). Spring provides an IoC container that uses DI.
I'm having 1 #Controller bean in my project
and 2 #Service beans that this controller calls.
the services using 2 different #Repository beans for persisting.
My question is :
my server is about to get hundreds of calls simultaniously, isnt it "awkward" that all my beans i described above are of scope="singleton"? should i use "prototype" instead? or maybe spring does it automatically ?
I think in both cases you wil have the same number of objects.
The non-singleton, prototype scope of bean deployment results in the
creation of a new bean instance every time a request for that specific
bean is made (that is, it is injected into another bean or it is
requested via a programmatic getBean() method call on the
container). As a rule of thumb, you should use the prototype scope
for all beans that are stateful, while the singleton scope should be
used for stateless beans.
in Spring by default all beans it is "singleton" and should be in most cases.
I'm developing a web application on Apache Tomcat 6 with Hibernate and Spring, I'm using different XML configuration files to define my Spring beans (like the Hibernate DAO, Quartz scheduler and some other stuff). All these files are loaded at Tomcat start up via web.xml (ContextLoaderListener).
Now I'm not sure what is the recommended way to get access to my beans.
Should I write on class which provides the BeanFactory for all classes which should use a bean, or is it the better way to load the BeanFactory in each class.
BeanFactory bf = (BeanFactory) ContextLoader.getCurrentWebApplicationContext();
One of the core ideas of the spring framework is to minimize dependencies between classes. You'll get the most benefit by using this concept throughout your whole project. Each and every backend object should be defined as bean and can therefore use the dependency injection automatism.
If some of your Beans need to access the ApplicationContext directly (eg for requesting all Beans implementing some marker interface) you can implement the ApplicationContextAware interface, so still no factory is needed.
Use dependency injection instead. Create getters & setters in each controller class, and use your *-servlet.xml to inject the required beans via the <property> tag. No factory needed!