CDI #ApplicationScoped bean in Spring - spring

I have a CDI bean that is annotated with #ApplicationScoped. Is there a way to tell Spring to pick it up during component scan, as if it were annotated with #Component? Spring does understand the #Inject annotation so why not #ApplicationScoped?
The idea is that it would be handy to use CDI beans in Spring (at least if they only use plain dependency injection without the fancy CDI stuff like interceptors, decorators...)

It is not entirely clear how is your code structured, if possible just annotate it with #Component as well. A component bean in Spring has similar properties as an application scoped bean. The default scope of a bean in Spring is singleton and it can be proxied, similar to what #ApplicationScoped would offer.
Spring does understand the #Inject annotation so why not #ApplicationScoped?
Spring offers support for JSR 330 annotation, #ApplicationScoped is nor part of those.

Related

SpringBeanFacesELResolver not working with spring WebFlow

I added SpringBeanFacesELResolver to my faces-config.xml but always when I am trying to inject property using #ManagedProperty the injection field is null.
Do you know if SpringBeanFacesELResolver works together with Spring WebFlow?
I had some problems injecting spring beans to manage beans with #ManagedProperty, i had also SpringBeanFacesELResolver the problem was that the beans have the #Transactional annotation and this beans has no interface when i created the interface and use the interface in the manage bean the injection was done, maybe this help you but i'm not using spring webflow
Note: I want to write this as comment but i don't know why i couldn't

How to inject service in JSF managed bean without using Spring IOC

Typically if I have to inject a service in Spring I use
<bean id="mycontroller" class="com.MyController">
<property name="myService" ref="myService" />
and
<bean id="myService" class="com.MyService"></bean>
How to do the same when using JSF? I dont want to use two IOC containers for the beans and rather keep it in faces context itself. I have seen links such as
JSF 2 inject Spring bean/service with #ManagedProperty and no xml
and A problem about injecting spring bean into jsf bean . They talk about injecting Spring managed bean into JSF context.
What I am trying to do must be really simple but am not able to find any relevant info. Am a newbie and will appreciate any help.
I think you may be confused by the word "bean".
The thing is, the "service" you are talking about is also a Spring bean, right?
You probably have it as a service cause it has some additional features (probably transaction management) added by Spring, according to your configuration.
The JSF IoC container is very simplistic, it does not allow you to configure its lifecycle to include transaction management, AOP and things like that. Those things you have to do with Spring (or EJB, in a Java EE environment).
So, when using JSF with Spring, you usually have two choices:
Either you put the backing beans for the JSF pages in the JSF container, annotating them with #ManagedBean, #RequestScoped, #ViewScoped, etc; and injecting any necessary Spring bean with #ManagedProperty in a property (a setter is required)
Or skip the JSF container and put the backing beans along with all others in the Spring container, and use the Spring scopes of request/session, annotating them with Spring's annotations #Component, #Scope("request"), #Scope("session") and injecting with #Autowired, #Qualifier and the like.
Personally, faced with that choice I'd go with the first choice, cause it gives you #ViewScoped and some other niceties. It's true it makes use of two IoC containers but then, which Java EE app does not?
If you want to go the second route anyway, you may also add a view scope for Spring beans, backed by JSF viewMap.
What Spring calls a "Service" is in Java EE terms an "EJB". EJB is out the box available in Java EE web profile containers like Glassfish, JBossAS and TomEE.
To create a stateless EJB service, just use #Stateless on the class:
#Stateless
public class SomeService {
public void doSomething() {
// ...
}
}
And to inject it in a JSF managed bean, just use #EJB on the property-to-be-injected:
#ManagedBean
#ViewScoped
public class SomeController {
#EJB
private SomeService service;
}
That's it. No getter/setter necessary. No XML boilerplate necessary.

jsf 2.0 spring 3 scope request not working

