SpringBeanAutowiringInterceptor for MDB on Weblogic injection failure - spring

I have a JavaEE 6 application using EJB and Spring 4 with a simple MDB that receive a single bean injection, but, each time I send a message to this MDB, below log appears:
com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke public void org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(javax.interceptor.InvocationContext) on bean class class app.module.mdb.impl.SpringBeanAutowiringInterceptor2_curj4r_Impl with args: [LifecycleEventCallbackInvocationContext(1495657191)]
..
Caused By: org.springframework.beans.factory.BeanCreationException: Injection of autowired dependencies failed for class [class app.module.mdb.impl.TestMDB]; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private app.module.bizo.impl.TestBOImpl app.module.mdb.impl.TestMDB.bizo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [app.module.bizo.impl.TestBOImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I'd like to know what I´m doing wrong, since I have everything properly configured.
MDB:
#MessageDriven(activationConfig = { #ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
#ActivationConfigProperty(propertyName = "connectionFactoryJndiName") })
#TransactionManagement(TransactionManagementType.CONTAINER)
#Interceptors(SpringBeanAutowiringInterceptor2.class)
public class TestMDB implements MessageListener {
#Autowired
private TestBO bizo;
#Logging
#Override
#TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void onMessage(final Message message) {
try {
this.bizo.process(message);
} catch (final Exception e) {
throw new RuntimeException(e.getCause());
}
}
}
BO:
#Component
public class TestBOImpl implements TestBO, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void process(Message message) throws BusinessException {
System.out.println("Im here!");
}
}
Config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="app.module" />
</beans>
XML Ref:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="IDContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="/META-INF/app-config.xml" />
</bean>
</beans>
Thanks.

It´s a classloader issue from Weblogic's server according Spring's issue: https://jira.spring.io/browse/SPR-14187

Related

Inject EJB 3.0 in spring 2.0 controller on websphere is throwing exception

I'd like to call an EJB service from my spring bean. I have tried many ways like the below and deployed on websphere, but it gives me exception in jndi names. Can anyone help?
Spring bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="ejbService" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName" value="ejb/EjbServiceImpl"/>
<property name="businessInterface" value="com.services.EjbService"/>
</bean>
<bean id="springController" class="com.controllers.SpringController" scope="session">
<property name="eService" ref="ejbService"/>
</bean>
</beans>
Spring controller
public class SpringController {
private EjbService eService;
public void setOrders(Order order) {
eService.liquidPortfolio(order);
}
public EjbService getEService() {
return eService;
}
public void setEService (
EjbService eService) {
this.eService = eService;
}
}
EJB
#Local
public class EjbService {
#Asynchronous
public void setOrders(Order order) ;
}
#Stateless
#Singleton
public class EjbServiceImpl implements EjbService{
#PostConstruct
public void init() {
System.out.println(" init method");
}
#Asynchronous
public void setOrders(Order order) {
System.out.println(" order=" + order);
}
}
Exception
[4/3/16 9:44:30:708 AST] 0000007a ContextLoader E org.springframework.web.context.ContextLoader initWebApplicationContext Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ejbService' defined in ServletContext resource [/WEB-INF/my-web-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Context: WSRUHHQ830Node01Cell/nodes/WSRUHHQ830Node01/servers/server1, name: ejb/EjbServiceImpl: First component in name EjbServiceImpl not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
Caused by: javax.naming.NameNotFoundException: Context: WSRUHHQ830Node01Cell/nodes/WSRUHHQ830Node01/servers/server1, name: ejb/EjbServiceImpl: First component in name EjbServiceImpl not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
at com.ibm.ws.naming.jndicos.CNContextImpl.mapNotFoundException(CNContextImpl.java:4564)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1822)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1777)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1434)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:616)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:165)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
at javax.naming.InitialContext.lookup(InitialContext.java:423)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
at com.ibm.ws.naming.ipcos.WsnOptimizedNamingImpl.do_resolve_complete_info(WsnOptimizedNamingImpl.java:567)
at com.ibm.ws.naming.cosbase.WsnOptimizedNamingImplBase.resolve_complete_info(WsnOptimizedNamingImplBase.java:2169)
at com.ibm.WsnOptimizedNaming._NamingContextStub.resolve_complete_info(_NamingContextStub.java:538)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2958)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2954)
at com.ibm.ws.naming.util.CommonHelpers.retry(CommonHelpers.java:871)
at com.ibm.ws.naming.jndicos.CNContextImpl.cosResolve(CNContextImpl.java:2952)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1818)
thanks for all whom trying help me, actually solved my problem in the following way
Spring XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="springController" class="com.controllers.SpringController" scope="session">
<property name="eService" ref="ejbService"/>
</bean>
<jee:jndi-lookup id="ejbService" jndi-name="java:global/myEAR/myApp/EjbService">
</jee:jndi-lookup>
</beans>
Spring controller
public class SpringController {
private EjbService eService;
public void setOrders(Order order) {
eService.liquidPortfolio(order);
}
public EjbService getEService() {
return eService;
}
public void setEService (
EjbService eService) {
this.eService = eService;
}
}
MY EJB
#LocalBean
#Stateless
#Singleton
public class EjbService {
#Asynchronous
public void setOrders(Order order) {
System.out.println(" order=" + order);
}
}

Autowire JNDI Resource in Spring

