Problem using SpringBeanAutowiringInterceptor with #Value annotation in a Stateless Session Bean - spring

I'm using spring 3.0.5 in JBoss and when I try to use "#Value" annotation like this #Value("${terminal.type}") in an statless Session Bean annotated with #Interceptors(SpringBeanAutowiringInterceptor.clas s), I receive a IllegalArgumentException "'name' must not be null".
No problem with #Autowired annotation.
In SpringBeanAutowiringInterceptor class the metadata.inject() method is called with the bean name argument set to null.
Do I need to specify the bean name somewhere ?
(Same problem was asked here by another user: http://forum.springsource.org/showthread.php?94930-Problem-using-SpringBeanAutowiringInterceptor-with-Value-annotation, but was unanswered)

This will be fixed for 3.0.6 and 3.1:
https://jira.springsource.org/browse/SPR-8621

Related

How to inject a Spring bean in to Camel DefultProducer

I have written a camel component by extending DefaultComponent and also have the associative classes implemetation for endpoint, consumer, producer. My producer is extending the camel DefaultProducer and I want to inject a spring bean inside this class, so that whenever a route will be executed like
<route id="myRoute"><from uri="file://inbox"/><to uri="myComp://outbox"/>
I will be able to get the file from the file system and store it into database. For storing the file into the DB I have a service class instantiated by the spring container, but whenever I inject that bean into MyProducer we are getting null.
I recongnized the problem was not about Camel, it is related to the spring and I was injecting the bean in a wrong way. I resolved the problem by implementing the ApplicationContextAware interface into my helper class and storing the spring context as static variable and with the help of this helper class I am able to get spring bean inside MyProducer class. Thanks for spring ApplicationContextAware interface.

Spring: Serialization of session scoped bean

I have a Spring bean with scope session. This bean holds a reference to another singleton bean which is not serializable. What is the best approach if I want to serialize the session scoped bean?
The same question is already asked here: Spring session-scoped beans (controllers) and references to services, in terms of serialization
The accepted answer is that:
[...]this issue is resolved in spring 3.0 by providing a proxy of non-serializable beans, which obtains an instance from the current application context
As far as I understand the speaker in the linked video it should "just work". But in my case it doesn't! When I try to serialize my session scoped bean i get a NotSerializableException.
How can I solve this problem?
You need to instruct Spring to create that proxy. In XML-based config, via <aop:scoped-proxy/> tag, in component-scan mode via annotation:
#Scope(proxyMode = ScopedProxyMode.INTERFACES)
on your controller class.
You may mark singleton reference field as transient. Then check How to execute method after deserialization and load reference from ApplicationContext.
Also, please provide stacktrace.
P.S.
It is not too good idea to use session passivation.

Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton

I use JSF with Spring security. I implement it.
My code:
#Named("userDetailsManagerImpl")
#Scope("application")
public class UserDetailBusinessImpl implements UserDetailsService {
#Inject
private MenuBackingBean menu;
}
Everything seem to be good. However I must define MenuBackingBean is application scope. I only want session scope for it. So I change scope to session, but I got an error below:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsManagerImpl': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton
I think this error cause by spring security set application for default scope.
I searched on Google but have no answer for this problem (maybe I have no right keyword searching). Please help me to solve, thank you
More info I used:
JSF 2.0
Spring security 3.1.2
Named and Inject from JSR 330
Scope annotation from springframework package
I suppose that you have this code somewhere:
#Inject
private YourSessionBean yourSessionBean;
// later in some method
this.yourSessionBean.doSomething();
Try to use javax.inject.Provider for all these cases:
#Inject
private Provider<YourSessionBean> yourSessionBeanProvider;
// later in some method
this.yourSessionBeanProvider.get().doSomething();

Autowiring spring bean by name using annotation

In Springs latest version, we can autowire a bean using annotation as #Autowired. This will autowire the bean using its type(or constructor, if applied on it).
Is there any way I can use the #Autowired annotation based on the bean name which we were doing without annotation in Spring's XML file as autowire="byName"?
You can use:
#Autowired
#Qualifier("beanname")
According to the #Qualifier javadoc
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring
You can use JSR-250 #Resource for by-name bean autowiring, unless you need constructor injection or multi-parameter method injection.
From the docs:
If you intend to express annotation-driven injection by name, do not primarily use #Autowired, even if is technically capable of referring to a bean name through #Qualifier values. Instead, use the JSR-250 #Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.
If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself e.g. #Service (“employeeManager”).
Then using below code you can enable autowire by Name
#Autowired
#Qualifier("employeeManager")
private EmployeeManagerService employeeManagerService;
I was using bean name proxy which was messing up autowiring by name. #Resource didn't have that issue since it doesn't care about type. So now I know one reason for this recommendation by Spring developers :-) Just FYI
Use #Component("beanname") in the java class definition of your bean
Then while autowiring use JSR 330
#Inject
#Named(Value="beanname")

Autowiring Struts Action Classes with Spring

I have a question about spring and struts.
Currently I have spring injecting my struts action classes for me.
I was experimenting and trying to get Spring to inject my Struts action classes
for me using autowiring.
I have my spring applicationContext config file scanning the base package that the
action class is in using context:component-scan base-package="my.package",
and im using #Component annotation at the action classes class level.
Im also using #Qualifier("myActionClass") at the same action classes class level.
Im not configuring the action class as a Spring bean in applicationContext.
Then in my struts.xml config file, while configuring my action class, instead of giving the fully qualified package and class name, I use the #Qualifier annotation name "myActionClass".
This doesnt work though.
If in my applicationContext config file, configure my action class as a spring bean, get rid of the #Component and #Qualifier annotation on the action class, and in struts.xml, put the action classes Spring bean id for the class, then Spring injects my action class for me and everything is dandy. Only, this isnt using Autowiring the action class, and thats what I was testing.
Anyone know if autowiring using context:component-scan base-package
to scan your packages for your action classes so you dont have to configure them in applicationContext is possible?
Everything is explained in Spring documentation: Apache Struts 1.x and 2.x.
I am not sure whether you are using Struts 1 or 2. For Struts 1 you had to add Spring plugin to Struts configuration (I know it works). In Struts 2 all actions are created by Spring hence they are fully capable of Spring injection like all other beans.
Struts 2 seems to rely on there being a spring bean with the same spring bean-name matching the action class name (full name with package). You can specify the bean name in the #Component annotation, and it's also possible to make a global user-defined bean naming strategy so you can avoid adding this information to all your beans

Resources