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"
                
相关文章