Inject Spring config file into Ejb using Annotation - spring

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>" />

Related

How to represent #Resource in spring XML

I'm working with a program that currently uses Java Beans and need to recreate these Beans in another project that uses XML Beans. How can I represent the following line in XML?
#Resource(name="applicationIdCredentialProvider")
private CredentialProvider applicationIdCredentialProvider;
I'm currently using the following code to create a bean for the applicationIdCredentialProvider:
<bean id="applicationIdCredentialProvider" class="com.uprr.enterprise.security.credential.CredentialProvider"/>
But getting the error:
Could not instantiate bean class [com.uprr.enterprise.security.credential.CredentialProvider]: Specified class is an interface
Turns out I needed to import two classes as Beans using the following lines in my spring.xml file:
Add the following to < beans >:
xmlns:context="http://www.springframework.org/schema/context"
Add the following after < beans >:
<context:annotation-config/>
The imported files should look like this:
<bean id="giveYourBeanAName" class="class.of.your.Java.object"/>

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

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

spring context:component-scan not working

I have two packages holding spring controllers,I am not able to give actual names and packages.
com.test.controller------ABCController
com.test.xyz.controller------xyzController
I defined <context:component-scan base-package="com.test"/> in spring xml file,Spring able to find out controllers from com.test.controller,XYZController never called from spring container.it is saying no handler found.If i move xyzcontroller under package com.test.controller,then it's start working.
Please give any hints,why XYZController not working.
Thanks,
Raj
Use <context:component-scan base-package="com.test.**"/> in your spring config, so it will scan for every annotated class in all subpackages of com.test.

Autowire Annotation in Spring without using Component Scanning

Is it possible to autowire beans using the #Autowired annotation without using component scanning?
Yes. <context-component-scan .. /> is responsible for discovering beans annotated with #Component, #Controller, #Service, #Respository, etc.
In order to have annotations processed (#Autowired, #Resource, etc) you need <context:annotation-config />. Thus annotations are processed on beans that are listed in applicationContext.xml.
As far as I know, <context-component-scan .. /> activates <context:annotation-config /> automatically.
This is true for both spring 2.5 and 3.0. (thanks skaffman)
I have never tried without component-scanning enabled, however I can confirm that #Autowire annotations works in Spring 3.0.x even with beans that are defined via XML.
When using AnnotationConfigApplicationContext, annotation config processors are always registered, meaning that any attempt to disable them at the #ComponentScan level would be ignored.
If it is meant in the question that you should explicitly state:
- <context:component-scan ...> in your xml file(it enables <context:annotation-config />)
or
- #ComponentScan in your java config
Then the answer is - Yes, it is possible to enable component scanning without any of the stated above statements in your code or xml file.
Another approach is to use AnnotationConfigApplicationContext :
AnnotationConfigApplicationContext context=
new AnnotationConfigApplicationContext("org.example.your.package");
Where "org.example.your.package" is your package for stereotyped annotated classes: #Component, #Repository, #Service, etc.
AnnotationConfigApplicationContext will search for your beans in the base package and inner packages.
No, we must use #ComponentScan if you are using java based configuration
(or) <context-component-scan .. /> for xml based configuration.
Note: If you are not using any of the approaches no corresponding instances are created in AplicationContext.
and when you try to access a resource (http://localhost:8080/customers) will end up with
WARNING: No mapping found for HTTP request with URI [/customers] in
DispatcherServlet with name 'dispatcher

Referencing beans in xml config created via Annotations

This must be possible but I can't work out how or see it in the docs.
I need to reference a bean which has been created via an annotation #Service and context:component-scan, within a spring xml config file.
How is this achieved?
Cheers
use #Service("myService")
in the xml, use <property name="myProperty" ref="myService"/>

Resources