Configuration to do the spring dependency injection in action class in struts 2? - spring

i have legacy j2ee project which is using struts 2 with spring. Now when i put debugger in first method call in action
class, i find all instance variables dependecies are injected automatically. i mean where do we configure dependency
injection for action class in struts? i explored the web.xml too , i do not find any related stuff?

Does your struts configuration (struts.xml) contain an element like this?:
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
Read more at http://struts.apache.org/2.2.3/docs/spring-plugin.html

If Spring is beiing used to inject the Dependencies for struts2 which includes results/actions etc.that means your code must be using Struts2-Spring plugin.Struts2 by default use its own Object factor to create instances of Action classes/ Results and Interceptors etc.
In order to use Spring one need to tell Struts2 about which object creation factory to be used.for that we need to define the following entry in to either struts.xml file or struts.properties file
struts.xml
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>
struts.properties
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
I belive that in your web.xml you will find an entry to Spring's ContextLoaderListener which will load the applicationContext xml file from the class path.
when uusing Spring the action will be create by spring in its xml file and in the action configuration file the bean reference is being used in place of Action Class name.
So all you need to check applicationContext.xml in your Project where Action will be initiated as a prototype beans and those bean references being used in struts.xml file to defining your Struts2 Action class

Related

Delay spring xml imports

I have an application where I have to read all application properties from properties files. Then override them from a external cache framework. Then initialize spring beans.
I am using Java Config of spring to read properties and override them. And using #import to load xml files. But xml files import as soon as context starts loading, resulting all the xml beans being initialized.
So is there a way I can delay xml files import until I load all the properties first?
You can use the following code.
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
or individually add lazy-init on beans which you don't want to load.
Or
There are lot of ways you can achieve your requirement One of which can be use of depends-on or #DependsOn if you are looking for annotation based configuration,
You can read property file after that you can create a cache bean and use
depends-on to let the container know you bean is depending on some other bean you can initialise a cache bean and use the same as you see fit.
See this example for better clearity.
<bean id="primaryBean" depends-on="cacheBean"></bean>
Now IoC guarantees that depending bean will be created before the bean which depends on this bean, So you will have an instance of this bean, and can use it.
Other than this you can implement LifeCycle interface
public interface Lifecycle {
void start();
void stop();
boolean isRunning();
}
For more references you can refer spring docs.

Inject Spring config file into Ejb using Annotation

Am trying to migrate project from ejb2.1 to ejb3.1. In my current project's ejb-jar.xml, i am using for loading the spring configuration xml file{which initializes the beans which going to be called in ejbbean class in onEjbCreate() method using "getBeanFactory().getBean("somespringclass") }.
Sample environment entry: <env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>config/xyz.xml</env-entry-value>
</env-entry>
what is the ideal syntax for using annotation(ejb3.1) in ejbbean class so that i can remove in ejb-jar.xml.
Thanks in advance.
Just annotate your EJB-3.1 interface with #EJB and put the following in your Spring configuration XML:
<context:component-scan base-package="<your-ejb-package-e.g.-com.ejb.sample>" />

integrating spring 2.5.6 and Struts 1.3.8

I want to clear some moments about integrating spring and struts. I have only one action class per application extended from MappingDispatchAction. So, actually my app when doing something uses not Action objects, but methods from my action. All I want from spring is to initialize this action and all for now. Just simply set DAO object. I looked through documentation, but I don't understand following:
We use action path from struts-config.xml as a name of bean in action-servlet.xml. Okay, but am I supposed to write beans in action-servlet.xml for every path name and set this poor DAO ref or what ?
The Struts 1 config file will use the DelegatingActionProxy class as the type attribute for all action configurations.
The Spring config file will contain the bean definitions of each action implementation. I don't know what DAO you're talking about, but actions that require DAO or service injection need to have them listed, yes--that's what Spring configuration is.
You may also be able to use annotations if you're not interested in using XML configuration, or use bean inheritance if many beans share the same DAO/service/etc. property values.

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

Struts2: custom action mapper defined in Spring

I have following situation:
I use Struts2 as MVC cobtroller
I use Spring as object factory
I implemented custom action mapper, I have configured this as a bean in my spring configuration.
How can I tell Struts to use this bean as an action mapper?
I tried top set:
struts.mapper.class=beanName
in struts.properties but this doesn't work.
Do you have have following the struts.xml file ? This is required to tell Struts2 that objects will be created by Spring
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>
OR
Add the following property in the struts.properties
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory

Resources