spring 中常用的组件标签有:

@Controller:控制层

@Service:业务层

@Repository:数据层

@Component:普通的pojo注入到spring容器

组件注册方式:

@ComponentScan  扫描那些要注入到spring容器的组件的包路径

package com.annotation.component;
import org.springframework.stereotype.Controller;import org.springframework.context.annotation.Configuration;@Controller
public class PersonController {}package com.annotation.register;
import org.springframework.context.annotation.ComponentScan;//表明这是个注解配置类
@Configuration
@ComponentScan(value={"com.annotation.component"})
public class  Config {}package com.annotation.register;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.annotation.component.controller.PersonController;public class Test {public static void main(String[] args) {   AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class);PersonController personController = annotationConfigApplicationContext.getBean(PersonController.class);}
}

@bean :主要作用在方法上,name可以指定bean在容器中的名称,不指定的话默认是方法名;initMethod指定bean的初始化方法;destroyMethod指定bean的销毁方法

package com.annotation.component;public class Blue {}package com.annotation.register;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.annotation.component.bean.Blue;@Configuration
public class MainConfig {@Bean(name="blue")public Blue getColor(){return new Blue();}}package com.annotation.register;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.annotation.component.bean.Blue;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);Blue blue= annotationConfigApplicationContext.getBean(Blue.class);System.out.println(blue);}
}

@Import   :

  • 注入一个bean
  • 导入ImportSelector的实现类,selectImports方法里面配置要注入的bean
  • 导入ImportBeanDefinitionRegistrar的实现类,registerBeanDefinitions方法里面配置要注入的bean
package com.annotation.component.bean;public class Blue {}package com.annotation.component.bean;public class Yellow {}package com.annotation.component.bean;public class Green{}package com.annotation.importbean;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.annotation.component.bean.White;@Configuration
@Import(value = { White.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class })
public class MainConfig {}package com.annotation.importbean;import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;public class MyImportSelector implements ImportSelector{@Overridepublic String[] selectImports(AnnotationMetadata annotationmetadata) {return new String[]{"com.annotation.component.bean.Yellow"};}}package com.annotation.importbean;import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;import com.annotation.component.bean.Green;public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{@Overridepublic void registerBeanDefinitions(AnnotationMetadata annotationmetadata,BeanDefinitionRegistry beandefinitionregistry) {RootBeanDefinition beanDefinition = new RootBeanDefinition(Green.class);beandefinitionregistry.registerBeanDefinition("com.annotation.component.bean.Green", beanDefinition);}}package com.annotation.importbean;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);String[] names = annotationConfigApplicationContext.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}annotationConfigApplicationContext.close();}
}

implements FactoryBean

  • getObject 方法表示要注入的对象,getObjectType表示对象类型,isSingleton表示是否单例
  • annotationConfigApplicationContext.getBean("colorFactoryBean") 表示获取的是工程里面注入的对象,即Color
  • annotationConfigApplicationContext.getBean("&colorFactoryBean") 表示获取的是工程对象本身,即ColorFactoryBean
package com.annotation.component.bean;public class Color {}package com.annotation.factorybean;import org.springframework.beans.factory.FactoryBean;import com.annotation.component.bean.Color;public class ColorFactoryBean implements FactoryBean<Color>{@Overridepublic Color getObject() throws Exception {return new Color();}@Overridepublic Class<?> getObjectType() {return Color.class;}@Overridepublic boolean isSingleton() {return false;}}package com.annotation.factorybean;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MainConfig {@Beanpublic ColorFactoryBean colorFactoryBean(){return new ColorFactoryBean();}}package com.annotation.factorybean;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);Object obj1 = annotationConfigApplicationContext.getBean("colorFactoryBean");Object obj2 = annotationConfigApplicationContext.getBean("colorFactoryBean");        System.out.println(obj1==obj2);//获取factorybean 本身Object obj3 = annotationConfigApplicationContext.getBean("&colorFactoryBean");System.out.println(obj3.getClass());annotationConfigApplicationContext.close();}
}

