BeanCreationException: Error creating bean with name 'EmpInfo' - spring

[HTTP:101216]Servlet: "cxf" failed to preload on startup in Web application: "TestSpring-0.0.1-SNAPSHOT.war". org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EmpInfo': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1403) at
My cxf.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- <jaxws:endpoint id="EmpInfo"
implementor="#EmpInfoImpl"
address="/EmpInfo"/> -->
<jaxws:endpoint id="EmpInfo"
implementor="com.test.example.impl.EmpInfoImpl"
address="/EmpInfo"/>
<!-- <bean id="EmpInfoImpl" class="com.test.example.impl.EmpInfoImpl" >
<property name="emplist" ref="EmpList" />
</bean>
<bean id="EmpList" class="com.test.example.impl.EmployeeList" /> -->
</beans>
#WebService(serviceName="EmpInfo",endpointInterface="com.test.example.EmpInfo",
portName="EmpInfoPortType", wsdlLocation="classpath:wsdl/EmpInfo.wsdl")
public class EmpInfoImpl implements EmpInfo{
private EmployeeList emplist;
public EmployeeList getEmplist() {
return emplist;
}
public void setEmplist(EmployeeList emplist) {
this.emplist = emplist;
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public Employee getInfo(String id) {
// TODO Auto-generated method stub
return new Employee("Test","Test","888");
//return emplist.getEmp(Integer.valueOf(id));
}
}
I am pretty new to CXF , so any help will be highly appreciated

This issue is resolved there was a issue in my code.

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);
}
}

Losing autowired attributes after executed the interceptor from Spring AOP

After a lot of time waste trying to find a answer, I decided to post this doubt.
I have a class that I would like to intercept by the Spring AOP.
ObjectToBeProxied.java
package com.ee.beans;
#Service
#Component
#Transactional
public ObjectToBeProxied implements IObjectToBeProxied {
#Autowired
private ParameterValueService parameterValueService;
public void doStuff() {
// do something before the call
getSelfRef().findEventParameterValue(new ParameterValueFilter());
// do something after
}
#HandleException
private Boolean findEventParameterValue(ParameterValueFilter parameterValueFilter) {
ParameterValue parameterValue = getSelfRef().parameterValueService.findParametertValueByFilter(parameterValueFilter);
return parameterValue.value();
}
private ObjectToBeProxied getSelfRef() {
return (ObjectToBeProxied) AopContext.currentProxy();
}
}
ExceptionHandlerAspect.java
package com.ee.aspects;
#Component
public class ExceptionHandlerAspect {
private static Logger LOGGER = Logger.getLogger(ObjectToBeProxied.class);
public Object handleAround(ProceedingJoinPoint joinPoint) throws Throwable {
// Handling the exception. Need to continue either the method throws a expcetion
// but it need to be logged
try {
return joinPoint.proceed();
} catch (Exception e) {
// something to handle the exception
}
return null;
}
}
Spring AOP configuration:
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config/>
<context:spring-configured />
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.ee.beans"/>
<bean id="exceptionHandlerAspect" class="com.ee.aspects.ExceptionHandlerAspect" />
<aop:config>
<aop:aspect id="exceptionHandlerConfig" ref="exceptionHandlerAspect">
<!-- Trata exceções lançadas -->
<aop:pointcut id="exceptionHandlerAroundMethod" expression="execution(* com.ee.beans.ObjectToBeProxied.*(..)) && #annotation(com.ee.exceptions.HandleException)" />
<aop:around pointcut-ref="exceptionHandlerAroundMethod" method="handleAround" />
</aop:aspect>
</aop:config>
When I call the method ObjectToBeProxied.doStuff(), ObjectToBeProxied.parameterValueService autowired is ok, not null.
But, when the aspect intercept the method call ObjectToBeProxied.findEventParameterValue(..) and execute the ExceptionHandlerAspect.handleAround(..), the ObjectToBeProxied.parameterValueService is not ok, it's null.
Debugging it, I can figure out that the Spring Aspect return the ObjectToBeProxied proxy after intercept it, but without the autowired attributes objects.
Where am I getting wrong?

Cannot create inner bean 'org.szymon.email.classes.MyMapperClass

