@ComponentScan
1 @Retention(RetentionPolicy.RUNTIME) 2 @Target(ElementType.TYPE) 3 @Documented 4 @Repeatable(ComponentScans.class) 5 public @interface ComponentScan { 6
7 /** 8 * 掃描路徑 9 * @ComponentScan(value = "spring.annotation.componentscan") 10 */
11 @AliasFor("basePackages") 12 String[] value() default {}; 13
14 /** 15 * 掃描路徑 16 */
17 @AliasFor("value") 18 String[] basePackages() default {}; 19
20 /** 21 * 指定掃描類 22 * @ComponentScan(basePackageClasses = {BookDao.class, BookService.class}) 23 */
24 Class<?>[] basePackageClasses() default {}; 25
26 /** 27 * 命名注冊的Bean,可以自定義實現命名Bean, 28 * 1、@ComponentScan(value = "spring.annotation.componentscan",nameGenerator = MyBeanNameGenerator.class) 29 * MyBeanNameGenerator.class 需要實現 BeanNameGenerator 接口,所有實現BeanNameGenerator 接口的實現類都會被調用 30 * 2、使用 AnnotationConfigApplicationContext 的 setBeanNameGenerator方法注入一個BeanNameGenerator 31 * BeanNameGenerator beanNameGenerator = (definition,registry)-> String.valueOf(new Random().nextInt(1000)); 32 * AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 33 * annotationConfigApplicationContext.setBeanNameGenerator(beanNameGenerator); 34 * annotationConfigApplicationContext.register(MainConfig2.class); 35 * annotationConfigApplicationContext.refresh(); 36 * 第一種方式只會重命名@ComponentScan掃描到的注解類 37 * 第二種只有是初始化的注解類就會被重命名 38 * 列如第一種方式不會重命名 @Configuration 注解的bean名稱,而第二種就會重命名 @Configuration 注解的Bean名稱 39 */
40 Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; 41
42 /** 43 * 用於解析@Scope注解,可通過 AnnotationConfigApplicationContext 的 setScopeMetadataResolver 方法重新設定處理類 44 * ScopeMetadataResolver scopeMetadataResolver = definition -> new ScopeMetadata(); 這里只是new了一個對象作為演示,沒有做實際的邏輯操作 45 * AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 46 * annotationConfigApplicationContext.setScopeMetadataResolver(scopeMetadataResolver); 47 48 * annotationConfigApplicationContext.register(MainConfig2.class); 49 * annotationConfigApplicationContext.refresh(); 50 * 也可以通過@ComponentScan 的 scopeResolver 屬性設置 51 *@ComponentScan(value = "spring.annotation.componentscan",scopeResolver = MyAnnotationScopeMetadataResolver.class) 52 */
53 Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; 54
55 /** 56 * 用來設置類的代理模式 57 */
58 ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; 59
60 /** 61 * 掃描路徑 如 resourcePattern = "**/*.class" 62 * 使用 includeFilters 和 excludeFilters 會更靈活 63 */
64 String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; 65
66 /** 67 * 指示是否應啟用對帶有{@code @Component},{@ code @Repository}, 68 * {@ code @Service}或{@code @Controller}注釋的類的自動檢測。 69 */
70 boolean useDefaultFilters() default true; 71
72 /** 73 * 對被掃描的包或類進行過濾,若符合條件,不論組件上是否有注解,Bean對象都將被創建 74 * @ComponentScan(value = "spring.annotation.componentscan",includeFilters = { 75 * @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class}), 76 * @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {SchoolDao.class}), 77 * @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class}), 78 * @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "spring.annotation..*"), 79 * @ComponentScan.Filter(type = FilterType.REGEX, pattern = "^[A-Za-z.]+Dao$") 80 * },useDefaultFilters = false) 81 * useDefaultFilters 必須設為 false 82 */
83 Filter[] includeFilters() default {}; 84
85 /** 86 * 指定哪些類型不適合進行組件掃描。 87 * 用法同 includeFilters 一樣 88 */
89 Filter[] excludeFilters() default {}; 90
91 /** 92 * 指定是否應注冊掃描的Bean以進行延遲初始化。 93 * @ComponentScan(value = "spring.annotation.componentscan",lazyInit = true) 94 */
95 boolean lazyInit() default false; 96
97
98 /** 99 * 用於 includeFilters 或 excludeFilters 的類型篩選器 100 */
101 @Retention(RetentionPolicy.RUNTIME) 102 @Target({}) 103 @interface Filter { 104
105 /** 106 * 要使用的過濾器類型,默認為 ANNOTATION 注解類型 107 * @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class}) 108 */
109 FilterType type() default FilterType.ANNOTATION; 110
111 /** 112 * 過濾器的參數,參數必須為class數組,單個參數可以不加大括號 113 * 只能用於 ANNOTATION 、ASSIGNABLE_TYPE 、CUSTOM 這三個類型 114 * @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class, Service.class}) 115 * @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {SchoolDao.class}) 116 * @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class}) 117 */
118 @AliasFor("classes") 119 Class<?>[] value() default {}; 120
121 /** 122 * 作用同上面的 value 相同 123 * ANNOTATION 參數為注解類,如 Controller.class, Service.class, Repository.class 124 * ASSIGNABLE_TYPE 參數為類,如 SchoolDao.class 125 * CUSTOM 參數為實現 TypeFilter 接口的類 ,如 MyTypeFilter.class 126 * MyTypeFilter 同時還能實現 EnvironmentAware,BeanFactoryAware,BeanClassLoaderAware,ResourceLoaderAware 這四個接口 127 * EnvironmentAware 128 * 此方法用來接收 Environment 數據 ,主要為程序的運行環境,Environment 接口繼承自 PropertyResolver 接口,詳細內容在下方 129 * @Override 130 * public void setEnvironment(Environment environment) { 131 * String property = environment.getProperty("os.name"); 132 * } 133 * 134 * BeanFactoryAware 135 * BeanFactory Bean容器的根接口,用於操作容器,如獲取bean的別名、類型、實例、是否單例的數據 136 * @Override 137 * public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 138 * Object bean = beanFactory.getBean("BeanName") 139 * } 140 * 141 * BeanClassLoaderAware 142 * ClassLoader 是類加載器,在此方法里只能獲取資源和設置加載器狀態 143 * @Override 144 * public void setBeanClassLoader(ClassLoader classLoader) { 145 * ClassLoader parent = classLoader.getParent(); 146 * } 147 * 148 * ResourceLoaderAware 149 * ResourceLoader 用於獲取類加載器和根據路徑獲取資源 150 * public void setResourceLoader(ResourceLoader resourceLoader) { 151 * ClassLoader classLoader = resourceLoader.getClassLoader(); 152 * } 153 */
154 @AliasFor("value") 155 Class<?>[] classes() default {}; 156
157 /** 158 * 這個參數是 classes 或 value 的替代參數,主要用於 ASPECTJ 類型和 REGEX 類型 159 * ASPECTJ 為 ASPECTJ 表達式 160 * @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "spring.annotation..*") 161 * REGEX 參數為 正則表達式 162 * @ComponentScan.Filter(type = FilterType.REGEX, pattern = "^[A-Za-z.]+Dao$") 163 */
164 String[] pattern() default {}; 165
166 } 167
168 }
Environment
1 public interface Environment extends PropertyResolver { 2
3 /**
4 * 這個方法用來取到 ConfigurableEnvironment.setActiveProfiles(String...) 設置的值,這個方法搭配 @Profile 注解使用 5 * 如 : 6 * @Bean() 7 * @Profile("dev") 8 * public Student student() { 9 * System.out.println("createStudent"); 10 * return new Student("王五", 18); 11 * } 12 * 13 * @Test 14 * public void testImport(){ 15 * AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 16 * ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment(); 17 * environment.setActiveProfiles("dev"); 18 * annotationConfigApplicationContext.register(MainConfig2.class); 19 * annotationConfigApplicationContext.refresh(); 20 * String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames(); 21 * for (String s : beanDefinitionNames) { 22 * System.out.println(s); 23 * } 24 * } 25 * 26 * @Profile("dev") 27 * environment.setActiveProfiles("dev"); 28 * 當 setActiveProfiles 里的值在 @Profile 中存在,此時就會注冊這個Bean 29 * 而 getActiveProfiles 取到的也是 setActiveProfiles 中的值 30 */
31 String[] getActiveProfiles(); 32
33 /**
34 * 未明確設置有效配置文件時,返回默認有效的配置文件集 35 */
36 String[] getDefaultProfiles(); 37
38 /**
39 * 返回一個或多個給定的配置文件是否處於活動狀態,或者在沒有顯式活動 40 * 配置文件的情況下,返回一個或多個給定的配置文件是否包含在默認配置 41 * 文件集中。 如果個人資料以“!”開頭 邏輯取反,即如果給定的配置文件未 42 * 激活,則該方法將返回true。 例如,如果配置文件“ p1”處於活動狀態或“ 43 * p2”處於非活動狀態,則env.acceptsProfiles(“ p1”,“!p2”)將返回true。 44 * 不推薦使用 45 */
46 @Deprecated 47 boolean acceptsProfiles(String... profiles); 48
49 /**
50 * Return whether the {@linkplain #getActiveProfiles() active profiles} 51 * 返回活動概要文件是否 與給定Profiles謂詞匹配。,不推薦使用 52 */
53 boolean acceptsProfiles(Profiles profiles); 54
55 }
PropertyResolver
1 public interface PropertyResolver { 2
3 /**
4 * 判斷給定鍵的值是否為 null 5 */
6 boolean containsProperty(String key); 7
8 /**
9 * 返回給定鍵的值,如果不存在就返回 null,可獲取配置文件中的數據 10 * @param key 鍵 11 */
12 @Nullable 13 String getProperty(String key); 14
15 /**
16 * 返回給定鍵的值,值若為 null 則返回默認值 17 * @param key 鍵 18 * @param defaultValue 指定返回的默認值 19 */
20 String getProperty(String key, String defaultValue); 21
22 /**
23 * 返回給定鍵的值,如果不存在就返回 null,需要指定返回的類型 24 * @param key 鍵 25 * @param targetType 指定返回的類型,不要用基礎類型,用包裝類 26 */
27 @Nullable 28 <T> T getProperty(String key, Class<T> targetType); 29
30 /**
31 * 返回給定鍵的值,如果不存在就返回默認值,需要指定返回的類型 32 * @param key 鍵 33 * @param targetType 指定的返回類型 34 * @param defaultValue 指定的返回默認值 35 *
36 */
37 <T> T getProperty(String key, Class<T> targetType, T defaultValue); 38
39 /**
40 * 返回給定鍵的值,如果不存在則拋出 IllegalStateException 異常 41 */
42 String getRequiredProperty(String key) throws IllegalStateException; 43
44 /**
45 * 返回給定鍵的值,如果不存在則拋出 IllegalStateException 異常,需要指定返回類型 46 * targetType 指定返回的類型 47 */
48 <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException; 49
50 /**
51 * 在給定的文本中解析$ {...}占位符,將其替換為由 getProperty 解析的相應屬性值。 沒有默認值的無法解析的占位符將被忽略,並按原樣傳遞。 52 * 比如 String str = environment.resolvePlaceholders("這是${os.name}系統"); 53 * 返回值為 這是Windows 10系統 54 * @param text 需要被被解析的文本,若文本為 null 則會拋出IllegalStateException 異常 55 */
56 String resolvePlaceholders(String text); 57
58 /**
59 * 在給定的文本中解析$ {...}占位符,將其替換為 getProperty 解析的相應屬性值。 沒有默認值的無法解析的占位符將導致引發IllegalArgumentException。 60 * 比如 String s = environment.resolveRequiredPlaceholders("${}"); 61 * 就會拋出 IllegalArgumentException異常 62 */
63 String resolveRequiredPlaceholders(String text) throws IllegalArgumentException; 64
65 }
ProPertyResolver.getProperty() 能獲取的數據(->左邊是鍵,右邊是值)
PropertyResolver.getProperty 獲取的數據有兩類 systemProperties 和 systemEnvironment systemProperties 數據有(環境會有不同,數據會有區別) java.runtime.name -> Java(TM) SE Runtime Environment sun.boot.library.path -> D:\Program Files\Java\jdk1.8.0_171\jre\bin java.vm.version -> 25.171-b11 java.vm.vendor -> Oracle Corporation java.vendor.url -> http://java.oracle.com/
path.separator -> ; java.vm.name -> Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg -> sun.io user.country -> CN user.script -> sun.java.launcher -> SUN_STANDARD sun.os.patch.level -> java.vm.specification.name -> Java Virtual Machine Specification user.dir -> D:\IDEAworkspaces\learn intellij.debug.agent -> true java.runtime.version -> 1.8.0_171-b11 java.awt.graphicsenv -> sun.awt.Win32GraphicsEnvironment java.endorsed.dirs -> D:\Program Files\Java\jdk1.8.0_171\jre\lib\endorsed os.arch -> amd64 java.io.tmpdir -> C:\Users\ADMINI~1\AppData\Local\Temp\ line.separator -> "\r\n" java.vm.specification.vendor -> Oracle Corporation user.variant -> os.name -> Windows 10 sun.jnu.encoding -> GBK java.library.path -> D:\Program Files\Java\jdk1.8.0_171\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\Program Files (x86)\NetSarang\Xshell 6\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Program Files\MySQL\MySQL Server 5.5\bin;D:\Program Files\Java\jdk1.8.0_171\bin;D:\Program Files\apache-tomcat-8.5.33\bin;D:\Program Files\apache-maven-3.5.4\bin;D:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\Wirel jboss.modules.system.pkgs -> com.intellij.rt java.specification.name -> Java Platform API Specification java.class.version -> 52.0 sun.management.compiler -> HotSpot 64-Bit Tiered Compilers os.version -> 10.0 user.home -> C:\Users\Administrator user.timezone -> java.awt.printerjob -> sun.awt.windows.WPrinterJob file.encoding -> UTF-8 java.specification.version -> 1.8 java.class.path -> D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit5-rt.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.ja user.name -> Administrator java.vm.specification.version -> 1.8 sun.java.command -> com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 IocTest,testImport java.home -> D:\Program Files\Java\jdk1.8.0_171\jre sun.arch.data.model -> 64 user.language -> zh java.specification.vendor -> Oracle Corporation awt.toolkit -> sun.awt.windows.WToolkit java.vm.info -> mixed mode java.version -> 1.8.0_171 java.ext.dirs -> D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext sun.boot.class.path -> D:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\sunrsasign.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_171\jre\classes java.vendor -> Oracle Corporation file.separator -> \ java.vendor.url.bug -> http://bugreport.sun.com/bugreport/
idea.test.cyclic.buffer.size -> 1048576 sun.io.unicode.encoding -> UnicodeLittle sun.cpu.endian -> little sun.desktop -> windows sun.cpu.isalist -> amd64 systemEnvironment數據有 USERDOMAIN_ROAMINGPROFILE -> LAPTOP-FLP30M67 LOCALAPPDATA -> C:\Users\Administrator\AppData\Local PROCESSOR_LEVEL -> 6 IntelliJ IDEA -> D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\bin; USERDOMAIN -> LAPTOP-FLP30M67 FPS_BROWSER_APP_PROFILE_STRING -> Internet Explorer LOGONSERVER -> \\LAPTOP-FLP30M67 JAVA_HOME -> D:\Program Files\Java\jdk1.8.0_171 SESSIONNAME -> Console ALLUSERSPROFILE -> C:\ProgramData PROCESSOR_ARCHITECTURE -> AMD64 TNS_ADMIN -> D:\Program Files\instantclient_18_3\network\admin PSModulePath -> C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules SystemDrive -> C: MAVEN_HOME -> D:\Program Files\apache-maven-3.5.4 OneDrive -> C:\Users\Administrator\OneDrive APPDATA -> C:\Users\Administrator\AppData\Roaming USERNAME -> Administrator ORACLE_HOME -> D:\Program Files\instantclient_18_3 ProgramFiles(x86) -> C:\Program Files (x86) CommonProgramFiles -> C:\Program Files\Common Files Path -> D:\Program Files (x86)\NetSarang\Xshell 6\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Program Files\MySQL\MySQL Server 5.5\bin;D:\Program Files\Java\jdk1.8.0_171\bin;D:\Program Files\apache-tomcat-8.5.33\bin;D:\Program Files\apache-maven-3.5.4\bin;D:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\Program Files\nodejs\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;d:\Prog FPS_BROWSER_USER_PROFILE_STRING -> Default PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC DriverData -> C:\Windows\System32\Drivers\DriverData OS -> Windows_NT COMPUTERNAME -> LAPTOP-FLP30M67 PROCESSOR_REVISION -> 8e0a CommonProgramW6432 -> C:\Program Files\Common Files NLS_LANG -> AMERICAN_AMERICA.UTF8 ComSpec -> C:\WINDOWS\system32\cmd.exe ProgramData -> C:\ProgramData ProgramW6432 -> C:\Program Files HOMEPATH -> \Users\Administrator SystemRoot -> C:\WINDOWS TEMP -> C:\Users\ADMINI~1\AppData\Local\Temp HOMEDRIVE -> C: PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 142 Stepping 10, GenuineIntel USERPROFILE -> C:\Users\Administrator TMP -> C:\Users\ADMINI~1\AppData\Local\Temp CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files ProgramFiles -> C:\Program Files PUBLIC -> C:\Users\Public NUMBER_OF_PROCESSORS -> 8 windir -> C:\WINDOWS =:: -> ::\ IDEA_INITIAL_DIRECTORY -> C:\Users\Administrator\Desktop