在不使用 applicationContext.xml 的 spring-boot 应用程序中,需要一个 bean 来更新应用程序使用的 jar 文件中存在的类的字段。如果应用程序使用 applicationContext.xml,则可以按如下方式指定 bean:
<bean id="au" class="path1.path2.path3.AU">
<property name="property1" value="newValue" />
</bean>
如何在 java 中以编程方式创建上述 bean?
您可以在@SpringBootApplication类的@Bean注释方法中以最简单的方式指定它:
import path1.path2.path3.AU;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) { ... }
// This method will produce a bean named "au" of class AU
@Bean
public AU au() {
AU au = new AU();
au.setProperty1("newValue");
return au;
}
}
这并不是 Spring Boot 特有的,而是纯粹的 Spring 功能。您可以在此处查看文档。