如果我们认为属性是不变的,以下内容似乎是有意义的。
<bean id="student" class="com.Student">
<property name="name" value="Paul"/>
<property name="id" value="12"/>
</bean>
<小时/>
Object s = fac.getBean("student");
Student stu = (Student) s;
可能最动态的方式是:
<bean id="student" class="com.Student"/>
<小时/>
Object s = fac.getBean("student");
Student stu = (Student) s;
stu.setName(...);
stu.setId(..);
我的问题是,这只是介绍Spring框架基 native 制的方式还是你真的进行原始值注入(inject)。
最佳答案
想象一下场景:
<beans profile="development">
<bean id="student" class="com.Student">
<property name="name" value="${name}"/>
<property name="id" value="123"/>
</bean>
</beans>
<beans profile="production">
<bean id="student" class="com.Student">
<property name="name" value="${name}"/>
<property name="id" value="#{idProvider.getId()}"/>
</bean>
</beans>
现在您将拥有 2 个不同的属性文件...
开发属性
name=developer
生产属性
name=Paul
要按照您的方式进行操作,您需要 2 个版本的代码。
关于java - 现实世界的 Spring 。你真的通过 bean 定义注入(inject)原始值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652491/