java - Spring bean creation is failing for beans without an "id" attribute -


i have spring controller definition follows :

@controller @requestmapping("/queue") public class queuecontroller {      queuesservice queueservice;      public queuecontroller(queuesservice queueservice) {         if (queueservice == null) {             throw new illegalargumentexception("queueservice cannot null");         }         this.queueservice = queueservice;     } } 

and corresponding entry in context-configuration file follows (where bean definition not have "id" attribute):

 <bean class="com.xy.web.controllers.queuecontroller"> <constructor-arg ref="queueserviceimpl"></constructor-arg> </bean> 

now,during application startup, spring throwing below exception:

caused by: org.springframework.beans.beaninstantiationexception: not instantiate bean class [com.xy.web.controllers.queuecontroller]: no default constructor found; nested exception java.lang.nosuchmethodexception: com.xy.web.controllers.queuecontroller.<init>() 

however when add "id" attribute "bean definition" (as follows), getting created properly.

<bean id="queuecontroller" class="com.xy.web.controllers.queuecontroller">     <constructor-arg ref="queueserviceimpl"></constructor-arg> </bean> 

any explanation or missing here?

i'm going assume have <content:component-scan ...> in config somewhere. try instantiate @component annotated classes. @controller @component spring try instantiate queuecontroller using class default empty constructor. in case, such constructor doesn't exist. therefore throw exception seeing.

you need add empty constructor

public queuecontroller() {} 

this happens regardless of bean declaration

<bean class="com.xy.web.controllers.queuecontroller">     <constructor-arg ref="queueserviceimpl"></constructor-arg> </bean> 

you're going end 2 queuecontroller instances. might not want.

as behavior you're seeing because of id:

when application context reads component-scan register beancomponentdefinition name queuecontroller. context moves on bean declaration. because you've specified id equal previous definition, override it. end 1 bean definition queuecontroller class. since bean declaration requires constructor specific argument , have that, won't complain , create bean.

if specified different id, abcd, application context register 2 beandefinitions: 1 name queuecontroller (following default name generation strategy) component-scan , 1 name abcd <bean> declaration. queuecontroller requires default constructor don't have. therefore exception.

more detailed

if using classpathxmlapplicationcontext, take @ classpathbeandefinitionscanner#doscan(string...) method's following call

string beanname = this.beannamegenerator.generatebeanname(candidate, this.registry); 

the beannamegenerator instance of annotationbeannamegenerator. calls

// fallback: generate unique default bean name. return builddefaultbeanname(definition, registry); 

which calls

string shortclassname = classutils.getshortname(definition.getbeanclassname()); return introspector.decapitalize(shortclassname); 

to return default name queuecontroller. that's 1 you're id overrides.

you can see in logs:

mon aug 26 12:12:15 edt 2013 [main] info  o.s.b.f.s.defaultlistablebeanfactory - overriding bean definition bean 'queuecontroller': replacing [generic bean: class [org.test.queuecontroller]; scope=singleton; abstract=false; lazyinit=false; autowiremode=0; dependencycheck=0; autowirecandidate=true; primary=false; factorybeanname=null; factorymethodname=null; initmethodname=null; destroymethodname=null; defined in file [c:\users\sotirios.delimanolis\git\content-store\target\test-classes\org\test\queuecontroller.class]] [generic bean: class [org.test.queuecontroller]; scope=; abstract=false; lazyinit=false; autowiremode=0; dependencycheck=0; autowirecandidate=true; primary=false; factorybeanname=null; factorymethodname=null; initmethodname=null; destroymethodname=null; defined in class path resource [app.xml]]  

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -