springboot注册bean的三种方法

spring在启动时会自己把bean(java组件)注册到ioc容器里,实现控制反转,在开发人员使用spring开发应用程序时,你是看不到new关键字的,所有对象都应该从容器里获得,它们的 生命周期 在放入容器时已经确定!

下面说一下三种注册bean的方法

  • @ComponentScan
  • @Bean
  • @Import

@ComponentScan注册指定包里的bean

Spring容器会扫描@ComponentScan配置的包路径,找到标记@Component注解的类加入到Spring容器。

我们经常用到的类似的(注册到IOC容器)注解还有如下几个:

  • @Configuration:配置类
  • @Controller :web控制器
  • @Repository :数据仓库
  • @Service:业务逻辑

下面代码完成了EmailLogServiceImpl这个bean的注册,当然也可以放在@Bean里统一注册,需要看@Bean那一节里的介绍。

@Component
public class EmailLogServiceImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceImpl.class);

 @Override
 public void send(String email, String message) {
  Assert.notNull(email, "email must not be null!");
  logger.info("send email:{},message:{}", email, message);
 }
}

@Bean注解直接注册

注解@Bean被声明在方法上,方法都需要有一个返回类型,而这个类型就是注册到IOC容器的类型,接口和类都是可以的,介于面向接口原则,提倡返回类型为接口。

下面代码在一个@Configuration注解的类中,同时注册了多个bean。

@Configuration
public class LogServiceConfig {

 /**
  * 扩展printLogService行为,直接影响到LogService对象,因为LogService依赖于PrintLogService.
  *
  * @return
  */
 @Bean
 public PrintLogService printLogService() {
  return new PrintLogServiceImpl();
 }

 @Bean
 public EmailLogService emailLogService() {
  return new EmailLogServiceImpl();
 }

 @Bean
 public PrintLogService consolePrintLogService() {
  return new ConsolePrintLogService();
 }
}

@Import注册Bean

这种方法最为直接,直接把指定的类型注册到IOC容器里,成为一个java bean,可以把@Import放在程序的八口,它在程序启动时自动完成注册bean的过程。

@Import({ LogService.class,PrintService.class })
public class RegistryBean {

}

Spring之所以如何受欢迎,我想很大原因是它自动化注册和自动化配置这一块的设计,确实让开发人员感到非常的自如,.net里也有类似的产品,像近几年比较流行的abp框架,大叔自己也写过类似的lind框架,都是基于自动化注册和自动化配置的理念。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot整合Kotlin构建Web服务的方法示例

    今天我们尝试Spring Boot整合Kotlin,并决定建立一个非常简单的Spring Boot微服务,使用Kotlin作为编程语言进行编码构建. 创建一个简单的Spring Boot应用程序.我会在这里使用maven构建项目: <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • SpringBoot深入理解之内置web容器及配置的总结

    前言 在学会基本运用SpringBoot同时,想必搭过SSH.SSM等开发框架的小伙伴都有疑惑,SpringBoot在spring的基础上做了些什么,使得使用SpringBoot搭建开发框架能如此简单,便捷,快速.本系列文章记录网罗博客.分析源码.结合微薄经验后的总结,以便日后翻阅自省. 正文 使用SpringBoot时,首先引人注意的便是其启动方式,我们熟知的web项目都是需要部署到服务容器上,例如tomcat.weblogic.widefly(以前叫JBoss),然后启动web容器真正运行我

  • eclipse下整合springboot和mybatis的方法步骤

    1.新建maven项目 先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid 2.建立项目结构 3.添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE<

  • SpringBoot项目整合mybatis的方法步骤与实例

    1. 导入依赖的jar包 springboot项目整合mybatis之前首先要导入依赖的jar包,配置pom.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • SpringBoot之LogBack配置详解

    LogBack 默认集成在 Spring Boot 中,是基于 Slf4j 的日志框架.默认情况下 Spring Boot 是以 INFO 级别输出到控制台. 它的日志级别是: ALL < TRACE < DEBUG < INFO < WARN < ERROR < OFF 配置 LogBack 可以直接在 application.properties 或 application.yml 中配置,但仅支持一些简单的配置,复杂的文件输出还是需要配置在 xml 配置文件中.配

  • SpringBoot整个启动过程的分析

    前言 前一篇分析了SpringBoot如何启动以及内置web容器,这篇我们一起看一下SpringBoot的整个启动过程,废话不多说,正文开始. 正文 一.SpringBoot的启动类是**application,以注解@SpringBootApplication注明. @SpringBootApplication public class CmsApplication { public static void main(String[] args) { SpringApplication.run

  • springboot打包不同环境配置以及shell脚本部署的方法

    前言 本篇和大家分享的是springboot打包并结合shell脚本命令部署,重点在分享一个shell程序启动工具,希望能便利工作: profiles指定不同环境的配置 maven-assembly-plugin打发布压缩包 分享shenniu_publish.sh程序启动工具 linux上使用shenniu_publish.sh启动程序 profiles指定不同环境的配置 通常一套程序分为了很多个部署环境:开发,测试,uat,线上 等,我们要想对这些环境区分配置文件,可以通过两种方式: 通过a

  • SpringBoot集成shiro,MyRealm中无法@Autowired注入Service的问题

    网上说了很多诸如是Spring加载顺序,shiroFilter在Spring自动装配bean之前的问题,其实也有可能忽略如下低级错误. 在ShiroConfiguration中要使用@Bean在ApplicationContext注入MyRealm,不能直接new对象. 道理和Controller中调用Service一样,都要是SpringBean,不能自己new. 错误方式: @Bean(name = "securityManager") public SecurityManager

  • SpringBoot中关于static和templates的注意事项以及webjars的配置

    1. 默认情况下, 网页存放于static目录下, 默认的"/"指向的是~/resouces/static/index.html文 2. 如果引入了thymeleaf, 则默认指向的地址为~/resouces/templates/index.html <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymel

  • SpringBoot thymeleaf eclipse热部署方案操作步骤

    网上找了好多的springboot热部署方案,也尝试了好几种方法,下面是我的成功方案跟大家分享 操作步骤 1.pom中添加热部署依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency&g

随机推荐