转载于:https://www.cnblogs.com/chenzhubing/p/11010745.html

spring 组件基于注解的注册方式相关推荐

  1. Spring 事务基于注解和xml方式

    文章目录 基于注解方式的Spring事务配置 1 创建表结构 2 创建实体类 3 创建Dao 4 创建DaoImpl 5 创建Service 6 创建SrviceImpl 7 创建TxConfig 8 ...

  2. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  3. Spring定时任务@Scheduled注解使用配置方式(cron表达式、fixedRate和fixedDelay)

    Spring定时任务@Scheduled注解使用配置方式(cron表达式.fixedRate和fixedDelay) 序言: 个人推荐一个很方便的在线Cron生成器(网页版):https://qqe2 ...

  4. java @around,Spring AOP基于注解的Around通知

    是一种建议类型,可确保方法执行前后的通知可以运行. 以下是通知的语法: 语法 @Pointcut("execution(* com.yiibai.Student.getAge(..))&qu ...

  5. Spring Boot 基于注解驱动源码分析--自动配置

    Spring作为Java开发最常用的容器管理框架,使用注解为我们提供很多便捷,下面通过源码分析Spring基于注解驱动自动配置的原理 首先介绍两个关键类: ConfigurationClassPost ...

  6. 从源码深处体验Spring核心技术--基于注解的IOC初始化

    Annotation 的前世今生 从 Spring2.0 以后的版本中,Spring 也引入了基于注解(Annotation)方式的配置,注解(Annotation)是 JDK1.5 中引入的一个新特 ...

  7. Spring mvc基于注解自定义servlet

    在spring mvc中,有一个servlet实现:DispatcherServlet,也是spring mvc的核心部分,拦截所有请求(/*),并分发给不同的处理器,进行处理. 需求 在一个spri ...

  8. Spring MCV基于注解的控制器

    一 Controller类的实现 package org.fkit.controller; import org.springframework.stereotype.Controller; impo ...

  9. 【Spring】基于注解实现事务控制(银行转账)

    结构 domain类 package com.itheima.domain;import java.io.Serializable;/*** 账户的实体类*/ public class Account ...

最新文章

  1. php遍历文件夹下文件内容_PHP遍历某文件夹下的文件与文件夹名
  2. RL之DQN:基于TF训练DQN模型玩“打砖块”游戏
  3. Java命令学习系列(四)——jstat
  4. 网易易盾的“外挂对抗战”,游戏出海之路如何走得更安全?
  5. DeepFake——实际操作
  6. python中input的用法霍格沃_欢迎来到霍格沃茨—魔法01 Python 环境安装
  7. 软件测试技术体系-专业术语
  8. 数据结构与算法--回溯的理解以及实现
  9. 推荐系统三十六式(刑无刀)学习笔记(二)
  10. vue实现简单瀑布流布局(vue-waterfall2)
  11. HUNNU 11786 Sir Charles Antony Richard Hoare
  12. Flutter从相册选择图片并显示出来,上传到服务器
  13. 樊登读书会-《关键对话》
  14. 计算机组成原理推荐书籍
  15. 已拿offer热乎乎的蚂蚁金服面经分享,建议收藏(Java岗、附答案)
  16. audio驱动之codec和codec_dai
  17. ajax与Java后台互相传数据
  18. 路由概述与静态路由、默认路由、浮动路由基本配置
  19. CentOS-7.2部署DNS域名解析服务器并进行相关配置测试
  20. [admin]-02

热门文章

  1. 用js方法做提交表单的校验
  2. Ubuntu 16.04 安装 Docker - Dependency failed for Docker Application Container
  3. 不要为了面子伤了自己
  4. GOF23设计模式(创建型模式)建造者模式
  5. AI基础架构Pass Infrastructure
  6. NVIDIA FFmpeg 转码技术分析
  7. GPU上如何优化卷积
  8. 激光雷达Lidar与毫米波雷达Radar:自动驾驶的利弊
  9. 2021年大数据ELK(十二):Elasticsearch编程(环境准备)
  10. 【注意事项】Markdown遇到的小问题