深入学习Spring组件注册
This commit is contained in:
parent
c6feec9ba6
commit
934ccbf08f
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.0.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>cc.mrbird</groupId>
|
||||||
|
<artifactId>demo</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>demo</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package cc.mrbird;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.config.WebConfig;
|
||||||
|
import cc.mrbird.demo.service.CalculateService;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.WebApplicationType;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class DemoApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ConfigurableApplicationContext context1 = new SpringApplicationBuilder(DemoApplication.class)
|
||||||
|
.web(WebApplicationType.NONE)
|
||||||
|
.profiles("java7")
|
||||||
|
.run(args);
|
||||||
|
|
||||||
|
// 返回 IOC 容器,使用注解配置,传入配置类
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);
|
||||||
|
System.out.println("容器创建完毕");
|
||||||
|
|
||||||
|
// User user = context.getBean(User.class);
|
||||||
|
// System.out.println(user);
|
||||||
|
|
||||||
|
// 查看 User 这个类在 Spring 容器中叫啥玩意
|
||||||
|
// String[] beanNames = context.getBeanNamesForType(User.class);
|
||||||
|
// Arrays.stream(beanNames).forEach(System.out::println);
|
||||||
|
|
||||||
|
// 查看基于注解的 IOC容器中所有组件名称
|
||||||
|
String[] beanNames = context.getBeanDefinitionNames();
|
||||||
|
Arrays.stream(beanNames).forEach(System.out::println);
|
||||||
|
|
||||||
|
// 组件的作用域
|
||||||
|
// Object user1 = context.getBean("user");
|
||||||
|
// Object user2 = context.getBean("user");
|
||||||
|
// System.out.println(user1 == user2);
|
||||||
|
|
||||||
|
// 测试懒加载
|
||||||
|
// Object user1 = context.getBean("user");
|
||||||
|
// Object user2 = context.getBean("user");
|
||||||
|
|
||||||
|
// 测试 Profile
|
||||||
|
CalculateService service = context1.getBean(CalculateService.class);
|
||||||
|
System.out.println("求合结果: " + service.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
|
||||||
|
|
||||||
|
// FactoryBean测试
|
||||||
|
Object cherry = context.getBean("cherryFactoryBean");
|
||||||
|
System.out.println(cherry.getClass());
|
||||||
|
|
||||||
|
Object cherryFactoryBean = context.getBean("&cherryFactoryBean");
|
||||||
|
System.out.println(cherryFactoryBean.getClass());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package cc.mrbird.demo.condition;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Condition;
|
||||||
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class MyCondition implements Condition {
|
||||||
|
@Override
|
||||||
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
|
String osName = context.getEnvironment().getProperty("os.name");
|
||||||
|
return osName != null && osName.contains("Windows");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package cc.mrbird.demo.config;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.condition.MyCondition;
|
||||||
|
import cc.mrbird.demo.domain.Hello;
|
||||||
|
import cc.mrbird.demo.domain.User;
|
||||||
|
import cc.mrbird.demo.factory.CherryFactoryBean;
|
||||||
|
import cc.mrbird.demo.filter.MyTypeFilter;
|
||||||
|
import cc.mrbird.demo.register.MyImportBeanDefinitionRegistrar;
|
||||||
|
import cc.mrbird.demo.selector.MyImportSelector;
|
||||||
|
import lombok.Builder;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
// @ComponentScan(value = "cc.mrbird.demo"
|
||||||
|
// , excludeFilters = {
|
||||||
|
// @Filter(type = FilterType.ANNOTATION,
|
||||||
|
// classes = {Controller.class, Repository.class}),
|
||||||
|
// @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = User.class)
|
||||||
|
// @Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
|
||||||
|
// }
|
||||||
|
// includeFilters = {
|
||||||
|
// @Filter(type = FilterType.ANNOTATION, classes = Service.class)
|
||||||
|
// }, useDefaultFilters = false
|
||||||
|
// )
|
||||||
|
@Import({Hello.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
|
||||||
|
public class WebConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
// @Conditional(MyCondition.class)
|
||||||
|
// @Lazy
|
||||||
|
// @Scope("prototype")
|
||||||
|
public User user() {
|
||||||
|
System.out.println("往IOC容器中注册user bean");
|
||||||
|
return new User("mrbird", 18);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CherryFactoryBean cherryFactoryBean() {
|
||||||
|
return new CherryFactoryBean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cc.mrbird.demo.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
public class UserController {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cc.mrbird.demo.dao;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Repository
|
||||||
|
public class UserMapper {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Apple {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Banana {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Cherry {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Hello {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Strawberry {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@ToString
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
// @Component
|
||||||
|
public class User {
|
||||||
|
private String name;
|
||||||
|
private Integer age;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
// @Component
|
||||||
|
public class UserDetail extends User {
|
||||||
|
|
||||||
|
private int sex;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.mrbird.demo.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class Watermelon {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cc.mrbird.demo.factory;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.domain.Cherry;
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class CherryFactoryBean implements FactoryBean<Cherry> {
|
||||||
|
@Override
|
||||||
|
public Cherry getObject() {
|
||||||
|
return new Cherry();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return Cherry.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package cc.mrbird.demo.filter;
|
||||||
|
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
import org.springframework.core.type.ClassMetadata;
|
||||||
|
import org.springframework.core.type.classreading.MetadataReader;
|
||||||
|
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||||
|
import org.springframework.core.type.filter.TypeFilter;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class MyTypeFilter implements TypeFilter {
|
||||||
|
@Override
|
||||||
|
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
|
||||||
|
// 获取当前正在扫描的类的注解信息
|
||||||
|
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
|
||||||
|
// 获取当前正在扫描的类的类信息
|
||||||
|
ClassMetadata classMetadata = metadataReader.getClassMetadata();
|
||||||
|
// 获取当前正在扫描的类的路径等信息
|
||||||
|
Resource resource = metadataReader.getResource();
|
||||||
|
|
||||||
|
String className = classMetadata.getClassName();
|
||||||
|
return StringUtils.hasText("er");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package cc.mrbird.demo.register;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.domain.Strawberry;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
|
@Override
|
||||||
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
||||||
|
final String beanName = "strawberry";
|
||||||
|
boolean contain = registry.containsBeanDefinition(beanName);
|
||||||
|
if (!contain) {
|
||||||
|
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Strawberry.class);
|
||||||
|
registry.registerBeanDefinition(beanName, rootBeanDefinition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package cc.mrbird.demo.selector;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ImportSelector;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public class MyImportSelector implements ImportSelector {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
return new String[]{
|
||||||
|
"cc.mrbird.demo.domain.Apple",
|
||||||
|
"cc.mrbird.demo.domain.Banana",
|
||||||
|
"cc.mrbird.demo.domain.Watermelon"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package cc.mrbird.demo.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
public interface CalculateService {
|
||||||
|
|
||||||
|
Integer sum(Integer... value);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cc.mrbird.demo.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cc.mrbird.demo.service.impl;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.service.CalculateService;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Profile("java7")
|
||||||
|
public class Java7CalculateServiceImpl implements CalculateService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer sum(Integer... value) {
|
||||||
|
System.out.println("Java 7环境下执行");
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i <= value.length; i++) {
|
||||||
|
result += i;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package cc.mrbird.demo.service.impl;
|
||||||
|
|
||||||
|
import cc.mrbird.demo.service.CalculateService;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author MrBird
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Profile("java8")
|
||||||
|
public class Java8CalculateServiceImpl implements CalculateService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer sum(Integer... value) {
|
||||||
|
System.out.println("Java 8环境下执行");
|
||||||
|
return Arrays.stream(value).reduce(0, Integer::sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue