你可以自由地使用任何标准的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

Spring Boot学习一之Spring Beans和依赖注入的更多相关文章

  1. Spring Boot 学习1-创建Spring Boot应用

    如果使用Maven, 确保先安装好Maven再继续. 创建POM文件 在这里有两种方式: 继承Spring Boot parent的pom. 不继承. 继承Spring Boot pom 1 2 3 ...

  2. 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...

  3. 【Spring Boot学习之四】Spring Boot事务管理

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.springboot整合事务事务分类:编程事务.声明事务(XML.注解),推荐使用注解方式,springboot默 ...

  4. 【Spring Boot学习之三】Spring Boot整合数据源

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.Spring Boot整合Spring JDBC 1.pom.xml <project xmlns=&quo ...

  5. Spring Boot 学习笔记 - 认识Spring Boot框架

    因各种原因,.NET前端工程师重新接触JAVA,真是向全栈的路上又迈出了无奈的一步. 下面正文: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初 ...

  6. Spring Boot学习——第一个Spring Boot程序

    依照下面的步骤创建项目: 点击 Next 项目介绍: Application.java中的主要代码: @SpringBootApplication public class ReaderApplica ...

  7. Spring boot 学习 四:spring boot 配置文件 application.yml

    一 关于端口: spring boot的默认端口是8080, 如果想更改的话,在配置文件中做如下配置.ServerProperties.class会去读取这个值. server: port: 另外一种 ...

  8. 【Spring Boot学习之一】Spring Boot简介

    环境 Java1.8 Spring Boot 1.3.2 一.Spring Boot特点1.使用java运行项目,内置tomcat,无需外部容器:2.减少XML配置,使用properties文件和注解 ...

  9. Spring Boot快速入门(三):依赖注入

    原文地址:https://lierabbit.cn/articles/6 spring boot使用依赖注入的方式很简单,只需要给添加相应的注解即可 @Service用于标注业务层组件 @Contro ...

随机推荐

  1. HTML--JS 9*9乘法口诀

    <html> <head> <title>9*9乘法口诀</title> <script language="JavaScript&qu ...

  2. Recurrent Neural Network(3):LSTM Basics and 《Inside Out》

    下图是Naive RNN的Recurrent Unit示意图,可以看到,在每个时间点t,Recurrent Unit会输出一个隐藏状态ht,对ht加工提取后将产生t时刻的输出yt.而在下一个时间节点t ...

  3. python包的补充

    1.包A和包B下有同名模块也不会冲突,如A.a与B.a来自俩个命名空间 2.常见目录结构 import os 2 os.makedirs('glance/api') 3 os.makedirs('gl ...

  4. Python 学习笔记19 安装robot Framework

    因为项目组要做自动化测试,本人其实很希望能够使用 MStest + unit + C#来实现. 毕竟产品是基于.net 环境,并且使用C#环境开发的,适用性比较好,一些开发代码可以复用. 但是领导基于 ...

  5. 自定义Aspect风格的AOP框架

    本篇博客参考<架构探险--从零开始写java web框架>4.3章节 1代理接口: package smart.myaop.framework; public interface Prox ...

  6. 用php实现一个简单的爬虫,抓取电影网站的视频下载地址

    昨天没什么事,先看一下电影,就用php写了一个爬虫在视频网站上进行视频下载地址的抓取,这里总结一下抓取过程中遇到的问题 1:通过访问浏览器来执行php脚本这种访问方式其实并不适合用来爬网页,因为要受到 ...

  7. php cookie session 深究一下

    当一个用户用浏览器访问web(www.96net.com.cn)时候,若服务器开启session_start() 服务器tmp临时目录 自动生成session_id 并放回给创建一个cookie 保存 ...

  8. P4195 【模板】exBSGS/Spoj3105 Mod

    传送门 首先要懂得 $BSGS$,$BSGS$ 可以求出关于 $Y$ 的方程 $X^Y \equiv Z (mod\ mo)$ 的最小解,其中 $gcd(X,Z)=1$ $exBSGS$ 算是 $BS ...

  9. 70.Trapping Rain Water(容水量)

    Level:   Hard 题目描述: Given n non-negative integers representing an elevation map where the width of e ...

  10. 使用onfocus与onblur实现搜索框附加信息

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...