Spring Boot控制器结构(依赖注入问题)

时间:2018-12-30 03:38:11

标签: spring-boot

在创建控制器时,我需要做一些结构上的事情。

可以按以下方式实现@autowired吗?

@Controller
class myController{

@autowired
MyComponent1 myComponent1;

public void myfunc(){
myComponent1.init();
myComponent1.excite();
}

}

我想知道单例创建的myComponent1是否不是并发问题。

如果锁定myfunc,它将太慢。

@Controller
class myController{

public void myfunc(){
Mybeen mybeen = SpringApplicationContext.getBean("myComponent1", myData); 
mybeen.init();
mybeen.excite();
}
}

@scope("prototype")
@Component("myComponent1")
class myComponent1{
~~~~~~~~~~~~~~~~~~~~~~~~
}

@Component,但它使@scope (prototype)可以在新的非单一对象上工作。

但是,存在一个问题,它会为每个请求创建一个新的myComponent1对象。

问题1。

哪种方法是对的?还有其他办法吗?

问题2。

如果@Component@scope ("prototype"),它与由new而非Component创建的对象有什么不同吗?

问题3。

每个请求都由一个新线程处理。我不需要在该结构中创建线程池,对吧?

0 个答案:

没有答案