You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Ivan Perales M." <iv...@gmail.com> on 2012/03/17 19:46:46 UTC

Using Spring parentContext to inject common dependencies is injecting dont expected values

Hello list, first of all thanks for your efforts trying to help noobs like
me.

This is the first time i am using struts, more precise struts 2 and spring
plugin. I also am setting a parent context for common beans in various
wars. My problem is as follows:

As i have read, struts only works with autowired context, dont know why but
it's ok. My common beans have an id of the form
com.mycompany.bsi.ServiceBsi, so i can not use autowired by name because i
can not name a variable like that. So i am using autowired by type. The
problem represented here is an instantiation of an Action object by spring
factory.

I have done many test, but what i observed is that no matter if i use
@Autowired annotation or @Resource or none on a field, spring always inject
beans if there is a method with a param type of any existing bean. If the
method does not exists, event if field with annotation does, spring does
not inject the bean. That's for a side, in the another one, the method
AbstractAutowireCapableBeanFactory.unsatisfiedNonSimpleProperties wich is
called from  AbstractAutowireCapableBeanFactory.autowireByType, return an
array of Strings of the targets properties to be injected, but the weird
thing (maybe not) is that array contains properties like actionErrors,
actionMessages, fieldErrors who are part of the validationAware, not in the
action itselft. So for some rare circumstances of life, i have declared in
the parent context various list of LinkedList type, so for a reason unknown
for me, spring if transforming these lists into a map, putting the id of
the bean as a key, and injecting the resulted map into the method
fieldErrors, because this is expecting a Map<String, List<String>>, then
when the action is been invoked, there are errors and you can imagine what
happens.

I tried to use autowired by name, but nothing is injected, even if i use
@Resource instead of @Autowired. I tried to put the ref in the bean
declaration but the same, nothing injected.

I could resolve it by interfacing ApplicationContextAware on every action,
and the context is injected, but i'd like to inject only properties, not
the whole context. Moreover i know that the generation of the properties to
be injected when i use autowired by type is spring guilt, but i'd like to
know if there is a work arounf to solve this.

OK, thanks in advance for your time.

PD, sorry for my english.

-- 
Lindolfo Iván Perales Mancinas
Solo existen 10 tipos de personas en el mundo, las que saben binario y las
que no.

RE: Using Spring parentContext to inject common dependencies is injecting dont expected values

Posted by Puneet Babbar 2 <pb...@sapient.com>.
Hey Ivan,

With my limited knowledge about spring and struts integration - The framework enables "autowiring" by default. (Autowiring means to look for objects defined in Spring with the same name as your object property). To change the wiring mode, modify thespring.autowire property.
struts.objectFactory.spring.autoWire = type


Try changing the autoWire type and play around.

Or,

Maybe you can try to explicitly wire your Actions using spring instead of getting them auto generated by spring-struts plugin when the framework loads.

For example - 
 I have a Action class which I want to explicitly wire using spring - here's what I would do - 

public class LoginPage extends ActionSupport {
	private String loginMessage;
	
	public String getLoginMessage() {
		return loginMessage;
	}
	public void setLoginMessage(String loginMessage) {
		this.loginMessage = loginMessage;
	}
	
	public String execute(){
		return SUCCESS;
	}
}


In your applicationContext.xml you can wire it as follows - 

<bean class="com.sapient.portfoliomanagement.login.LoginPage" scope="prototype">
	<property name="loginMessage" value="Test Message"></property>
</bean>

You don't need to give the name of the bean explicitly, now when a request comes for this action class, the object you explicitly wired using spring would be used.

I hope this helps

Regards

Puneet

-----Original Message-----
From: Ivan Perales M. [mailto:ivan.perales@gmail.com] 
Sent: Sunday, March 18, 2012 12:17 AM
To: user@struts.apache.org
Subject: Using Spring parentContext to inject common dependencies is injecting dont expected values

Hello list, first of all thanks for your efforts trying to help noobs like
me.

This is the first time i am using struts, more precise struts 2 and spring
plugin. I also am setting a parent context for common beans in various
wars. My problem is as follows:

As i have read, struts only works with autowired context, dont know why but
it's ok. My common beans have an id of the form
com.mycompany.bsi.ServiceBsi, so i can not use autowired by name because i
can not name a variable like that. So i am using autowired by type. The
problem represented here is an instantiation of an Action object by spring
factory.

I have done many test, but what i observed is that no matter if i use
@Autowired annotation or @Resource or none on a field, spring always inject
beans if there is a method with a param type of any existing bean. If the
method does not exists, event if field with annotation does, spring does
not inject the bean. That's for a side, in the another one, the method
AbstractAutowireCapableBeanFactory.unsatisfiedNonSimpleProperties wich is
called from  AbstractAutowireCapableBeanFactory.autowireByType, return an
array of Strings of the targets properties to be injected, but the weird
thing (maybe not) is that array contains properties like actionErrors,
actionMessages, fieldErrors who are part of the validationAware, not in the
action itselft. So for some rare circumstances of life, i have declared in
the parent context various list of LinkedList type, so for a reason unknown
for me, spring if transforming these lists into a map, putting the id of
the bean as a key, and injecting the resulted map into the method
fieldErrors, because this is expecting a Map<String, List<String>>, then
when the action is been invoked, there are errors and you can imagine what
happens.

I tried to use autowired by name, but nothing is injected, even if i use
@Resource instead of @Autowired. I tried to put the ref in the bean
declaration but the same, nothing injected.

I could resolve it by interfacing ApplicationContextAware on every action,
and the context is injected, but i'd like to inject only properties, not
the whole context. Moreover i know that the generation of the properties to
be injected when i use autowired by type is spring guilt, but i'd like to
know if there is a work arounf to solve this.

OK, thanks in advance for your time.

PD, sorry for my english.

-- 
Lindolfo Iván Perales Mancinas
Solo existen 10 tipos de personas en el mundo, las que saben binario y las
que no.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Using Spring parentContext to inject common dependencies is injecting dont expected values

Posted by Eric Lentz <Er...@sherwin.com>.
> As i have read, struts only works with autowired context, dont know why 
but it's ok.

I'm not able to take time to read and understand your whole question, but 
this premise, upon which your question appears to be built, is incorrect. 
I use Spring and Struts 2 together, all of the time, and I never use 
autowiring, but I'm also not using annotations. I configure through a XML 
file. I call the bean whatever I want to call it and then use that name in 
the place of the class name in the struts.xml. Maybe that little bit of 
information can help you?