spring - Autowiring String property through application context -


i want autowire string bean below

<bean id="name" class="java.lang.string" autowire="byname">         <constructor-arg value="aravind"/> </bean> <bean id="employee" class="employee" autowire="byname"/>  public class employee {    private string name;    public void setname(string name)    {      this.name=name;     }    public string getname()    {      return name;    } } 

when try access name attribute in employee null

employee emp=(employee)getapplicationcontext().getbean("employee"); system.out.println(emp.getname()==null); 

it prints true.

can on this?

you still need set property on employee somehow.

setting name can done in multiple ways.

xml configuration.

<bean id="employee" class="employee" autowire="byname">     <property name="name">         <ref bean="name" />     </property> </bean> 

using @autowired

public class employee {    @autowired    private string name;     public void setname(string name) {      this.name=name;    }     public string getname() {      return name;    } } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -