无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Spring Boot快速入門之(七):Bean和依賴注入

2020-08-14 16:10 作者:信碼由韁  | 我要投稿

【注】本文譯自: https://www.tutorialspoint.com/spring_boot/spring_boot_beans_and_dependency_injection.htm


? ? 在Spring Boot 中,我們可以利用 Spring Framework 定義 bean 及其依賴注入。@ComponentScan?及其對(duì)應(yīng)的@Autowired?注解被用于發(fā)現(xiàn)和注入 bean。

? ? 如果你遵循典型的 Spring Boot 代碼結(jié)構(gòu),那么就不需要使用?@ComponentScan?注解的任何參數(shù)。所有的組件類文件都被注冊(cè)為?Spring Beans。

? ? 下面的示例說(shuō)明如何自動(dòng)注入 Rest Template 對(duì)象并創(chuàng)建一個(gè)相同的:

@Bean

public RestTemplate getRestTemplate() {

???return new RestTemplate();

}

? ? 以下代碼展示如何在主Spring Boot 應(yīng)用類文件中自動(dòng)注入?Rest Template 對(duì)象及其 Bean 對(duì)象:

package com.tutorialspoint.demo;


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

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;


@SpringBootApplication

public class DemoApplication {

@Autowired

???RestTemplate restTemplate;

???

???public static void main(String[] args) {

??????SpringApplication.run(DemoApplication.class, args);

???}

???@Bean

???public RestTemplate getRestTemplate() {

??????return new RestTemplate();???

???}

}


Spring Boot快速入門之(七):Bean和依賴注入的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
阳城县| 延津县| 天津市| 汤阴县| 朝阳区| 米脂县| 绩溪县| 阜新市| 双峰县| 克什克腾旗| 泰和县| 吐鲁番市| 左权县| 黔东| 临朐县| 镇原县| 太康县| 景谷| 阿巴嘎旗| 曲靖市| 安泽县| 鄂尔多斯市| 绥滨县| 南雄市| 临桂县| 西昌市| 彰化市| 青川县| 精河县| 舒兰市| 新密市| 偃师市| 平塘县| 西华县| 阿尔山市| 天津市| 海口市| 蓬莱市| 苏尼特左旗| 金湖县| 通渭县|