我正在学习 Spring,我想问一下有没有办法在运行时为 Bean 属性赋值?我在互联网和书籍中尝试过的所有示例通常都在 xml 文件中实现硬编码值。
例如,在本网站http://www.dzone.com/tutorials/java/spring/spring-bean-setter-injection-1.html
中找到的简短教程
User 类具有 getter 和 setter 方法,但其所有值都是在 beans.xml 中硬编码。我尝试通过 main() 中的 setter 方法为用户的成员变量设置值,但出现了很多错误。
所以我只是想知道这是否可能,如果你能展示包含本教程的简短示例或网站,我将不胜感激。提前致谢。
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing
Offending resource: class path resource [SimpleValues.xml]
Bean 'insertValues'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: <property> element for property 'name' must specify a ref or value
Offending resource: class path resource [SimpleValues.xml]
Bean 'insertValues'
-> Property 'name'
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:324)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:579)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:477)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:446)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:311)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:202)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:181)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
//More errors
这是我的 xml 文件
<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-2.5.xsd">
<bean id="insertValues" class="com.SimpleValues.SimpleValues">
<property name="name"/>
<property name="age"/>
<property name="height"/>
</bean>
</beans>
主要方法
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("SimpleValues.xml");
ctx.refresh();
SimpleValues inject = (SimpleValues) ctx.getBean("insertValues");
inject.setName("Andres");
inject.setAge(20);
inject.setHeight(7);
System.out.println("Name: "+ inject.getName()+ " Age: "+inject.getAge()+ " Height: "+inject.getHeight());
}