公众号编辑器排行榜,网站建设优化推广教程,wordpress文章采集插件,yellow在线观看大全目录
1. 基于XML配置文件 2. 基于XML注解方式声明bean 自定义bean
第三方bean
3.注解方式声明配置类 扩展1#xff0c;FactoryBean 扩展2,加载配置类并加载配置文件#xff08;系统迁移) 扩展3#xff0c;proxyBeanMethodstrue的使用
4. 使用Import注解导入要注入的bean…目录
1. 基于XML配置文件 2. 基于XML注解方式声明bean ·自定义bean
·第三方bean
3.注解方式声明配置类 扩展1FactoryBean 扩展2,加载配置类并加载配置文件系统迁移) 扩展3proxyBeanMethodstrue的使用
4. 使用Import注解导入要注入的bean对应的字节码 扩展4使用Import注解导入配置类
5. 编程式方式
6. 实现了ImportSelector接口
7.实现ImportBeanDefinitionRegistrar接口
8.实现了BeanDefinitionRegistryPostProcessor接口
总结 便于练习导一个比较大的包
dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.23/version
/dependency
1. 基于XML配置文件
!--声明自定义bean--
bean idbookService
classcom.itheima.service.impl.BookServiceImpl
scopesingleton/
!--声明第三方开发bean--
bean iddataSource classcom.alibaba.druid.pool.DruidDataSource/
/beans 2. 基于XML注解方式声明bean ·自定义bean
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
context:component-scan base-packagecom.itheima/
/beans
使用注解如Component、Service、Repository等来标识Bean类
·第三方bean
在配置类上写Component才可以被扫描到 Configuration也行更推荐
Component
public class DbConfig {
Bean
public DruidDataSource getDataSource(){DruidDataSource ds new DruidDataSource();return ds;}
}
3.注解方式声明配置类
Configuration
//Configuration配置项如果不用于被扫描可以省略
ComponentScan(com.itheima)
public class SpringConfig {Beanpublic DruidDataSource getDataSource(){DruidDataSource ds new DruidDataSource();return ds;}
}
扩展1FactoryBean
初始化实现FactoryBean接口的类实现对bean加载到容器之前的批处理操作
public class BookFactoryBean implements FactoryBeanBook {public Book getObject() throws Exception {Book book new Book();// 进行book对象相关的初始化工作return book;}public Class? getObjectType() {return Book.class;}
}
public class SpringConfig8 {Beanpublic BookFactoryBean book(){return new BookFactoryBean();}
}
扩展2,加载配置类并加载配置文件系统迁移)
在新的注解开发的项目中使用原来的老的配置文件使用ImportResource然后指定xml文件
Configuration
ComponentScan(com.itheima)
ImportResource(applicationContext-config.xml)
public class SpringConfig2 {
} 扩展3proxyBeanMethodstrue的使用
使用proxyBeanMethodstrue可以保障调用此方法得到的对象是从容器中获取的而不是重新创建的
默认是true
Configuration(proxyBeanMethods true)
public class SpringConfig3 {Beanpublic Book book(){System.out.println(book init ...);return new Book();}
}
先拿到了配置类的对象从容器中获取再调用了方法得到的就是同一个对象
public class AppObject {public static void main(String[] args) {ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig3.class);SpringConfig3 config ctx.getBean(SpringConfig3, SpringConfig3.class);SpringConfig3.book();SpringConfig3.book();}
}
4. 使用Import注解导入要注入的bean对应的字节码
Import(Dog.class)
public class SpringConfig5 {
}
被导入的bean无需使用注解声明为bean
public class Dog {
}
此形式可以有效的降低源代码与Spring技术的耦合度在spring技术底层及诸多框架的整合中大量使用
扩展4使用Import注解导入配置类
使用这种方式可以加载配置类并且也不用加Configuration了
Import(DbConfig.class)
public class SpringConfig {}
5. 编程式方式
·使用上下文对象在容器初始化完毕后注入bean
这种只能使用AnnotationConfigApplicationContext其他不生效
public class AppImport {public static void main(String[] args) {AnnotationConfigApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig5.class);ctx.register(Cat.class);String[] names ctx.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}}
}
6. 实现了ImportSelector接口
导入实现了ImportSelector接口的类实现对导入源的编程式处理谁导入他他就能查谁的户口把他里面的注解方法等等全部可以获取到
在配置类上Import(MyImportSelector.class)
public class MyImportSelector implements ImportSelector {public String[] selectImports(AnnotationMetadata metadata) {boolean flag metadata.hasAnnotation(org.springframework.context.annotation.Import);if(flag){return new String[]{com.itheima.domain.Dog};}return new String[]{com.itheima.domain.Cat};}
}
7.实现ImportBeanDefinitionRegistrar接口 导入实现了ImportBeanDefinitionRegistrar接口的类通过BeanDefinition的注册器注册实名bean实现对容器中bean的裁定例如对现有bean的覆盖进而达成不修改源代码的情况下更换实现的效果
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,BeanDefinitionRegistry registry) {BeanDefinition beanDefinition BeanDefinitionBuilder.rootBeanDefinition(BookServiceImpl2.class).getBeanDefinition();registry.registerBeanDefinition(bookService, beanDefinition);}}
8.实现了BeanDefinitionRegistryPostProcessor接口
导入实现了BeanDefinitionRegistryPostProcessor接口的类通过BeanDefinition的注册器注册实名bean实现对容器中bean的最终裁定
public class MyPostProcessor implements BeanDefinitionRegistryPostProcessor {public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {BeanDefinition beanDefinition BeanDefinitionBuilder.rootBeanDefinition(BookServiceImpl4.class).getBeanDefinition();registry.registerBeanDefinition(bookService, beanDefinition);}
}
总之实现了BeanDefinitionRegistryPostProcessor接口可以对最终的Bean定义产生影响可以动态地添加、修改或删除Bean定义从而对应用程序的配置和行为进行定制化。
总结 文章转载自: http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn