Skip to content

Commit dea6d26

Browse files
committed
SpringBoot学习
1 parent d13aa64 commit dea6d26

8 files changed

Lines changed: 70 additions & 24 deletions

File tree

springboot/src/main/java/com/tuacy/study/springboot/StudySpringBootApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.tuacy.study.springboot.configuration.UserInfo;
44
import com.tuacy.study.springboot.hook.classPathBeanDefinitionScanner.BeanIocScan;
55
import com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.RunStartScan;
6+
import com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.customercomponent.CustomerComponentScan;
67
import org.springframework.boot.SpringApplication;
78
import org.springframework.boot.autoconfigure.SpringBootApplication;
89
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -12,6 +13,7 @@
1213
@RunStartScan(basePackages = {"com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.runstart"})
1314
@BeanIocScan(basePackages = {"com.tuacy.study.springboot.hook.classPathBeanDefinitionScanner.autoioc"})
1415
@EnableConfigurationProperties(value = {UserInfo.class})
16+
@CustomerComponentScan(basePackages = "com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.customercomponent")
1517
public class StudySpringBootApplication {
1618

1719
public static void main(String[] args) {

springboot/src/main/java/com/tuacy/study/springboot/aware/MessageSourceAware/BeanMessageSourceAware.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public void setMessageSource(MessageSource messageSource) {
2222

2323
public void printMessage() {
2424

25-
String hello = messageSource.getMessage("hello", null, "", null);
26-
27-
String name = messageSource.getMessage("hello",
28-
new Object[]{28, "http://www.yiibai.com"}, Locale.US);
29-
30-
System.out.println("Customer name (English) : " + name);
31-
32-
String namechinese = messageSource.getMessage("customer.name",
33-
new Object[]{28, "http://www.yiibai.com"},
34-
Locale.SIMPLIFIED_CHINESE);
35-
36-
System.out.println("Customer name (Chinese) : " + namechinese);
25+
// String hello = messageSource.getMessage("hello", null, "", null);
26+
//
27+
// String name = messageSource.getMessage("hello",
28+
// new Object[]{28, "http://www.yiibai.com"}, Locale.US);
29+
//
30+
// System.out.println("Customer name (English) : " + name);
31+
//
32+
// String namechinese = messageSource.getMessage("customer.name",
33+
// new Object[]{28, "http://www.yiibai.com"},
34+
// Locale.SIMPLIFIED_CHINESE);
35+
//
36+
// System.out.println("Customer name (Chinese) : " + namechinese);
3737
}
3838
}

springboot/src/main/java/com/tuacy/study/springboot/hook/importBeanDefinitionRegistrar/RunStartManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ public List<Class<IRunStart>> getClassesList(String[] packageNames) throws Excep
7777
if (resources.length == 0) {
7878
return Collections.emptyList();
7979
}
80-
80+
String packageBasePath = packageName.replace(".", "/");
8181
for (Resource resource : resources) {
8282
try {
83-
classes.add((Thread.currentThread().getContextClassLoader().loadClass((packageName + "." + resource.getFilename()).replace(".class", ""))));
83+
String resourceUriPath = resource.getURL().getPath();
84+
String resourcePackage = resourceUriPath.substring(resourceUriPath.indexOf(packageBasePath)).replace("/", ".").replace(".class", "");
85+
classes.add((Thread.currentThread().getContextClassLoader().loadClass(resourcePackage)));
8486
} catch (Exception ignored) {
8587
}
8688
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.customercomponent;
22

3-
import org.springframework.context.annotation.Import;
4-
53
import java.lang.annotation.*;
64

75
/**
86
* @name: RunStart
97
* @author: tuacy.
108
* @date: 2019/8/16.
119
* @version: 1.0
12-
* @Description: 添加这个注解的类在程序刚运行的时候,自动调用指定的函数
10+
* @Description: 和@Component的功能一样
1311
*/
1412
@Retention(RetentionPolicy.RUNTIME)
1513
@Target(ElementType.TYPE)
1614
@Documented
17-
@Import(CustomerComponentScannerRegister.class)
1815
public @interface CustomerComponent {
1916

2017
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.customercomponent;
2+
3+
/**
4+
* @name: CustomerComponentBean
5+
* @author: tuacy.
6+
* @date: 2019/9/19.
7+
* @version: 1.0
8+
* @Description:
9+
*/
10+
@CustomerComponent
11+
public class CustomerComponentBean {
12+
13+
public void testFunction() {
14+
System.out.println("aaaaaaaaaaaa");
15+
}
16+
17+
}

springboot/src/main/java/com/tuacy/study/springboot/hook/importBeanDefinitionRegistrar/customercomponent/MyClassPathBeanDefinitionScanner.java renamed to springboot/src/main/java/com/tuacy/study/springboot/hook/importBeanDefinitionRegistrar/customercomponent/CustomerComponentClassPathBeanDefinitionScanner.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@
88
import java.util.Set;
99

1010
/**
11-
* @name: MyClassPathBeanDefinitionScanner
11+
* @name: CustomerComponentClassPathBeanDefinitionScanner
1212
* @author: tuacy.
1313
* @date: 2019/9/16.
1414
* @version: 1.0
1515
* @Description:
1616
*/
17-
public class MyClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {
17+
public class CustomerComponentClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {
1818

19-
public MyClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
19+
public CustomerComponentClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
2020
super(registry, useDefaultFilters);
2121
}
2222

2323
protected void registerFilters() {
24+
// 只搜索添加了CustomerComponent注解的类
2425
addIncludeFilter(new AnnotationTypeFilter(CustomerComponent.class));
2526
}
2627

28+
/**
29+
* 指定收缩路径
30+
*/
2731
@Override
2832
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
2933
return super.doScan(basePackages);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.tuacy.study.springboot.hook.importBeanDefinitionRegistrar.customercomponent;
2+
3+
import org.springframework.context.annotation.Import;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* @name: RunStart
9+
* @author: tuacy.
10+
* @date: 2019/8/16.
11+
* @version: 1.0
12+
* @Description: 添加这个注解的类在程序刚运行的时候,自动调用指定的函数
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target(ElementType.TYPE)
16+
@Documented
17+
@Import(CustomerComponentScannerRegister.class)
18+
public @interface CustomerComponentScan {
19+
String[] basePackages() default "";
20+
}

springboot/src/main/java/com/tuacy/study/springboot/hook/importBeanDefinitionRegistrar/customercomponent/CustomerComponentScannerRegister.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616
*/
1717
public class CustomerComponentScannerRegister implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
1818

19+
private final static String PACKAGE_NAME_KEY = "basePackages";
20+
1921
private ResourceLoader resourceLoader;
2022

2123
@Override
2224
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
23-
AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(CustomerComponent.class.getName()));
25+
AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(CustomerComponentScan.class.getName()));
2426
if (annoAttrs == null || annoAttrs.isEmpty()) {
2527
return;
2628
}
27-
MyClassPathBeanDefinitionScanner scanner = new MyClassPathBeanDefinitionScanner(beanDefinitionRegistry, false);
29+
// 搜索路径
30+
String[] basePackages = (String[]) annoAttrs.get(PACKAGE_NAME_KEY);
31+
CustomerComponentClassPathBeanDefinitionScanner scanner = new CustomerComponentClassPathBeanDefinitionScanner(beanDefinitionRegistry, false);
2832
scanner.setResourceLoader(resourceLoader);
2933
scanner.registerFilters();
30-
scanner.doScan("com.faderw.school.domain");
34+
scanner.doScan(basePackages);
3135
}
3236

3337
@Override

0 commit comments

Comments
 (0)