精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

SpringBoot定義Bean的幾種實現方式

springboot定義bean的幾種實現方式

 

1.@bean

@bean
public demoservice demoservice() {
  return new demoservice();
}

 

2.@component

@component
public class demoservice {
}

 

3.@controller、@restcontroller、@service、@repository

@restcontroller
public class democontroller{
  @getmapping("/test")
  public string test() {
      return "succeed";
  }
}

 

4.@controlleradvice、@restcontrolleradvice

import org.springframework.core.methodparameter;
import org.springframework.http.mediatype;
import org.springframework.http.server.serverhttprequest;
import org.springframework.http.server.serverhttpresponse;
import org.springframework.web.bind.annotation.controlleradvice;
import org.springframework.web.servlet.mvc.method.annotation.responsebodyadvice;
/**
 * 

@title democontrolleradvice  *

@description controller增強  *  * @author acgkaka  * @date 2023/4/25 21:07  */ @controlleradvice public class democontrolleradvice implements responsebodyadvice {     @override     public boolean supports(methodparameter methodparameter, class aclass) {         return true;     }     @override     public object beforebodywrite(object body, methodparameter methodparameter, mediatype mediatype, class aclass, serverhttprequest serverhttprequest, serverhttpresponse serverhttpresponse) {         system.out.println("body is: " + body);         return body;     } }

注意:@controlleradvice相當于對于controller的切面,可以綁定propertyeditor。

(類似于aop,但是底層不是aop實現。)

 

5.@configuration

@configuration
public class democonfig {
}

注意:@configuration 主要標識一個bean是一個配置bean,利用這個bean可以對spring進行配置,比如掃描路徑、定義其他的bean。

 

6.@import

@springbootappilcation
@import(demo.class)
public class myapplication {
  public static void main(string[] args) {
      springapplication.run(myapplication.class);
  }
}

 

7.beandefinition

這是我們其他所有方法的底層實現。

myapplication.java

@springbootapplication
@import(demoimportbeandefinitionregistrar.class)
public class myapplication {
  public static void main(string[] args) {
      springapplication.run(myapplication.class);
  }
}

demoimportbeandefinitionregistrar.java

import com.demo.service.demoservice;
import org.springframework.beans.factory.support.abstractbeandefinition;
import org.springframework.beans.factory.support.beandefinitionbuilder;
import org.springframework.beans.factory.support.beandefinitionregistry;
import org.springframework.beans.factory.support.beannamegenerator;
import org.springframework.context.annotation.importbeandefinitionregistrar;
import org.springframework.core.type.annotationmetadata;
/**
 * 

@title demoimportbeandefinationregistar  *

@description @import注解的實現類  *  * @author acgkaka  * @date 2023/4/25 21:18  */ public class demoimportbeandefinitionregistrar implements importbeandefinitionregistrar {     @override     public void registerbeandefinitions(annotationmetadata importingclassmetadata, beandefinitionregistry registry, beannamegenerator importbeannamegenerator) {         abstractbeandefinition beandefinition = beandefinitionbuilder.genericbeandefinition().getbeandefinition();         // 定義bean         beandefinition.setbeanclass(demoservice.class);         // 注冊bean         registry.registerbeandefinition("demoservice", beandefinition);     } }

 

8.

最古老的方式

@springbootapplication
@importresource("classpath:spring.xml")
public class myapplication {
  public static void main(string[] args) {
      springapplication.run(myapplication.class);
  }
}

spring.xml

<!--?xml version="1.0" encoding="utf-7"
                
相關文章