I am trying to use spring to provide managed beans to jsf. I assume that #ManagedBean will be picked up by JSF container to link the EL in JSF to managed bean even when I use spring by configuring spring usage in faces-config.xml.
Spring shall provide the beans but now who manages scope of the beans?
I have tried following annotation on beans to have it become Request scope but they do not work.
#ManagedBean(name="helloBean") //meant for JSF
#RequestScoped //meant for JSF
#Scope(value="request") //meant for spring
#Controller //meant for spring
public class HelloBean implements Serializable {
Actually earlier I was using plain JSF and #ManagedBean and #RequestScoped were working well. Now as I tried to integrate using spring the scope are not working.
I have even tried setting bean scope in spring config but they work as expected in context of spring (singleton and prototype) but not web request context.
I was trying to avoid having to use above #Scope and #Controller annotation hoping that JSF will manage scope but do not seem like.
Below are my files snippet for spring config and MyHelloBean which probably will help communicate better.
<bean id="helloBean" class="com.mkyong.common.HelloBean" init-method="init" />
<bean id="myHelloBean" class="com.mkyong.common.MyHelloBean" init-method="init" >
<property name="helloBean" ref="helloBean"></property>
</bean>
#ManagedBean
#RequestScoped
#Scope(value="request")
#Controller
public class MyHelloBean implements Serializable {
private static final long serialVersionUID = 1L;
//#ManagedProperty(value = "#{helloBean}")
private HelloBean helloBean;
see in above MyHelloBean I am using spring DI to set helloBean which gets set by spring fine. I have commented out #ManagedBean which I think I can leave it in there as it will be ignored by spring any ways and JSF is not going to process it I guess but to be safe I commented it out for JSF to not process it.
To complete I use below in faces-config to activate spring usage.
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
Regards,
Miten.
Our team faced similar problems integrating JSF and Spring beans, including problems with their scopes. And here I am to share our knowledge.
Scopes
Basically now, when you defined in your application context, that Spring will be managing your beans, thus scopes. Spring will map JSF scope annotations to his native scope annotations.
Example when both Spring and JSF support provided scope:
#RequestScoped annotation will be mapped to #Scope("request") Spring's annotation, etc with other supported scopes.
Example when Spring does not support JSF's provided scope:
#ViewScoped is not defined in Spring's native scope annotations, thus (not sure) it will use Spring's default scope, which is singleton, or request scope (not sure).
Bean Injection
In JSF2 you used #ManagedProperty annotations for injection, while Spring uses #Autowired annotation. What are the differences and which to choose?
Injecting Spring beans with #ManagedProperty:
Spring component you wish to inject must have a value which will match jsf injection annotation's value: #Component(value = "valueMatches") injected with #ManagedProperty(value = "valueMatches").
Injecting Spring beans with #Autowired:
Spring component you wish to inject must does not require a custom value to distinguish, if it is the only implementation of the bean you are injecting: #Component injected with #Autowired.
Our way
We used Spring's annotations for defining Beans, Scopes and Injection.
We marked JSF beans with #Scope(value = "desiredScope"), #Controller(value = "beanName") and #Qualifier(value = "beanName") annotations. Later which could be accessed from JSF context with help of in faces-config.xml via "beanName" value defined in the #Controller annotation.
We marked Spring services with #Service annotation.
We injected Spring services and JSF beans with #Autowired annotation.
We found ViewScope and FlashScope custom implemetations on the web and used them for our beans. Thus we did not lose any of JSF2 scopes and even added new one.
Hope this helps.
Your approach is a bit confusing in the sense that it seems that you're mixing Spring XML configuration and Spring Annotation-based configuration. As described as an example here, if you're using annotated configuration then you should have:
<context:component-scan base-package="com.yourcom.package" />
to order Spring scan for the annotations. Otherwise, if you're using XML configuration then you should have:
<bean id="helloBean" class="com.mkyong.common.HelloBean" init-method="init" scope="request" />
as by default the scope for a Spring bean is singleton.

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)

How to #autowire into jsf managed beans

In order to use the #Autowire annotation, the object where you use the annotation must come from the spring context.
JSF managed beans are created by JSF's IOC not Springs, therefor i cannot use #Autowire inside of them must must use faces-config.xml and managed properties.
I already setup an EL resolver that lets be have spring beans as managed properties, i want to take it one step further and get rid of the need to go into the faces-config.xml every time i need to autowire something. Is this possible?
Just annotate your managed beans with #Controller (or #Component), and #Scope("request") (or session) and add <context:component-scan> (if you haven't), and managed beans will automatically be detected as spring beans. And since you are already using the ELResolver, that should be it - you should be able to use #Autowired (or better - #Inject, if using spring 3.0).
You can use #ManagedProperty(#{'someBean'}) for autowire other beans in jsf bean

Resources