zoukankan      html  css  js  c++  java
  • Spring Boot学习一之Spring Beans和依赖注入

      你可以自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖。简单起见,我们经常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入。

      如果遵循以上的建议组织代码结构(将应用的main类放到包的最上层,即rootpackage),那么你就可以添加 @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.riskAssess= riskAssessor;

      }
      // ...
    }

    注 注意使用构建器注入允许 riskAssessor 字段被标记为 final ,这意味着 riskAssessor 后续是不能改变的

    @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan

  • 相关阅读:
    unit3d 4.6 document open solution
    Unity3dBug
    linq to xml
    A const field of a reference type other than string can only be initialized with null Error [duplicate]
    Redis数据类型
    redis快照与AOF
    redis实现高并发下的抢购/秒杀功能
    xss攻击怎么防止
    四种常见的索引类型
    什么是sql 注入及如何预防 sql 注入
  • 原文地址:https://www.cnblogs.com/yhtboke/p/10213736.html
Copyright © 2011-2022 走看看