1.å¦ä½ç¨mavenå°java8åç代ç ç¼è¯ä¸ºjava6å¹³å°ç
2.java getResourceAsStream方法
3.@Import({ AutoConfigurationImportSelector.class})
å¦ä½ç¨mavenå°java8åç代ç ç¼è¯ä¸ºjava6å¹³å°ç
ããå¨ä¸è¬çJavaåºç¨å¼åè¿ç¨ä¸ï¼å¼å人å使ç¨Javaçæ¹å¼æ¯è¾ç®åãæå¼æ¯ç¨çIDEï¼ç¼åJavaæºä»£ç ï¼åå©ç¨IDEæä¾çåè½ç´æ¥è¿è¡Java ç¨åºå°±å¯ä»¥äºãè¿ç§å¼å模å¼èåçè¿ç¨æ¯ï¼å¼å人åç¼åçæ¯Javaæºä»£ç æ件ï¼.javaï¼ï¼IDEä¼è´è´£è°ç¨Javaçç¼è¯å¨æJavaæºä»£ç ç¼è¯æå¹³å°æ å ³çåè代ç ï¼byte codeï¼ï¼ä»¥ç±»æ件çå½¢å¼ä¿åå¨ç£çä¸ï¼.classï¼ãJavaèææºï¼JVMï¼ä¼è´è´£æJavaåè代ç å 载并æ§è¡ãJavaéè¿è¿ç§æ¹å¼æ¥å®ç°å ¶âç¼åä¸æ¬¡ï¼å°å¤è¿è¡ï¼Write once, run anywhereï¼â çç®æ ãJavaç±»æ件ä¸å å«çåè代ç å¯ä»¥è¢«ä¸åå¹³å°ä¸çJVMæ使ç¨ãJavaåè代ç ä¸ä» å¯ä»¥ä»¥æ件形å¼åå¨äºç£çä¸ï¼ä¹å¯ä»¥éè¿ç½ç»æ¹å¼æ¥ä¸è½½ï¼è¿å¯ä»¥åªåå¨äºå åä¸ãJVMä¸çç±»å è½½å¨ä¼è´è´£ä»å å«åè代ç çåèæ°ç»ï¼byte[]ï¼ä¸å®ä¹åºJavaç±»ãå¨æäºæ åµä¸ï¼å¯è½ä¼éè¦å¨æççæ Javaåè代ç ï¼ææ¯å¯¹å·²æçJavaåè代ç è¿è¡ä¿®æ¹ãè¿ä¸ªæ¶åå°±éè¦ç¨å°æ¬æä¸å°è¦ä»ç»çç¸å ³ææ¯ãé¦å ä»ç»ä¸ä¸å¦ä½å¨æç¼è¯Javaæºæ件ã
ããå¨æç¼è¯Javaæºæ件
ããå¨ä¸è¬æ åµä¸ï¼å¼å人åé½æ¯å¨ç¨åºè¿è¡ä¹åå°±ç¼åå®æäºå ¨é¨çJavaæºä»£ç 并ä¸æåç¼è¯ã对æäºåºç¨æ¥è¯´ï¼Javaæºä»£ç çå 容å¨è¿è¡æ¶å»æè½ç¡®å®ãè¿ä¸ªæ¶åå°±éè¦å¨æç¼è¯æºä»£ç æ¥çæJavaåè代ç ï¼åç±JVMæ¥å è½½æ§è¡ãå ¸åçåºæ¯æ¯å¾å¤ç®æ³ç«èµçå¨çº¿è¯æµç³»ç»ï¼å¦PKU JudgeOnlineï¼ï¼å 许ç¨æ·ä¸ä¼ Java代ç ï¼ç±ç³»ç»å¨åå°ç¼è¯ãè¿è¡å¹¶è¿è¡å¤å®ãå¨å¨æç¼è¯Javaæºæ件æ¶ï¼ä½¿ç¨çåæ³æ¯ç´æ¥å¨ç¨åºä¸è°ç¨Javaç¼è¯å¨ã
ããJSR å¼å ¥äºJavaç¼è¯å¨APIãå¦æ使ç¨JDK 6çè¯ï¼å¯ä»¥éè¿æ¤APIæ¥å¨æç¼è¯Java代ç ãæ¯å¦ä¸é¢ç代ç ç¨æ¥å¨æç¼è¯æç®åçHello Worldç±»ã该Javaç±»ç代ç æ¯ä¿åå¨ä¸ä¸ªå符串ä¸çã
ãã public class CompilerTest {
ãã public static void main(String[] args) throws Exception {
ãã String source = "public class Main { public static void main(String[] args) { System.out.println(\"Hello World!\");} }";
ãã JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ãã StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
ãã StringSourceJavaObject sourceObject = newCompilerTest.StringSourceJavaObject("Main", source);
ãã Iterable< extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
ãã CompilationTask task = compiler.getTask(null, fileManager, null,null, null, fileObjects);
ãã boolean result = task.call();
ãã if (result) {
ãã System.out.println("ç¼è¯æåã");
ãã }
ãã }
ãã
ãã static class StringSourceJavaObject extends SimpleJavaFileObject {
ãã
ãã private String content = null;
ãã public StringSourceJavaObject(String name, String content) throwsURISyntaxException {
ãã super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
ãã this.content = content;
ãã }
ãã
ãã public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
ãã return content;
ãã }
ãã }
ãã }
ããå¦æä¸è½ä½¿ç¨JDK 6æä¾çJavaç¼è¯å¨APIçè¯ï¼å¯ä»¥ä½¿ç¨JDKä¸çå·¥å ·ç±»com.sun.tools.javac.Mainï¼ä¸è¿è¯¥å·¥å ·ç±»åªè½ç¼è¯åæ¾å¨ç£çä¸çæ件ï¼ç±»ä¼¼äºç´æ¥ä½¿ç¨javacå½ä»¤ã
ããå¦å¤ä¸ä¸ªå¯ç¨çå·¥å ·æ¯Eclipse JDT Coreæä¾çç¼è¯å¨ãè¿æ¯Eclipse Javaå¼åç¯å¢ä½¿ç¨çå¢éå¼Javaç¼è¯å¨ï¼æ¯æè¿è¡åè°è¯æé误ç代ç ã该ç¼è¯å¨ä¹å¯ä»¥åç¬ä½¿ç¨ãPlayæ¡æ¶å¨å é¨ä½¿ç¨äºJDTçç¼è¯å¨æ¥å¨æç¼è¯Javaæºä»£ç ãå¨å¼å模å¼ä¸ï¼Playæ¡æ¶ä¼å®ææ«æ项ç®ä¸çJavaæºä»£ç æ件ï¼ä¸æ¦åç°æä¿®æ¹ï¼ä¼èªå¨ç¼è¯ Javaæºä»£ç ãå æ¤å¨ä¿®æ¹ä»£ç ä¹åï¼å·æ°é¡µé¢å°±å¯ä»¥çå°ååã使ç¨è¿äºå¨æç¼è¯çæ¹å¼çæ¶åï¼éè¦ç¡®ä¿JDKä¸çtools.jarå¨åºç¨ç CLASSPATHä¸ã
ããä¸é¢ä»ç»ä¸ä¸ªä¾åï¼æ¯å ³äºå¦ä½å¨Javaéé¢åååè¿ç®ï¼æ¯å¦æ±åºæ¥(3+4)*7-çå¼ãä¸è¬çåæ³æ¯åæè¾å ¥çè¿ç®è¡¨è¾¾å¼ï¼èªå·±æ¥æ¨¡æ计ç®è¿ç¨ãèèå°æ¬å·çåå¨åè¿ç®ç¬¦çä¼å 级çé®é¢ï¼è¿æ ·ç计ç®è¿ç¨ä¼æ¯è¾å¤æï¼èä¸å®¹æåºéãå¦å¤ä¸ç§åæ³æ¯å¯ä»¥ç¨JSR å¼å ¥çèæ¬è¯è¨æ¯æï¼ç´æ¥æè¾å ¥ç表达å¼å½åJavaScriptææ¯JavaFXèæ¬æ¥æ§è¡ï¼å¾å°ç»æãä¸é¢ç代ç 使ç¨çåæ³æ¯å¨æçæJavaæºä»£ç 并ç¼è¯ï¼æ¥çå è½½Javaç±»æ¥æ§è¡å¹¶è·åç»æãè¿ç§åæ³å®å ¨ä½¿ç¨Javaæ¥å®ç°ã
ãã private static double calculate(String expr) throws CalculationException {
ãã String className = "CalculatorMain";
ãã String methodName = "calculate";
ãã String source = "public class " + className
ãã + " { public static double " + methodName + "() { return " + expr +"; } }";
ãã //çç¥å¨æç¼è¯Javaæºä»£ç çç¸å ³ä»£ç ï¼åè§ä¸ä¸è
ãã boolean result = task.call();
ãã if (result) {
ãã ClassLoader loader = Calculator.class.getClassLoader();
ãã try {
ãã Class<?> clazz = loader.loadClass(className);
ãã Method method = clazz.getMethod(methodName, new Class<?>[] { });
ãã Object value = method.invoke(null, new Object[] { });
ãã return (Double) value;
ãã } catch (Exception e) {
ãã throw new CalculationException("å é¨é误ã");
ãã }
ãã } else {
ãã throw new CalculationException("é误ç表达å¼ã");
ãã }
ãã }
ããä¸é¢ç代ç ç»åºäºä½¿ç¨å¨æçæçJavaåè代ç çåºæ¬æ¨¡å¼ï¼å³éè¿ç±»å è½½å¨æ¥å è½½åè代ç ï¼å建Javaç±»ç对象çå®ä¾ï¼åéè¿Javaåå°APIæ¥è°ç¨å¯¹è±¡ä¸çæ¹æ³ã
ããJavaåè代ç å¢å¼º
ããJava åè代ç å¢å¼ºæçæ¯å¨Javaåè代ç çæä¹åï¼å¯¹å ¶è¿è¡ä¿®æ¹ï¼å¢å¼ºå ¶åè½ãè¿ç§åæ³ç¸å½äºå¯¹åºç¨ç¨åºçäºè¿å¶æ件è¿è¡ä¿®æ¹ãå¨å¾å¤Javaæ¡æ¶ä¸é½å¯ä»¥è§å°è¿ç§å®ç°æ¹å¼ãJavaåè代ç å¢å¼ºé常ä¸Javaæºæ件ä¸ç注解ï¼annotationï¼ä¸å使ç¨ã注解å¨Javaæºä»£ç ä¸å£°æäºéè¦å¢å¼ºçè¡ä¸ºåç¸å ³çå æ°æ®ï¼ç±æ¡æ¶å¨è¿è¡æ¶å»å®æ对åè代ç çå¢å¼ºãJavaåè代ç å¢å¼ºåºç¨çåºæ¯æ¯è¾å¤ï¼ä¸è¬é½éä¸å¨åå°åä½ä»£ç å对å¼å人åå±è½åºå±çå®ç°ç»èä¸ãç¨è¿JavaBeansç人å¯è½å¯¹å ¶ä¸é£äºå¿ 须添å çgetter/setteræ¹æ³æå°å¾ç¹çï¼å¹¶ä¸é¾ä»¥ç»´æ¤ãèéè¿åè代ç å¢å¼ºï¼å¼å人ååªéè¦å£°æBeanä¸çå±æ§å³å¯ï¼getter/setteræ¹æ³å¯ä»¥éè¿ä¿®æ¹åè代ç æ¥èªå¨æ·»å ãç¨è¿JPAç人ï¼å¨è°è¯ç¨åºçæ¶åï¼ä¼åç°å®ä½ç±»ä¸è¢«æ·»å äºä¸äºé¢å¤ç ååæ¹æ³ãè¿äºååæ¹æ³æ¯å¨è¿è¡æ¶å»ç±JPAçå®ç°å¨ææ·»å çãåè代ç å¢å¼ºå¨é¢åæ¹é¢ç¼ç¨ï¼AOPï¼çä¸äºå®ç°ä¸ä¹æ使ç¨ã
java getResourceAsStream方法
J2SE中的getResourceAsStream用法有以下几种:
第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,源码用同时有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目录的法方法mud 源码 下载子目录下,例如:com.x.y 下有类me.class ,源码用同时在 com.x.y.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目录下,法方法怎么删除git源码也不在子目录下,源码用键盘侠原始源码例如:com.x.y 下有类me.class ,法方法同时在 com.x.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("/com/x/file/myfile.xml");
总结一下,源码用可能只是法方法两种写法
第一:前面有 “ / ”
“ / ”代表了工程的根目录,例如工程名叫做myproject,源码用“ / ”代表了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面没有 “ / ”
代表当前类的法方法目录
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");
注:
getResourceAsStream读取的文件路径只局限与工程的源文件夹中,包括在工程src根目录下,源码用以及类包里面任何位置,法方法香港福星彩源码但是源码用如果配置文件路径是在除了源文件夹之外的其他文件夹中时,该方法是法方法飞屏软件源码用不了的。
@Import({ AutoConfigurationImportSelector.class})
ï¼2ï¼@Import({ AutoConfigurationImportSelector.class})ï¼å°AutoConfigurationImportSelectorè¿ä¸ªç±»å¯¼å ¥å°spring容å¨ä¸ï¼AutoConfigurationImportSelectorå¯ä»¥å¸®å©springbootåºç¨å°ææ符åæ¡ä»¶ç@Configurationé ç½®é½å è½½å°å½åSpringBootå建并使ç¨çIoC容å¨(ApplicationContext)ä¸
继ç»ç 究AutoConfigurationImportSelectorè¿ä¸ªç±»ï¼éè¿æºç åæè¿ä¸ªç±»ä¸æ¯éè¿selectImportsè¿ä¸ªæ¹æ³åè¯springbooté½éè¦å¯¼å ¥é£äºç»ä»¶ï¼
![image-](./images/image-.png)
æ·±å ¥ç 究loadMetadataæ¹æ³
![image-](./images/image-.png)
æ·±å ¥getCandidateConfigurationsæ¹æ³
个æ¹æ³ä¸æä¸ä¸ªéè¦æ¹æ³loadFactoryNamesï¼è¿ä¸ªæ¹æ³æ¯è®©SpringFactoryLoaderå»å è½½ä¸äºç»ä»¶çååã
![image-](./images/image-.png)
继ç»ç¹å¼loadFactoryæ¹æ³
```java
public static ListloadFactoryNames(Class factoryClass, @Nullable ClassLoaderclassLoader) {
//è·ååºå ¥çé®
String factoryClassName = factoryClass.getName();
return(List)loadSpringFactories(classLoader).getOrDefault(factoryClassName,Collections.emptyList());
}
private static Map>loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap result =(MultiValueMap)cache.get(classLoader);
if (result != null) {
return result;
} else {
try {
//å¦æç±»å è½½å¨ä¸ä¸ºnullï¼åå 载类路å¾ä¸spring.factoriesæ件ï¼å°å ¶ä¸è®¾ç½®çé 置类çå ¨è·¯å¾ä¿¡æ¯å°è£ 为Enumeration类对象
Enumeration urls =classLoader != null ?classLoader.getResources("META-INF/spring.factories") :ClassLoader.getSystemResources("META-INF/spring.factories");
LinkedMultiValueMap result =new LinkedMultiValueMap();
//循ç¯Enumeration类对象ï¼æ ¹æ®ç¸åºçèç¹ä¿¡æ¯çæProperties对象ï¼éè¿ä¼ å ¥çé®è·åå¼ï¼å¨å°å¼åå²ä¸ºä¸ä¸ªä¸ªå°çå符串转å为Arrayï¼æ¹æ³resultéåä¸
while(urls.hasMoreElements()) {
URL url =(URL)urls.nextElement();
UrlResource resource = newUrlResource(url);
Properties properties =PropertiesLoaderUtils.loadProperties(resource);
Iterator var6 =properties.entrySet().iterator();
while(var6.hasNext()) {
Entry entry= (Entry)var6.next();
String factoryClassName= ((String)entry.getKey()).trim();
String[] var9 =StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
int var =var9.length;
for(int var = 0;var < var; ++var) {
String factoryName= var9[var];
result.add(factoryClassName, factoryName.trim());
}
}
}
cache.put(classLoader, result);
return result;
```
ä¼å»è¯»åä¸ä¸ªsprin g.factoriesçæ件ï¼è¯»åä¸å°ä¼è¡¨è¿ä¸ªé误ï¼æ们继ç»æ ¹æ®ä¼çå°ï¼æç»è·¯å¾çé¿è¿æ ·ï¼èè¿ä¸ªæ¯springæä¾çä¸ä¸ªå·¥å ·ç±»
```java
public final class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION ="META-INF/spring.factories";
}
```
å®å ¶å®æ¯å»å è½½ä¸ä¸ªå¤é¨çæ件ï¼èè¿æ件æ¯å¨
![image-](./images/image-.png)
![image-](./images/image-.png)
@EnableAutoConfigurationå°±æ¯ä»classpathä¸æ寻META-INF/spring.factoriesé ç½®æ件ï¼å¹¶å°å ¶ä¸org.springframework.boot.autoconfigure.EnableutoConfiguration对åºçé 置项éè¿åå°ï¼Java Refletionï¼å®ä¾å为对åºçæ 注äº@ConfigurationçJavaConfigå½¢å¼çé 置类ï¼å¹¶å è½½å°IOC容å¨ä¸
以ååç项ç®ä¸ºä¾ï¼å¨é¡¹ç®ä¸å å ¥äºWebç¯å¢ä¾èµå¯å¨å¨ï¼å¯¹åºçWebMvcAutoConfigurationèªå¨é 置类就ä¼çæï¼æå¼è¯¥èªå¨é 置类ä¼åç°ï¼å¨è¯¥é 置类ä¸éè¿å ¨æ³¨è§£é 置类çæ¹å¼å¯¹Spring MVCè¿è¡æéç¯å¢è¿è¡äºé»è®¤é ç½®ï¼å æ¬é»è®¤åç¼ãé»è®¤åç¼ãè§å¾è§£æå¨ãMVCæ ¡éªå¨çãèè¿äºèªå¨é 置类çæ¬è´¨æ¯ä¼ ç»Spring MVCæ¡æ¶ä¸å¯¹åºçXMLé ç½®æ件ï¼åªä¸è¿å¨Spring Bootä¸ä»¥èªå¨é 置类çå½¢å¼è¿è¡äºé¢å é ç½®ãå æ¤ï¼å¨Spring Boot项ç®ä¸å å ¥ç¸å ³ä¾èµå¯å¨å¨åï¼åºæ¬ä¸ä¸éè¦ä»»ä½é 置就å¯ä»¥è¿è¡ç¨åºï¼å½ç¶ï¼æ们ä¹å¯ä»¥å¯¹è¿äºèªå¨é 置类ä¸é»è®¤çé ç½®è¿è¡æ´æ¹
**æ»ç»
**å æ¤springbootåºå±å®ç°èªå¨é ç½®çæ¥éª¤æ¯ï¼
1. springbootåºç¨å¯å¨ï¼
2. @SpringBootApplicationèµ·ä½ç¨ï¼
3. @EnableAutoConfigurationï¼
4. @AutoConfigurationPackageï¼è¿ä¸ªç»å注解主è¦æ¯@Import(AutoConfigurationPackages.Registrar.class)ï¼å®éè¿å°Registrarç±»å¯¼å ¥å°å®¹å¨ä¸ï¼èRegistrarç±»ä½ç¨æ¯æ«æ主é 置类å级ç®å½ä»¥ååå ï¼å¹¶å°ç¸åºçç»ä»¶å¯¼å ¥å°springbootå建管çç容å¨ä¸ï¼
5.
@Import(AutoConfigurationImportSelector.class)ï¼å®éè¿å°AutoConfigurationImportSelectorç±»å¯¼å ¥å°å®¹å¨ä¸ï¼AutoConfigurationImportSelectorç±»ä½ç¨æ¯éè¿selectImportsæ¹æ³æ§è¡çè¿ç¨ä¸ï¼ä¼ä½¿ç¨å é¨å·¥å ·ç±»SpringFactoriesLoaderï¼æ¥æ¾classpathä¸ææjarå ä¸çMETA-INF/spring.factoriesè¿è¡å è½½ï¼å®ç°å°é 置类信æ¯äº¤ç»SpringFactoryå è½½å¨è¿è¡ä¸ç³»åç容å¨å建è¿ç¨
åå¦äºæå¾æè²çãJavaå·¥ç¨å¸é«èªè®ç»è¥ãï¼çå°åå¦å°çç¹å°±åçäºãå¸ææå¾è½ç»ææ¨å°æ³å»çå ¬å¸ï¼ç®æ ï¼åèï¼ï¼