Autowiring Struts Action Classes with Spring - 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

Related

Purpose of using #Configuration annotation

I have created a spring mvc based application but I didn't use this #Configuration annotation. What is the purpose of using #Configuration annotation? By using this, what are we communicating to springMVC container?
Assuming your application is using xml configuration rather than AnnotationConfig so it is not loaded to ApplicationContext at all.
#Configuration is used when ApplicationContext has been initialized and bean registration.
#Configuration annotation is a core Spring annotation, and not Spring MVC. It is a core entry point to configuring Spring-based application using Java config instead of XML config.
Please, use Spring Documentation more often because it is a place where you will find answers to most of your questions. Like this one:
Indicates that a class declares one or more Bean #Bean methods and may
be processed by the Spring container to generate bean definitions and
service requests for those beans at runtime

Spring Boot Scanning Classes from jars issue

In my sample spring boot application, i have added a dependency of a custom jar. My sample application has a support for web and jpa.
The jar which i've created contains a Spring MVC controller. Below is the sample code
#Controller
public class StartStopDefaultMessageListenerContainerController {
#Autowired(required=false)
private Map<String, DefaultMessageListenerContainer> messageListeners;
I haven't manually created a bean instance of this controller anywhere in my code.
Problem - When i start my spring boot application by running the main class, i get an error in console that prob while autowiring DefaultMessageListenerContainer.
My question here is, even though this class StartStopDefaultMessageListenerContainerController is just present in the classpath, it's bean shouldn't be created and autowiring should not happen. But spring boot is scanning the class automatically and then it tries to autowire the fields.
Is this the normal behavior of spring and is there anyway i can avoid this?
If the StartStopDefaultMessageListenerContainerController class is part of component scanning by spring container, Yes spring tries to instantiate and resolve all dependencies.
Here your problem is #Autowired on collection. Spring docs says,
Beans that are themselves defined as a collection or map type cannot be injected through #Autowired, because type matching is not properly applicable to them. Use #Resource for such beans, referring to the specific collection or map bean by unique name.
And also Refer inject-empty-map-via-spring

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.

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.

Using springs component-scan and injecting beans

Im using spring 2.5 and writing a DispatcherServlet. In the context file for the servlet im using component-scan and giving the location of the class where my controllers are. The classes use the #Controller("bean Name") annotation.
How can i inject properties into this bean?
Thanks
Also remember that you can inject beans defined in main application context to the beans defined in application context for servlet, but not vice-versa. These contexts aren't merged, but they form parent-child relationship.
Using #Autowired (docs) or #Resource (docs).

Resources