java - Spring autowire byName not working as expected -


spring autowire byname not working expected.

public class spellchecker {     public spellchecker() {         system.out.println("inside spellchecker constructor." );     }      public void checkspelling() {         system.out.println("inside checkspelling." );     } }  public class texteditor {         private spellchecker spellchecker1;        private string name;         public void setspellchecker( spellchecker spellchecker1 ){           this.spellchecker1 = spellchecker1;        }        public spellchecker getspellchecker() {           return spellchecker1;        }        public void setname(string name) {           this.name = name;        }        public string getname() {           return name;        }     public void spellcheck() {        system.out.println(" texteditor name " +name);       spellchecker1.checkspelling();    } }  public class texteditormain {      public static void main(string args[]) throws interruptedexception{          applicationcontext context = new          classpathxmlapplicationcontext("beans.xml");          texteditor teditor = (texteditor) context.getbean("texteditor");         teditor.spellcheck();        } } 

spring beans configuration:

<bean id = "spellchecker1" class = "com.spring.beans.spellchecker"> </bean>  <bean id = "texteditor" class = "com.spring.beans.texteditor" autowire="byname">    <property name = "name" value = "text1"/> </bean> 

when give spellchecker1 bean id not working. below console o/p,

inside spellchecker constructor.  texteditor name text1 exception in thread "main" java.lang.nullpointerexception     @ com.spring.beans.texteditor.spellcheck(texteditor.java:26)     @ com.spring.main.texteditormain.main(texteditormain.java:15) 

bean id , reference name both same spellchecker1 still not working. strange thing if change bean id in xml spellchecker1 spellchecker code working , giving below o/p,

inside spellchecker constructor.  texteditor name text1 inside checkspelling. 

so why dependency not added when using spellchecker1 ?

it works designed. property named spellchecker not spellchecker1. have field named spellchecker1.

the name of field not same name of property. name of property defined get , set methods available on class. have setspellchecker (and corresponding getter) there property named spellchecker.

all of written down in javabeans specification (which written somewhere in 1998!)

basically properties named attributes associated bean can read or written calling appropriate methods on bean. example, bean might have foreground property represents foreground color. property might read calling color getforeground() method , updated calling void setforeground(color c) method.

source javabeans specification.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -