我刚刚开始使用 Spring 并尝试按名称自动装配这是我的代码
地址类:
package org.springinaction;
public class Address {
private String addressline;
public String getAddressline() {
return addressline;
}
public void setAddressline(String addressline) {
this.addressline = addressline;
}
}
客户等级:
package org.springinaction;
public class Customer {
private Address address;
public Address getN() {
return address;
}
public void setN(Address n) {
this.address = n;
}
}
弹簧配置:
<beans>
<bean id="customer" class="org.springinaction.Customer" autowire="byName" />
<bean id="address" class="org.springinaction.Address">
<property name="addressline" value="bangalore" />
</bean>
</beans>
客户测试.java
package org.springinaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CustomerTest {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("SpringInAction.xml");
Customer cust=(Customer)context.getBean("customer");
System.out.println(cust.getN());
}
}
当我尝试按名称进行自动装配时,这表明如果属性名称与名称名称匹配,它将自动装配。但是在我的情况下它没有发生。它给了我null这个...任何人都可以帮我这个,以便正确地自动连接