亲宝软件园·资讯

展开

SpringBoot自动装配之@Enable深入讲解

不死鸟.亚历山大.狼崽子 人气:0

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);
    }
}

测试如下:

加载全部内容

相关教程
猜你喜欢
用户评论