Skip to content

Commit 934ccbf

Browse files
committed
深入学习Spring组件注册
1 parent c6feec9 commit 934ccbf

23 files changed

+418
-0
lines changed

50.Spring-Regist-Bean/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.0.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cc.mrbird</groupId>
12+
<artifactId>demo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<optional>true</optional>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-maven-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
43+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cc.mrbird;
2+
3+
import cc.mrbird.demo.config.WebConfig;
4+
import cc.mrbird.demo.service.CalculateService;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.WebApplicationType;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.builder.SpringApplicationBuilder;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
12+
13+
import java.util.Arrays;
14+
15+
@SpringBootApplication
16+
public class DemoApplication {
17+
18+
public static void main(String[] args) {
19+
ConfigurableApplicationContext context1 = new SpringApplicationBuilder(DemoApplication.class)
20+
.web(WebApplicationType.NONE)
21+
.profiles("java7")
22+
.run(args);
23+
24+
// 返回 IOC 容器,使用注解配置,传入配置类
25+
ApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);
26+
System.out.println("容器创建完毕");
27+
28+
// User user = context.getBean(User.class);
29+
// System.out.println(user);
30+
31+
// 查看 User 这个类在 Spring 容器中叫啥玩意
32+
// String[] beanNames = context.getBeanNamesForType(User.class);
33+
// Arrays.stream(beanNames).forEach(System.out::println);
34+
35+
// 查看基于注解的 IOC容器中所有组件名称
36+
String[] beanNames = context.getBeanDefinitionNames();
37+
Arrays.stream(beanNames).forEach(System.out::println);
38+
39+
// 组件的作用域
40+
// Object user1 = context.getBean("user");
41+
// Object user2 = context.getBean("user");
42+
// System.out.println(user1 == user2);
43+
44+
// 测试懒加载
45+
// Object user1 = context.getBean("user");
46+
// Object user2 = context.getBean("user");
47+
48+
// 测试 Profile
49+
CalculateService service = context1.getBean(CalculateService.class);
50+
System.out.println("求合结果: " + service.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
51+
52+
// FactoryBean测试
53+
Object cherry = context.getBean("cherryFactoryBean");
54+
System.out.println(cherry.getClass());
55+
56+
Object cherryFactoryBean = context.getBean("&cherryFactoryBean");
57+
System.out.println(cherryFactoryBean.getClass());
58+
59+
}
60+
}
61+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cc.mrbird.demo.condition;
2+
3+
import org.springframework.context.annotation.Condition;
4+
import org.springframework.context.annotation.ConditionContext;
5+
import org.springframework.core.type.AnnotatedTypeMetadata;
6+
7+
/**
8+
* @author MrBird
9+
*/
10+
public class MyCondition implements Condition {
11+
@Override
12+
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
13+
String osName = context.getEnvironment().getProperty("os.name");
14+
return osName != null && osName.contains("Windows");
15+
}
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cc.mrbird.demo.config;
2+
3+
import cc.mrbird.demo.condition.MyCondition;
4+
import cc.mrbird.demo.domain.Hello;
5+
import cc.mrbird.demo.domain.User;
6+
import cc.mrbird.demo.factory.CherryFactoryBean;
7+
import cc.mrbird.demo.filter.MyTypeFilter;
8+
import cc.mrbird.demo.register.MyImportBeanDefinitionRegistrar;
9+
import cc.mrbird.demo.selector.MyImportSelector;
10+
import lombok.Builder;
11+
import org.springframework.context.annotation.*;
12+
import org.springframework.context.annotation.ComponentScan.Filter;
13+
import org.springframework.stereotype.Controller;
14+
import org.springframework.stereotype.Repository;
15+
import org.springframework.stereotype.Service;
16+
17+
/**
18+
* @author MrBird
19+
*/
20+
@Configuration
21+
// @ComponentScan(value = "cc.mrbird.demo"
22+
// , excludeFilters = {
23+
// @Filter(type = FilterType.ANNOTATION,
24+
// classes = {Controller.class, Repository.class}),
25+
// @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = User.class)
26+
// @Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
27+
// }
28+
// includeFilters = {
29+
// @Filter(type = FilterType.ANNOTATION, classes = Service.class)
30+
// }, useDefaultFilters = false
31+
// )
32+
@Import({Hello.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
33+
public class WebConfig {
34+
35+
@Bean
36+
// @Conditional(MyCondition.class)
37+
// @Lazy
38+
// @Scope("prototype")
39+
public User user() {
40+
System.out.println("往IOC容器中注册user bean");
41+
return new User("mrbird", 18);
42+
}
43+
44+
@Bean
45+
public CherryFactoryBean cherryFactoryBean() {
46+
return new CherryFactoryBean();
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cc.mrbird.demo.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
5+
/**
6+
* @author MrBird
7+
*/
8+
@Controller
9+
public class UserController {
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cc.mrbird.demo.dao;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
/**
6+
* @author MrBird
7+
*/
8+
@Repository
9+
public class UserMapper {
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.mrbird.demo.domain;
2+
3+
/**
4+
* @author MrBird
5+
*/
6+
public class Apple {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.mrbird.demo.domain;
2+
3+
/**
4+
* @author MrBird
5+
*/
6+
public class Banana {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.mrbird.demo.domain;
2+
3+
/**
4+
* @author MrBird
5+
*/
6+
public class Cherry {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.mrbird.demo.domain;
2+
3+
/**
4+
* @author MrBird
5+
*/
6+
public class Hello {
7+
}

0 commit comments

Comments
 (0)