深圳前十网站建设公司,百度推广关键词和创意,销售管理系统数据库设计,vs制作网页教程本文问题: 1. Scope是做什么的#xff1f;常见的用法有几种#xff1f;2. DependsOn是做什么的#xff1f;常见的用法有几种#xff1f;3. ImportResource干什么的#xff1f;通常用在什么地方#xff1f;4. Lazy做什么的#xff0c;通常用在哪些地方#xff1f;常见的…本文问题:
1. Scope是做什么的常见的用法有几种2. DependsOn是做什么的常见的用法有几种3. ImportResource干什么的通常用在什么地方4. Lazy做什么的通常用在哪些地方常见的用法有几种
1,Scope指定bean的作用域
Scope用来配置bean的作用域等效于bean.xml中的bean元素中的scope属性。
Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface Scope {AliasFor(scopeName)String value() default ;AliasFor(value)String scopeName() default ;ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
}用法: 1,和Compontent一起使用在类上
Component
Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ServiceA {
}2,和Bean一起标注在方法上
Configurable
public class Main {BeanScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)public ServiceA serviceA() {return new ServiceA();}
}2,DependsOn指定当前bean依赖的bean
DependsOn等效于bean xml中的bean元素中的depend-on属性。 spring在创建bean的时候如果bean之间没有依赖关系那么spring容器很难保证bean实例创建的顺序如果想确保容器在创建某些bean之前需要先创建好一些其他的bean可以通过DependsOn来实现DependsOn可以指定当前bean依赖的bean通过这个可以确保DependsOn指定的bean在当前bean创建之前先创建好 Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface DependsOn {String[] value() default {};
}2种用法 1,和Compontent一起使用在类上
DependsOn({service2, service3})
Component
public class Service1 {public Service1() {System.out.println(create Service1);}
}2,和Bean一起标注在方法上
Configurable
public class MainConfig {BeanDependsOn({service2, service3})public Service1 service1() {return new Service1();}Beanpublic Service2 service2() {return new Service2();}Beanpublic Service3 service3() {return new Service3();}
}3,ImportResource配置类中导入bean定义的配置文件
有些项目前期可能采用xml的方式配置bean后期可能想采用spring注解的方式来重构项目但是有些老的模块可能还是xml的方式spring为了方便在注解方式中兼容老的xml的方式提供了ImportResource注解来引入bean定义的配置文件。
Retention(RetentionPolicy.RUNTIME)
Target({ElementType.TYPE})
Documented
public interface ImportResource {//value和locations效果一样只能配置其中一个是一个string类型的数组用来指定需要导入的配置文件的路径。AliasFor(locations)String[] value() default {};//value和locations效果一样只能配置其中一个是一个string类型的数组用来指定需要导入的配置文件的路径。AliasFor(value)String[] locations() default {};Class? extends BeanDefinitionReader reader() default BeanDefinitionReader.class;
}上面 value/locations 资源文件路径的写法:
通常我们的项目是采用maven来组织的配置文件一般会放在resources目录这个目录中的文件被编译之后会在target/classes目录中。
spring中资源文件路径最常用的有2种写法 以classpath:开头检索目标为当前项目的classes目录。以classpath*:开头检索目标为当前项目的classes目录以及项目中所有jar包中的目录如果你确定jar不是检索目标就不要用这种方式由于需要扫描所有jar包所以速度相对于第一种会慢一些。 相对路径的方式
classpath:com/chen/beans.xml
或者
classpath*:com/chen/beans.xml/绝对路径的方式
classpath:/com/chen/beans.xml*文件通配符的方式 会匹配chen目录中所有以beans-开头的xml结尾的文件
classpath:/com/chen/beans-*.xml*目录通配符的方式 会匹配chen中所有子目录中所有以beans-开头的xml结尾的文件注意这个地方只包含chen的子目录不包含子目录的子目录不会进行递归
classpath:/com/chen/*/beans-*.xml**递归任意子目录的方式 **会递归当前目录以及下面任意级的子目录
classpath:/com/javacode2018/**/beans-*.xml案例:
Configurable
ImportResource(classpath:/com/chen/beans*.xml)
public class MainConfig5 {
}4,Lazy延迟初始化
Lazy等效于bean xml中bean元素的lazy-init属性可以实现bean的延迟初始化。
延迟初始化就是使用到的时候才会去进行初始化。
Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface Lazy {
//boolean类型用来配置是否应发生延迟初始化默认为true。boolean value() default true;
}3种用法 1,和Compontent一起标注在类上可以使这个类延迟初始化
Component
//使用到了Lazy默认值为true表示会被延迟初始化在容器启动过程中不会被初始化当从容器中查找这个bean的时候才会被初始化。
Lazy
public class Service1 {public Service1() {System.out.println(创建Service1);}
}2,和Configuration一起标注在配置类中可以让当前配置类中通过Bean注册的bean延迟初始化 Lazy和Configuration一起使用此时配置类中所有通过Bean方式注册的bean都会被延迟初始化不过也可以在Bean标注的方法上使用Lazy来覆盖配置类上的Lazy配置 Lazy
Configurable
public class MainConfig {Beanpublic String name() {System.out.println(create bean:name);return Hello World;}Beanpublic String address() {System.out.println(create bean:address);return 广州;}Bean//这个方法上面使用到了Lazy(false)此时age这个bean不会被延迟初始化。其他2个bean会被延迟初始化。Lazy(false)public Integer age() {System.out.println(create bean:age);return 30;}
}3,和Bean一起使用可以使当前bean延迟初始化 BeanLazy(false)public Integer age() {System.out.println(create bean:age);return 30;}总结 Scope用来定义bean 的作用域2种用法第1种标注在类上第2种和Bean一起标注在方法上DependsOn用来指定当前bean依赖的bean可以确保在创建当前bean之前先将依赖的bean创建好2种用法第1种标注在类上第2种和Bean一起标注在方法上ImportResource标注在配置类上用来引入bean定义的配置文件Lazy让bean延迟初始化常见3种用法第1种标注在类上第2种标注在配置类上会对配置类中所有的Bean标注的方法有效第3种和Bean一起标注在方法上