Using Spring Boot —— Spring Beans and Dependency Injection

Spring Beans

  @ComponentScan  扫描指定包下的@Component @Controller @Service @Respository类

  @Autowired 自动注入bean,可以在属性/构造方法/set方法上使用

  

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;
    }

    // ...

}

猜你喜欢

转载自www.cnblogs.com/han6/p/11531955.html