spring 组件基于注解的注册方式
2019-06-13 09:02:28来源:博客园 阅读 ()
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(); } }
原文链接:https://www.cnblogs.com/chenzhubing/p/11010745.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:浅谈volatile关键字
下一篇:结合案例深入解析策略模式
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash