SpringBoot自动装配之@Enable深入讲解
短信预约 -IT技能 免费直播动态提醒
SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的。而其底层原理是使用@Import注 解导入一些配置类,实现Bean的动态加载。
提问:SpringBoot 工程是否可以直接获取jar包中定义的Bean?
答:不可以
案例:
两个子模块
①子模块要得到
②子模块的User类的bean(这里用编号表示)
方法一:使用@ComponentScan扫描com.itheima.springbooyembal包
package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
引入依赖:
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
使用ComponentScan:
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@ComponentScan("com.enable.config")
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}
测试如下:
方法二:可以使用@Import注解,加载类,这些类都会被Spring创建,并放入IOC容器。
package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
引入依赖
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
使用Import注解
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@Import(UserConfig.class)
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}
测试如下:
方法三:对@Import注解进行封装
自定义@EnableUser注解
package com.enable.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}
自定义配置类
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
新建实体类:
package com.enable.entity;
public class User {
}
引入依赖
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
使用自定义的注解
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@EnableUser
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}
测试如下:
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341