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;
/**
 * <p> @Title DemoControllerAdvice
 * <p> @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;
/**
 * <p> @Title DemoImportBeanDefinationRegistar
 * <p> @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.<bean />

最古老的方式

@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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="demoService" class="com.demo.service.DemoService" />
</beans>

参考地址:

1.【最新最全】一周刷完Java面试八股文的变态方法

关于SpringBoot定义Bean的几种实现方式的文章就介绍至此,更多相关SpringBoot定义Bean内容请搜索编程教程以前的文章,希望以后支持编程教程

下一章:Java通过Lambda表达式实现简化代码

之前,调用第3方服务,每个方法都差不多“长”这样, 写起来啰嗦, 改起来麻烦, 还容易改漏。public void authorizeRoleToUser(Long use ...