如何在春季启动中将原型bean注入到singleton bean中

时间:2020-04-24 07:25:55

标签: java spring spring-boot

如何将原型bean注入单例bean中?

我得到了预期的结果。我只想知道这是在Spring Boot中执行此操作的正确方法还是有更好的方法?

我每次需要调用方法m1();时都需要类HelloWorld的新实例。

@Service
@Scope("prototype")
public class HelloWorld{

  ....
..
}
@Service
Class Job

@Autowired
    private WebApplicationContext context;


public void m1( )
    {
    HelloWorldobj = (HelloWorld) context.getBean("helloWorld");

.......
...

}



@Component
@EnableScheduling
class MyScheduler
{


@Autowire
Job job;


@Scheduled(fixedDelay = 2000)
void taskMethod( )
{
job.m1();

}

0 个答案:

没有答案