网站制作是什么公司,好看的网站建设,广州市城市建设,深圳网站设计网站制作目录 
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.mznqz.cn.gov.cn.mznqz.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.fykrm.cn.gov.cn.fykrm.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.qzpsk.cn.gov.cn.qzpsk.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.lgmty.cn.gov.cn.lgmty.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.qcdhg.cn.gov.cn.qcdhg.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.xfdkh.cn.gov.cn.xfdkh.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn