Gzjps

Springboot bean依赖注入

您可以自由地使用任何标准的Spring Framework技术来定义bean及其注入的依赖项。为简单起见,我们经常发现使用 @ComponentScan (找到你的bean)并使用 @Autowired (做构造函数注入)效果很好。

如果按照上面的建议构建代码(在根包中定位应用程序类),则可以添加 @ComponentScan 而不带任何参数。所有应用程序组件( @Component@Service@Repository@Controller 等)都会自动注册为Spring Beans。

以下示例显示了一个 @Service Bean,它使用构造函数注入来获取所需的 RiskAssessor bean:

 

package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatabaseAccountService implements AccountService {

	private final RiskAssessor riskAssessor;

	@Autowired
	public DatabaseAccountService(RiskAssessor riskAssessor) {
		this.riskAssessor = riskAssessor;
	}

	// ...

}

觉得有帮助的可以支持一下博主

发表于 2019-08-14 17:38  撸码狂魔  阅读(1066)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2021-12-26
  • 2022-03-08
  • 2021-09-14
  • 2021-12-05
  • 2022-01-01
  • 2021-09-23
  • 2021-11-19
  • 2021-11-06
猜你喜欢
  • 2022-12-23
  • 2021-04-06
  • 2021-06-25
  • 2022-01-01
  • 2021-06-09
  • 2022-12-23
  • 2021-12-13
相关资源
相似解决方案