我开发了一个Spring应用程序,它覆盖Setter Injection
超过Constructor Injection
,
但是getter函数会产生错误。
public class Book {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book(String name) {
this.name = name;
}
}
Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="bookBean" class="com.abc.xyz.Book">
<property name="name" value="ABCD"></property>
<constructor-arg value="EFGH"></constructor-arg>
</bean>
</beans>
ClientApp.java:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientApp {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Book book = (Book) context.getBean("bookBean");
book.getName();
}