@Enable*
创建一个主启动类
-
package com.you.boot;
-
-
import com.you.config.EnableUser;
-
import com.you.config.UserConfig;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.context.ConfigurableApplicationContext;
-
import org.springframework.context.annotation.ComponentScan;
-
import org.springframework.context.annotation.Import;
-
-
@SpringBootApplication
-
public
class
BootEnable02Application {
-
-
public
static
void
main
(String[] args) {
-
ConfigurableApplicationContext
context
= SpringApplication.run(BootEnable02Application.class, args);
-
// 获取Bean
-
Object
user
= context.getBean(
"user");
-
System.out.println(user);
-
}
-
-
}
新建一个Module
【如果创建完,不是并列,如下图(并列),而是包含】
【错误原因】路径写错了,需要把红框类的去掉
在新Module里写一个用户类和配置类
User类
-
package com.you.domain;
-
-
public
class
User {
-
-
}
UserConfig
-
package com.you.config;
-
-
import com.you.domain.User;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
-
@Configuration
-
public
class
UserConfig {
-
@Bean
-
public User
user
()
-
{
-
return
new
User();
-
}
-
}
在主启动类的Pom里引入配置类的依赖
-
<dependency>
-
<groupId>com.you</groupId>
-
<artifactId>boot-enable-other</artifactId>
-
<version>
0.0
.1-SNAPSHOT</version>
-
</dependency>
启动。问题SpringBoot不可以直接获取Jar包中定义的Bean
解决办法
解放方案一:
@ConponentScan("配置类包名")
效果:
解决方案2:@Import
效果图
解决方案3:@Import的封装
在配置类包下,创建如下类
-
package com.you.config;
-
-
import org.springframework.context.annotation.Import;
-
-
import java.lang.annotation.*;
-
-
@Target({ElementType.TYPE})
-
@Retention(RetentionPolicy.RUNTIME)
-
@Documented
-
@Import(UserConfig.class)
-
public
@interface EnableUser {
-
-
}
效果:
@Import
@Import提供了四种用法
1、导入Bean
2、导入配置类
3、导入ImportSelector实现类,一般用于加载配置文件的类
4、导入ImportBeanDefinitionRegistrar实现类
1、导入Bean
-
@SpringBootApplication
-
@Import(User.class)
-
public
class
BootEnable02Application {
-
-
public
static
void
main
(String[] args) {
-
ConfigurableApplicationContext
context
= SpringApplication.run(BootEnable02Application.class, args);
-
User
user
= context.getBean(User.class);
-
System.out.println(user);
-
}
-
-
}
可以获取到
2、导入配置类,和上面@Enable*详解中的一样
3、 导入ImportSelector实现类,一般用于加载配置文件的类
首先需要写一个ImporeSelector的实现类,还是写在config包下
-
package com.you.config;
-
-
import org.springframework.context.annotation.ImportSelector;
-
import org.springframework.core.type.AnnotationMetadata;
-
-
-
-
public
class
MySelector
implements
ImportSelector {
-
@Override
-
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
-
return
new
String[]{
"com.you.domain.User"};
-
}
-
-
-
}
效果
4、导入ImportBeanDefinitionRegistrar实现类
首先需要写一个ImportBeanDefinitionRegistrar实现类,同样写在config包下
-
package com.you.config;
-
-
import com.you.domain.User;
-
import org.springframework.beans.factory.support.AbstractBeanDefinition;
-
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
-
import org.springframework.beans.factory.support.BeanNameGenerator;
-
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
-
import org.springframework.core.type.AnnotationMetadata;
-
-
public
class
MyImportBeanDefinitionRegistrar
implements
ImportBeanDefinitionRegistrar {
-
@Override
-
public
void
registerBeanDefinitions
(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
-
AbstractBeanDefinition
beanDefinition
= BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
-
registry.registerBeanDefinition(
"user",beanDefinition);
-
}
-
}
效果
转载:https://blog.csdn.net/m0_59792745/article/details/125823408
查看评论