I would like to know how to autowire JNDI resource in Spring controller using annotation.
Currently I can retrieve the resource using
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="my/service"/>
</bean>
Is there any way, I can do the same thing using annotation? Something like
#Resource(name="my/service") ?
#Configuration
public class Configuration {
#Bean(destroyMethod = "close")
public DataSource dataSource() {
JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(false);
DataSource dataSource = dsLookup.getDataSource("my/service");
return dataSource;
}
}
I use this configuration to inject a JNDI resource
spring config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="destination" jndi-name="java:/queue/inbound/jndiname" />
</beans>
Class
#Autowired
private javax.jms.Destination destination;

#scope("prototype") not working properly

Consider the following configuration
public class MainApp {
public static void main(String args[]){
ApplicationContext ac=new ClassPathXmlApplicationContext("src/Beans.xml");
HelloWorld obj1=(HelloWorld) ac.getBean("helloWorld");
obj1.setMessage("OBJ1");
HelloWorld obj2=(HelloWorld) ac.getBean("helloWorld");
//obj2.setMessage("OBJ2");
System.out.println(obj1.getMessage());
System.out.println(obj2.getMessage());
}
}
#Scope("prototype")
public class HelloWorld {
String message;
public String getMessage() {
return "Your Message:"+message;
}
public void setMessage(String message) {
this.message = message;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="SpringDemo.src" />
<bean id="helloWorld" class="src.HelloWorld">
</bean>
</beans>
If i am not wrong it is showing the behavior of a Singleton scope. can someone let me know why it is not behaving as a "Prototype" scope?
You have this <bean id="helloWorld" class="src.HelloWorld"> in the xml configuration. When no scope is specified the scope defaults to singleton. The xml configuration overrides the annotation. Remove #Scope("prototype") and add scope="prototype" in the xml.

contex:componet-scan not picking up beans defined in jar file

I am working on a Spring Project:Common that uses a combination of Annotaions and Spring IOC in XML.
I have a common.jar which contains Common classes used by various projects.
And I have another Spring Project:WebService that refers to the beans defined in common.jar.
For some reason beans marked with #Component Annotation in Common.jar are not being picked up by my WebService Project. But all beans defined using <bean id="" class="" /> in Common.jar were picked up.
Below are the code for all files that have necessary configuration. Would really appreciate your help. Thanks in advance.
In Common.jar, applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<import resource="springConfig/app/AppServices.xml"/> <!-- Beans in this file were loaded. -->
<context:annotation-config/>
<context:component-scan base-package="com.ipd.app1"/> <!-- Beans for all classes under app1 package were NOT loaded -->
</beans>
In Common.jar, AppServices.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="inquireOrderApp" class="com.ipd.app.inquireOrderDetail.InquireOrderDetailAppImpl"/>
</beans>
Common.jar, com.test.app.MyClass
package com.ipd.app1;
#Component("createOrderApp")
public class CreateOrderAppImpl implements CreateOrderApp {
#Override
public CreateOrderResponse processMSSOrder(TransactionContext tx,
CreateOrderRequest createOrderRequest)
throws ApplicationException, Exception {
System.out.println("In App Layer Class CreateOrderAppImpl to ProcessOrder.");
return response;
}
}
WebService Project, IpdService_IPDSoapHTTPPortImpl.java
#WebService(portName = "IpdSoapHTTPPort", serviceName = "IpdService", targetNamespace = "http://ipd.com/ipdIpdweb/", wsdlLocation = "/wsdls/Ipd.wsdl", endpointInterface = "com.ipd.ipdIpdweb.IpdPortType")
#BindingType("http://schemas.xmlsoap.org/wsdl/soap/http")
public class IpdService_IpdSoapHTTPPortImpl implements IpdPortType {
ApplicationContext ctx;
public IpdService_IpdSoapHTTPPortImpl() {
this.ctx = AppContext.getCtx();
}
#Override
public void createOrder(WSHeader wsHeader,
CreateOrderRequest createOrderRequest,
Holder<WSResponseHeader> wsResponseHeader,
Holder<CreateOrderResponse> createOrderResponse)
throws WSException {
CreateOrderApp createOrderApp = (CreateOrderApp) ctx.getBean("createOrderApp");
res = createOrderApp.processOrder(tx, createOrderRequest);
res.setResponseCode(BigInteger.valueOf(0));
res.setResponseMessage("Success");
.....
}
}
Please let me know if you need see the code for any other file.
Well add this to applicationContext.xml
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

spring 3.1 annotated autowiring on standalone application

i wrote this app with eclipse and it works. But when i deploy it as standalone/console application, it cannot find StartApp bean i've injected.
here's the codes:
main app:
#Component
public class StartApp {
#Autowired
private Processor proc;
public StartApp() {
System.out.println("Starting App!");
}
private void say() {
proc.say();
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StartApp app = ctx.getBean(StartApp.class);
app.say();
}
}
service:
#Service
public class Processor {
public Processor() {
System.out.println("Processor initialized!");
}
public void say() {
System.out.println("hello!");
}
}
and applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="test.spring.desktop"/>
</beans>
i did put all spring libraries and logger including slf4j libraries.
and for console command i put these:
java -cp lib/*:lib/spring-3.1/*:test-spring-desktop.jar test.spring.desktop.StartApp
then i got these error message:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [test.spring.desktop.StartApp] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at test.spring.desktop.StartApp.main(StartApp.java:24)
Try to put in applicationCntext.xml this configuration:
<context:annotation-config />

Resources