base package attribute in context:component-scan tag in spring 3 - spring

I am writing simple Hello application using maven in spring 3. I have made a HelloWorldService class by using #Service annotation. In the applicaioncontext.xml file giving different value to base-package attribute of context:component-scan base-package="yyy.xxx". My program is running.
What is the use of base-package in context:component-scan?

What is the purpose of the second tag, <context:component-scan>? Well, you need a bit of background information to understand the purpose of this tag.
Basically,say the #Controller annotation indicates that a particular class located at base-package="yyy.xxx" serves the role of a controller. Another example the #RequestMapping annotated methods to serve a web request.
So, component-scan base-package="yyy.xxx" will tell the spring container via the dispatcher servlet to locate such annotated classes and methods for mapping. But, There are plenty of more detailed explanation if you google it.

Related

How to override #RestController bean in springboot

I have a #RestController annotated class defined in a 3rd party .jar file in my springboot app. It's initialized via package scan. How can I overwrite this 3rd party controller bean with my own controller bean? Is there something like the "alias" of the xml configuration in spring-boot with java configure only environment?
Each mapping must be unique.There is no way to overrule an existing #RequestMapping.
There are workarounds for this though.
you can refer this post to get an idea about the answer :
How to override #RequestMapping in another controller?

<context:annotation-config> vs <context:component-scan>

I was going through some of the Spring documentation and from there I got to know that <context:annotation-config> is subset of <context:component-scan>, which means that what we can achieve with the help of the <context:annotation-config> , we can achieve the same using the <context:component-scan> with some add on functionality (i.e, scanning the specified basePackage for any defined component). So my question is what are the uses of these two?
in simple words: annotation-config: Annotation config main job is to activate all the annotations that are present in java beans and those are already registered either by defining in your application context file or being registered while component scanning. Important point is they need to be registered.
component-scan: Component scan can do everything that annotation config does,in addition to it,it also registers the java classes as spring bean those are annotated with #Component , #Service ,#Repository etc.

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.

Spring custom annotation - how to make it part of a library?

I've created a custom annotation (in Spring 3.05) that works great. I'd like to take that code and make it part of a library, packaged in a jar file, so I don't have to include my custom annotation code in each web app I write.
I'm unable to get Spring to act on the annotation, however. My library jar is in my web app's classpath and I tried scanning for it in applicationContext.xml:
<context:component-scan base-package="my.annotation.pkg" />
The field annotated with my custom annotation continues to be null.
Ideally I'd like to this to just work with a minimum of fuss and configuration, but so far I haven't had any success.
What part of Spring's wiring am I missing to get my custom annotation recognized when it's part of an external library?
Update
Here is how I "solved" it...just had to read a little more closely. In each context file (i.e. applicationContext.xml, dispatch-servlet.xml) I added the line:
<bean class="my.annotation.CustomInjector" />
...where my CustomInjector implements BeanPostProcessor. I based this on the code at this blog post: Implementing Seam style #Logger injection with Spring.
The author says I needed to do exactly what I did, so bad on me for not reading thoroughly. Why, though, is adding that bean definition required? Maybe Spring annotations are configured similarly under the hood - I just don't get why having the jar file on the classpath isn't enough.
Is your custom annotation annotated with the #Component annotation? From the Spring reference manual:
By default, classes annotated with #Component, #Repository, #Service, #Controller, or a custom annotation that itself is annotated with #Component are the only detected candidate components.
Alternatively, you could add a custom include-filter to the component-scan element in your XML configuration.

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