spring 组件基于注解的注册方式
程序员文章站
2022-07-11 12:59:41
spring 中常用的组件标签有: @Controller:控制层 @Service:业务层 @Repository:数据层 @Component:普通的pojo注入到spring容器 组件注册方式: @ComponentScan 扫描那些要注入到spring容器的组件的包路径 @bean :主要作 ......
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{ @override public 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{ @override public 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>{ @override public color getobject() throws exception { return new color(); } @override public class<?> getobjecttype() { return color.class; } @override public boolean issingleton() { return false; } } package com.annotation.factorybean; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @configuration public class mainconfig { @bean public 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(); } }
下一篇: 不懂什么叫编程?
推荐阅读
-
02Spring基于xml的IOC配置--实例化Bean的三种方式
-
vue注册组件的几种方式总结
-
向Spring容器中注册组件的方法汇总小结
-
Spring的IOC控制反转实现注解方式配置
-
基于Ajax和forms组件实现注册功能的实例代码
-
Spring学习笔记第二天,Spring基于xml或注解的IOC以及IOC的案例
-
基于SpringBoot的操作日志管理(AOP+自定义注解方式)
-
基于Spring Boot 2.0.2.RELEASE 的 Spring Cloud 速成指南 | 二. Spring Cloud 服务注册中心(Eureka Server)
-
Spring MVC @Transactional注解方式事务失效的解决办法
-
Spring5参考指南:基于注解的容器配置