I have exception type: org.springframework.beans.factory.BeanCreationException
full stack:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot create inner bean 'org.szymon.email.classes.MyMapperClass#1e222db' of type [org.szymon.email.classes.MyMapperClass] while setting bean property 'defaultViews' with key [1]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.szymon.email.classes.MyMapperClass] for bean with name 'org.szymon.email.classes.MyMapperClass#1e222db' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.szymon.email.classes.MyMapperClass
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:121)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
my dispatcher-servlet.xml:
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="org.szymon.email.*" />
<tx:annotation-driven />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="jsonp" value="application/javascript"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
<bean class="org.szymon.email.classes.MyMapperClass"/>
</list>
</property>
</bean>
</beans>
and my MyMapperClass:
package org.szymon.email.classes;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
public class MyMapperClass extends MappingJacksonJsonView {
/**
* Default content type. Overridable as bean property.
*/
public static final String DEFAULT_CONTENT_TYPE = "application/javascript";
#Override
public String getContentType() {
return DEFAULT_CONTENT_TYPE;
}
/**
* Prepares the view given the specified model, merging it with static
* attributes and a RequestContext attribute, if necessary.
* Delegates to renderMergedOutputModel for the actual rendering.
* #see #renderMergedOutputModel
*/
#Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if("GET".equals(request.getMethod().toUpperCase())) {
#SuppressWarnings("unchecked")
Map<String, String[]> params = request.getParameterMap();
if(params.containsKey("callback")) {
response.getOutputStream().write(new String(params.get("callback")[0] + "(").getBytes());
super.render(model, request, response);
response.getOutputStream().write(new String(");").getBytes());
response.setContentType("application/javascript");
}
else {
super.render(model, request, response);
}
}
else {
super.render(model, request, response);
}
}
}
This is continuation of solving problem from question:
Spring RESTful ajax gets error
There is one more interesting thing.
When I put comment on this bean like this:
<!-- <bean class="org.szymon.email.classes.MyMapperClass"/> -->
And create instance of this class in method on my controller and invoke render method of it, then everything is fine... but it is not proper solution.
Please help.
Cheers
The problem seems to be a build - IDE related. I was able to run the project both from IntellIJ IDEA and also by executing then Maven build and placing the produced war in webapps directory of Tomcat.
I suggest you do a Maven clean-compile-package and try again

#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.

Why destroy-method is giving error in Spring MVC?

I have destroy method in my bean but it is not showing in the out put. Could you please help me here.
package com.vaannila;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Ticket helloWorld = (Ticket) context.getBean("ticket");
helloWorld.setTicketNo("ABC009");
helloWorld.display();
context.close();
}
}
below is my xml file
<?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="helloWorld" class="com.vaannila.HelloWorld">
<property name="message" value="Hello World!"></property>
</bean>
<bean id="ticket" class="com.vaannila.Ticket"
scope="prototype" init-method="init" destroy-method="destroy"/>
</beans>
and Ticket class is below
package com.vaannila;
public class Ticket {
private String ticketNo="";
public String getTicketNo() {
return ticketNo;
}
public void setTicketNo(String ticketNo) {
this.ticketNo = ticketNo;
}
public void display()
{
System.out.println("Your Ticket No. is"+ ticketNo);
}
public void init()
{
System.out.println("Bean is ready You can use it now");
}
public void destroy()
{
System.out.println("Bean is going to destroy");
}
}
The out put is giving for init method but not for destroy method..
If i changed the init-method and destroy-method as default as below it is giving error in destroying the bean called "helloWorld"
<?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"
default-init-method="init" default-destroy-method="destroy">
<bean id="helloWorld" class="com.vaannila.HelloWorld">
<property name="message" value="Hello World!"></property>
</bean>
<bean id="ticket" class="com.vaannila.Ticket"
scope="prototype"/>
</beans>
When a bean is defined as prototype, the bean container creates new instances of this been whenever it is asked for that bean. That's the idea behind prototype-scoped beans.
After they are created, the container gives up responsibility for the bean. It cannot know if you are still holding a reference to it, or when is the moment you drop the last reference. This is true even after the container is closed. (The container is not the garbage collector.) So it cannot possibly know when is the right moment to call the destroy method.
If you need deinitialization for your ticket, you will have to call such a method from your code directly I think (assuming that it makes no sense to have singleton tickets).

Resources