您可以将原型bean与一起使用
BeanFactory。
@Configurationpublic class AppConfig { @Autowired Dao dao; @Bean @Scope(value = "prototype") public FixedLengthReport fixedLengthReport(String sourceSystem) { return new TdctFixedLengthReport(sourceSystem, dao); }}@Scope(value ="prototype")意味着Spring不会在启动时立即实例化Bean,而是稍后在需要时进行实例化。现在,要定制原型bean的实例,您必须执行以下操作。
@Controllerpublic class ExampleController{ @Autowired private BeanFactory beanFactory; @RequestMapping("/") public String exampleMethod(){ TdctFixedLengthReport report = beanFactory.getBean(TdctFixedLengthReport.class, "sourceSystem"); }}注意,由于无法在启动时实例化bean,因此不能直接自动装配bean。否则,Spring将尝试实例化bean本身。这种用法将导致错误。
@Controllerpublic class ExampleController{ //next declaration will cause ERROR @Autowired private TdctFixedLengthReport report;}


