I am getting the following error while developing an application of spring RMI below is the code ..
The serializable class..
package co.tcs.pojo;
import java.io.Serializable;
public class Message implements Serializable {
private final String message;
public Message(String message) { this.message = message; }
public String getMessage() { return message; }
}
the interface ...
package com.tcs;
import co.tcs.pojo.Message;
interface EasyServer {
Message exchangeMessage(String user);
}
the implementation class and the server class also...
package com.tcs;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import co.tcs.pojo.Message;
public class EasyServerImpl implements EasyServer {
public Message exchangeMessage(String user) {
return new Message(user + ", Spring rocks!");
}
public static void main(String[] args) {
try{
ApplicationContext appctx=new FileSystemXmlApplicationContext("src/SpringContainer.xml");
System.out.println("RMI Server started.....>>");
}
catch(Exception e)
{
System.out.println("Exception : ServerProgram : main() : "); System.out.println("----->");
e.printStackTrace();
System.out.println("<-----"); }
}
}
finally the 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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="easyServer" class="com.tcs.EasyServerImpl">
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- register the impl as the 'easyServerService' -->
<property name="serviceName" value="easyServerService"/>
<property name="service" ref="easyServer"/>
<property name="serviceInterface" value="EasyServer"/>
<!-- defaults to 1099 -->
<property name="registryPort" value="1199"/>
</bean>
</beans>
the snap shot of the package structure..
http://imageshack.us/photo/my-images/607/sd1l.jpg/
the error I am getting is..
15 Dec, 2012 5:01:10 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext#70eb7859: display name [org.springframework.context.support.FileSystemXmlApplicationContext#70eb7859]; startup date [Sat Dec 15 17:01:10 IST 2012]; root of context hierarchy
15 Dec, 2012 5:01:10 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [D:\Pos2Workspace\ztestRmi\src\SpringContainer.xml]
15 Dec, 2012 5:01:10 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext#70eb7859]: org.springframework.beans.factory.support.DefaultListableBeanFactory#28bb0d0d
15 Dec, 2012 5:01:10 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#28bb0d0d: defining beans [easyServer,org.springframework.remoting.rmi.RmiServiceExporter#0]; root of factory hierarchy
15 Dec, 2012 5:01:10 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#28bb0d0d: defining beans [easyServer,org.springframework.remoting.rmi.RmiServiceExporter#0]; root of factory hierarchy
Exception : ServerProgram : main() :
----->
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExporter#0' defined in file [D:\Pos2Workspace\ztestRmi\src\SpringContainer.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Class] for property 'serviceInterface'; nested exception is java.lang.IllegalArgumentException: Cannot find class [EasyServer]. Root cause: java.lang.ClassNotFoundException: EasyServer
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:413)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:735)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:124)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:68)
at com.tcs.EasyServerImpl.main(EasyServerImpl.java:18)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Class] for property 'serviceInterface'; nested exception is java.lang.IllegalArgumentException: Cannot find class [EasyServer]. Root cause: java.lang.ClassNotFoundException: EasyServer
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:395)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1313)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1042)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 13 more
Caused by: java.lang.IllegalArgumentException: Cannot find class [EasyServer]. Root cause: java.lang.ClassNotFoundException: EasyServer
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:252)
at org.springframework.beans.propertyeditors.ClassEditor.setAsText(ClassEditor.java:63)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:341)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:325)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:390)
... 17 more
Root cause: java.lang.ClassNotFoundException: EasyServer
interface EasyServer is in package com.tcs
you might want to try
<property name="serviceInterface" value="com.tcs.EasyServer"/>
Related
I am learning DI and new to spring while trying out CI I have written following code and I think I am correct in syntax still it's showing bean creation error. why it is unable to create bean..??
The code is
Constuctor.java
package beans;
public class Constructor {
private String name;
private int age;
private String email;
public void Constructor(String name, int age, String email){
this.name=name;
this.age=age;
this.email=email;
}
public void show()
{
System.out.println("Name = "+name);
System.out.println("Age = "+age);
System.out.println("Email = "+email);
}
}
spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Ordered parameters -->
<beans>
<bean id="t" class="beans.Constructor">
<constructor-arg value="Alok"/>
<constructor-arg value="24"/>
<constructor-arg value="alok#gmail.com"/>
</bean>
</beans>
Const_main.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Constructor;
public class Const_main {
public static void main(String[] args) {
ApplicationContext ap= new ClassPathXmlApplicationContext("resources/spring.xml");
Constructor c = (Constructor)ap.getBean("t");
c.show();
}
}
it's giving the following error
Jun 29, 2017 3:16:45 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#b1a58a3: startup date [Thu Jun 29 15:16:45 IST 2017]; root of context hierarchy
Jun 29, 2017 3:16:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/spring.xml]
Jun 29, 2017 3:16:46 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: **Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Exception in thread "main"** org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:240)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.Const_main.main(Const_main.java:10)
Try this way
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Ordered parameters -->
<beans>
<bean id="t" class="beans.Constructor">
<constructor-arg>
<value>Alok</value>
</constructor-arg>
<constructor-arg>
<value>24</value>
</constructor-arg>
<constructor-arg>
<value>alok#gmail.com</value>
</constructor-arg>
</bean>
</beans>
You are trying to use Constructor Dependency Injection without creating any such constructor in your DTO (Constructor.java)
The method you defined above:
public void Constructor(String name, int age, String email){
this.name=name;
this.age=age;
this.email=email;
}
is just a simple method/function not a constructor, try to remove the word void.
Note:
To remove any ambiguities for constructor matching, it is more prefer to use indexes with constructor's parameters like:
<bean id="t" class="beans.Constructor">
<constructor-arg value="Alok" index="0"/>
<constructor-arg value="24" index="1"/>
<constructor-arg value="alok#gmail.com" index="2"/>
</bean>
I have written a small code to check #Autowired annotation in Spring, here is my piece of code.
package beans;
//import org.springframework.beans.factory.annotation.AutoWired;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.*;
public class Car {
#Autowired
#Qualifier(value="e1")
private Engine engine;
//no need to have setters or constructors here
public void printData()
{
System.out.println("Engine model year: " +engine.getModelyear());
}
}
package beans;
public class Engine {
private String modelyear;
//generate setter and getter
public void setModelyear(String modelyear) {
this.modelyear = modelyear;
}
public String getModelyear() {
return modelyear;
}
}
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Car;
//import beans.Test;
public class Client {
/**
* #param args
*/
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("resource/spring.xml");
Car c=(Car)ap.getBean("c");
c.printData();
}
}
<!--spring.xml-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- activate autowire annotation -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="engine" class="beans.Engine">
<property name="modelyear" value="2015"/>
</bean>
<bean id="e1" class="beans.Engine">
<property name="modelyear" value="2016"/>
</bean>
<bean id="c" class="beans.Car">
</bean>
</beans>
When I am trying to run Client.java class ,I am getting following error:
Can someone suggest why I am facing this issue?
Mar 11, 2017 10:19:24 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#70d18a80: display name [org.springframework.context.support.ClassPathXmlApplicationContext#70d18a80]; startup date [Sat Mar 11 10:19:24 IST 2017]; root of context hierarchy
Mar 11, 2017 10:19:24 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resource/spring.xml]
Mar 11, 2017 10:19:25 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext#70d18a80]: org.springframework.beans.factory.support.DefaultListableBeanFactory#18d1cf9e
Mar 11, 2017 10:19:25 AM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Mar 11, 2017 10:19:25 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#18d1cf9e: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,engine,e1,c]; root of factory hierarchy
Mar 11, 2017 10:19:25 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#18d1cf9e: defining beans [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,engine,e1,c]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'c': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private beans.Engine beans.Car.engine; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg/springframework/beans/factory/config/DependencyDescriptor;Ljava/lang/String;Ljava/util/Set;Lorg/springframework/beans/TypeConverter;)Ljava/lang/Object;
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private beans.Engine beans.Car.engine; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg/springframework/beans/factory/config/DependencyDescriptor;Ljava/lang/String;Ljava/util/Set;Lorg/springframework/beans/TypeConverter;)Ljava/lang/Object;
Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg/springframework/beans/factory/config/DependencyDescriptor;Ljava/lang/String;Ljava/util/Set;Lorg/springframework/beans/TypeConverter;)Ljava/lang/Object;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredElement.inject(AutowiredAnnotationBeanPostProcessor.java:361)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:61)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:228)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:414)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:66)
at test.Client.main(Client.java:13)
Set of jars which I am using are:
List of jars used and reference libraries while creating the project
I am a new bee to spring and am writing my first spring program.I have the following files.
package com.springstarter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringUser
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SpringStarter starter = ( SpringStarter ) context.getBean( "springstarter" );
starter.getMessage();
}
}
I have a bean called SpringStarter
package com.springstarter;
public class SpringStarter
{
private String message;
public String getMessage()
{
return message;
}
public void setMessage( String message )
{
this.message = message;
}
}
Beans.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-3.0.xsd">
<bean id="springstarter" class="com.springstarter.SpringStarter">
<property name="message" value="Hello World!"/>
</bean>
</beans>
The following is the package structure:
I have run the program in eclipse Mars, using Spring 4.2.4.
I didn't find any compilation issues, but the program is just showing the following logs.
Jan 14, 2016 10:36:08 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#73a83205: startup date [Thu Jan 14 10:36:08 IST 2016]; root of context hierarchy
Jan 14, 2016 10:36:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
The expected output though, is Hello World!
Kindly let me know if i'm making any obvious mistakes.
Nothing wrong, but to print the output on console, so you should use it like:
System.out.println(starter.getMessage());
I am experimenting on "beforeAdvice" over "JointPoints" in spring aop. it's pretty simple i am calling target method. before executing target class method, before() method should execute.
ClientApp:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.nt.services.LoanApprover;
public class ClientApp {
public static void main(String[] args) {
//activate ioc container
ApplicationContext context = new FileSystemXmlApplicationContext("src/com/nt/cfgs/applicationContext.xml");
//get bean
LoanApprover approver = context.getBean("pfb",LoanApprover.class);
//call b.method
approver.approver();
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="auditAdvice" class="com.nt.aspect.MyBeforeAdvice" />
<bean id="target" class="com.nt.services.LoanApprover" />
<bean id="pfb" class="
org.springframework.aop.framework.ProxyFactoryBean ">
<property name="target" ref="target" />
<property name="interceptorNames">
<list>
<value>
auditAdvice
</value>
</list>
</property>
</bean>
</beans>
MyBeforeAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice{
#Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("I am in MyBeforeAdvice class");
}
}
LoanApprover.java
public class LoanApprover {
public void approver(){
System.out.println("I am in LoanApprover method");
}
}
when i run this application i am getting this exception.
Aug 13, 2015 12:16:04 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext#7c6cd67b: startup date [Thu Aug 13 12:16:04 IST 2015]; root of context hierarchy
Aug 13, 2015 12:16:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [G:\java\Frameworks\SpringAOP\AOPProj3(Before Advice)\src\com\nt\cfgs\applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pfb': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '
auditAdvice
' is defined
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObject FromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFr omFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanIn stance(AbstractBeanFactory.java:1517)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract BeanFactory.java:251)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe anFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractA pplicationContext.java:962)
at test.ClientApp.main(ClientApp.java:13)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '
auditAdvice
' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi nition(DefaultListableBeanFactory.java:694)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean Definition(AbstractBeanFactory.java:1168)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract BeanFactory.java:281)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe anFactory.java:194)
at org.springframework.aop.framework.ProxyFactoryBean.initializeAdvisorChain(ProxyF actoryBean.java:460)
at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.ja va:244)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObject FromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 6 more
please anyone tell me why i am getting this exception?
Simply change
<value>
auditAdvice
</value>
to
<value>auditAdvice</value>
It seems like spring is using whitespaces and newline symbols as ID here.
Further information on that topic is available here.
Hope that helps.
I'm trying to run a simple Spring + Hibernate tutorial => Maven Spring Hibernate annotation example
My beans definition file BeanLocations.xml is like this :
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Database Configuration -->
<import resource="../database/Datasource.xml"/>
<import resource="../database/Hibernate.xml" />
<context:component-scan base-package="com.sample.springhibernate"/>
</beans>
My main method:
public static void main( String[] args )
{
ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:BeanLocations.xml");
StockBo stockBo = (StockBo)appContext.getBean("stockBo");
}
I have a defined service with an interface:
public interface StockBo {
public void save(Stock stock);
public void update(Stock stock);
public void delete(Stock stock);
public Stock findByStockCode(String stockCode);
}
And his implementation:
#Service("stockBo")
public class StockBoImpl implements StockBo {
#Autowired
StockDao stockDao;
public void setStockDao(StockDao stockDao){
this.stockDao = stockDao;
}
#Override
public void save(Stock stock) {
stockDao.save(stock);
}
........
There is any problem with this because spring throws a Exception when StockBo)appContext.getBean("stockBo") :
30-oct-2014 15:31:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#64dc11: display name [org.springframework.context.support.ClassPathXmlApplicationContext#64dc11]; startup date [Thu Oct 30 15:31:49 CET 2014]; root of context hierarchy
30-oct-2014 15:31:49 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext#64dc11]: org.springframework.beans.factory.support.DefaultListableBeanFactory#a3d4cf
30-oct-2014 15:31:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#a3d4cf: defining beans []; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'stockBo' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
Spring not foud my annotated service StockBoo (with #Service("stockBo") )....
What is the problem? How can I ensure that Spring recognize my service with component scan?
FYI: StockBo is in com.sample.springhibernate.bo and StockBoImpl in com.sample.springhibernate.bo.impl
If you turn your log level to DEBUG, you'll more than likely find a line like
DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [classpath*:BeanLocations.xml]
That is, your ClassPathXmlApplicationContext did not find any resources that match classpath*:BeanLocations.xml and therefore didn't load any context.
You'll need to provide a resource location String value that properly identifies and locates the context configuration file.