赞
踩
这边先抛出一个问题
下面是根据字节码文件加载,这个代码的作用是如果加载了某个字节码文件,那么就会继续加载其他类。不加载的话也就不会加载和这个相关的方法,非常实用。
上面这个功能在springboot 里面大量使用,下面举几个例子。可以更好的理解Springboot 原理。
下面就是Springboot 自动装配使用到的注解,当然这边提到的还很少,有兴趣的可以自己查查这些注解的功能效用!
下面是Springboot自动装配的redis类
运用到上面的知识,切换服务器就变的非常的简单易懂!
这个底层实际上是@import, 下面就是这个注解的源码
Springboot加载配置类的时候只会加载启动类本包(同级)及其子包的配置类,那么Spring加载的时候也会加载@import 引入的Class类,并且也可以用@CommentScan 包扫描来加载指定路径下的类
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @AutoConfigurationPackage
- @Import(AutoConfigurationImportSelector.class)
- public @interface EnableAutoConfiguration {
-
- /**
- * Environment property that can be used to override when auto-configuration is
- * enabled.
- */
- String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
-
- /**
- * Exclude specific auto-configuration classes such that they will never be applied.
- * @return the classes to exclude
- */
- Class<?>[] exclude() default {};
-
- /**
- * Exclude specific auto-configuration class names such that they will never be
- * applied.
- * @return the class names to exclude
- * @since 1.3.0
- */
- String[] excludeName() default {};
-
- }
上面图片就是所有自动配置的类,但是不一定都会加载,因为加了判断,情况满足参会加载到容器里面。
- @SpringBootApplication
- @Import(User.class)
- public class SpringInitApplication {
-
- public static void main(String[] args) {
- ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
- User bean = applicationContext.getBean(User.class);
- System.out.println(bean);
-
- }
-
- }
首先是配置类的代码
- package com.zx.domain;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class UserConfig {
- @Bean
- public User user(){
- return new User();
- }
- }
下面是启动类的导入
- @SpringBootApplication
- //@Import(User.class)
- @Import(UserConfig.class)
- public class SpringInitApplication {
-
- public static void main(String[] args) {
- ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
- User bean = applicationContext.getBean(User.class);
- System.out.println(bean);
-
- }
-
- }
首先实现ImportSelector类
- package com.zx.domain;
- import org.springframework.context.annotation.ImportSelector;
- import org.springframework.core.type.AnnotationMetadata;
- import java.util.function.Predicate;
- public class MyImportSelector implements ImportSelector {
- @Override
- public String[] selectImports(AnnotationMetadata importingClassMetadata) {
- return new String[]{
- //这边是你要导入的对象
- "com.zx.domain.User"
- };
- }
- @Override
- public Predicate<String> getExclusionFilter() {
- return ImportSelector.super.getExclusionFilter();
- }
- }
导入的方式还是上面老样子
@Import(MyImportSelector.class)
使用方法
- public class MyImportRegister implements ImportBeanDefinitionRegistrar {
- @Override
- public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
- ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
- registry.registerBeanDefinition("cs",BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition());
- }
- }
启动类
-
- @SpringBootApplication
- //@Import(User.class)
- //@Import(UserConfig.class)
- @Import(MyImportRegister.class)
- public class SpringInitApplication {
-
- public static void main(String[] args) {
- ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitApplication.class, args);
- User bean = (User) applicationContext.getBean("cs");
- System.out.println(bean);
- }
-
- }
- @Configuration
- @EnableConfigurationProperties(UserProperties.class)
- public class AutoZx {
- @Bean
- public UserInfo getUserInfo(UserProperties userProperties){
- System.out.println("加载默认用户!");
- return new UserInfo(userProperties.getName());
- }
-
- }
- //这边遇到一个问题,无法映射到项目内部的配置文件,只能映射项目外部的配置文件,暂不清楚什么原因
- @ConfigurationProperties(prefix = "zx")
- public class UserProperties {
- private String name;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
- public class UserInfo {
- private String name ;
-
- public UserInfo(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。