2015-06-30 63 views
1

我是Spring框架的新手。我正在努力研究Autowired概念,但我的输出结果不正确。我使用了波纹管代码。我不知道我错在哪里。任何人都可以帮我解决这个问题吗?如何在春季使用autowire概念?

Employee.java:

package com.autowire; 

public class employee { 

    private String name; 
    private String country; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getCountry() { 
     return country; 
    } 
    public void setCountry(String country) { 
     this.country = country; 
    } 

    public void show() 
    { 
     System.out.println("hai my country is:"+country); 
     System.out.println("hai my name is"+name); 

    } 

} 

main.java:

package com.autowire; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.Resource; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext;; 


public class main { 
    public static void main(String args[]){ 
     ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationcontext.xml"); 

     employee emp=(employee) context.getBean("b1"); 

     emp.show(); 
    } 

} 

的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" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

<bean id="b1" class="com.autowire.employee" autowire="byName"> 

</bean> 

<bean id="name" class="com.autowire.checking"> 
<property name="id" value="12"></property> 
<property name="name1" value="yes"></property> 
</bean> 

<bean id="id" class="com.autowire.checking"> 
<property name="id" value="12"></property> 
<property name="name1" value="yes"></property> 
</bean> 

</beans> 

检查ing.java

package com.autowire; 

public class checking { 
    public String getName1() { 
     return name1; 
    } 
    public void setName1(String name1) { 
     this.name1 = name1; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    private String name1; 
    private int id; 



} 

输出: 海我的国家是:空海 我的名字ISNULL

+1

对于这种一般性问题,请参考到Spring手册http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s11.html –

回答

1

首先,用大写字母开始类名称是一个很好的练习,例如您的Employee类应该以E而不是e开头。

Employee.java

package com.autowire; 
    public class Employee { 

     private String name; 
     private String country; 

     public String getName() { 
      return name; 
     } 

     public void setName(final String name) { 
      this.name = name; 
     } 

     public String getCountry() { 
      return country; 
     } 

     public void setCountry(final String country) { 
      this.country = country; 
     } 

     public void show() { 
      System.out.println("hai my country is: " + country); 
      System.out.println("hai my name is: " + name); 
     } 
    } 

其次,为什么当你想填充Employee类的实例变量创建多个豆。创建如下的单个bean,并将namecountry设置为Employee类的属性。

applicationcontext。XML

<bean id="b1" class="com.autowire.Employee"> 
     <property name="name" value= "A"/> 
     <property name="country" value= "India"/> 
</bean> 

接下来,在你的main类避免不必要的进口,并调用b1绿豆象下面这样:

Main.java

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Main { 
    public static void main(String args[]) { 
     ApplicationContext context = new ClassPathXmlApplicationContext(
       "applicationcontext.xml"); 
     Employee emp = (Employee) context.getBean("b1"); 
     emp.show(); 
    } 
} 
1

你可以像这样的Bean的属性名前使用@Autowired注解 -

@Autowired 
@Qualifier("name") 
private String name; 

@Autowired 
@Qualifier("country") 
private String Country; 

如果你使用这样的autowire那么你不必使用getter和setter方法。

0

Razib说的很好,但是不需要为要注入的类添加注释(@Component,@service等)?

0

您首先需要将检查添加为Employee类中的属性。 所以你应该做这样的,

public class Employee { 
Checking checking; 

public Employee(Checking checking) { 
    super(); 
    this.checking = checking; 
} 

public Checking getChecking() { 
    return checking; 
} 

public void setChecking(Checking checking) { 
    this.checking = checking; 
} 

@Override 
public String toString() { 
    return "Employee [checking=" + checking + "]"; 
} 

}

public class Checking { 
String name; 
String id; 

public Checking(String name, String id) { 
    super(); 
    this.name = name; 
    this.id = id; 
} 
@Override 
public String toString() { 
    return "Checking [name=" + name + ", id=" + id + "]"; 
} 

}

System.out.println(emp); 

,然后打印员工对象,

这应该给预期的结果。