springboot 获取jar包中定义的bean

Posted 健康平安的活着

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 获取jar包中定义的bean相关的知识,希望对你有一定的参考价值。

一 概述

springboot中提供了很多Enable开头的注解,用于动态启动某些类,底层原理就是使用@Import注解导入一些配置类,实现bean的动态加载。

要学会,服务启动时,动态加载javabean:

 

二 实操案例

2.1 通过applicationContext获取

通过服务启动时候获取

2.1.1 创建一个工程,作为应用jar包:

model层

package com.ljf.spring.boot.demo.hello.world.model;

/**
 * @ClassName: DeviceRunStateVo
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/04/02 11:37:31 
 * @Version: V1.0
 **/
public class DeviceRunStateVo 
    private  String deviceName;//设备名称  电机,皮带
    private  int deviceTotal;//设备总数
    private  int runTotal;//运行设备数
    private  int stopTotal;//停止设备数
    private  int brokenTotal;//故障设备数
    private  String currentTotalTime;//统计时间
    public String getDeviceName() 
        return deviceName;
    

    public void setDeviceName(String deviceName) 
        this.deviceName = deviceName;
    

    public int getDeviceTotal() 
        return deviceTotal;
    

    public void setDeviceTotal(int deviceTotal) 
        this.deviceTotal = deviceTotal;
    

    public int getRunTotal() 
        return runTotal;
    

    public void setRunTotal(int runTotal) 
        this.runTotal = runTotal;
    

    public int getStopTotal() 
        return stopTotal;
    

    public void setStopTotal(int stopTotal) 
        this.stopTotal = stopTotal;
    

    public int getBrokenTotal() 
        return brokenTotal;
    

    public void setBrokenTotal(int brokenTotal) 
        this.brokenTotal = brokenTotal;
    

    public String getCurrentTotalTime() 
        return currentTotalTime;
    

    public void setCurrentTotalTime(String currentTotalTime) 
        this.currentTotalTime = currentTotalTime;
    

    public DeviceRunStateVo() 
    

    public DeviceRunStateVo(String deviceName, int deviceTotal, int runTotal, int stopTotal, int brokenTotal, String currentTotalTime) 
        this.deviceName = deviceName;
        this.deviceTotal = deviceTotal;
        this.runTotal = runTotal;
        this.stopTotal = stopTotal;
        this.brokenTotal = brokenTotal;
        this.currentTotalTime = currentTotalTime;
    

    @Override
    public String toString() 
        return "DeviceRunStateVo" +
                "deviceName='" + deviceName + '\\'' +
                ", deviceTotal=" + deviceTotal +
                ", runTotal=" + runTotal +
                ", stopTotal=" + stopTotal +
                ", brokenTotal=" + brokenTotal +
                ", currentTotalTime='" + currentTotalTime + '\\'' +
                '';
    

2.配置类:

@Component
public class ProdcuctConfig 
    @Bean
    public DeviceRunStateVo getDeviceRunStateVo()
        return new DeviceRunStateVo();
    


2.1.2 再创建一个主工程

1.在pom文件,引用上面的jar包工程

2.启动类:使用@ComponentScan("com.ljf.spring.boot.demo.hello.world.config") 注解

package com.ljf.spring.boot.demo;


import com.ljf.spring.boot.demo.hello.world.model.DeviceRunStateVo;
import com.ljf.spring.boot.demo.zhujie.EnableMyZhujie;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Locale;
import java.util.Map;

/**
 * Hello world!
 *
 */
@SpringBootApplication
//@EnableMyZhujie
@ComponentScan("com.ljf.spring.boot.demo.hello.world.config")
public class App 

    public static void main( String[] args )
    
        ConfigurableApplicationContext  context=SpringApplication.run(App.class,args);
        Object de=  context.getBean("getDeviceRunStateVo");//填写的是方法名,
        DeviceRunStateVo dev=(DeviceRunStateVo)de;
        System.out.println( "获取的javabean: " +dev);
    

 注意:上面context.getBean("getDeviceRunStateVo")填写的是调用配置类中的方法名,如下图所示

 执行后的结果:

 3.启动类:自定义注解

package com.ljf.spring.boot.demo.zhujie;

import com.ljf.spring.boot.demo.hello.world.model.DeviceRunStateVo;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(DeviceRunStateVo.class)
public @interface EnableMyZhujie 

启动类:

package com.ljf.spring.boot.demo;


import com.ljf.spring.boot.demo.hello.world.model.DeviceRunStateVo;
import com.ljf.spring.boot.demo.zhujie.EnableMyZhujie;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Locale;
import java.util.Map;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableMyZhujie
//@ComponentScan("com.ljf.spring.boot.demo.hello.world.config")
public class App 

    public static void main( String[] args )
    
        ConfigurableApplicationContext  context=SpringApplication.run(App.class,args);
        Object de=  context.getBean("getDeviceRunStateVo");//填写的是方法名,
        DeviceRunStateVo dev=(DeviceRunStateVo)de;
        System.out.println( "获取的javabean: " +dev);
    

结果如图: 

 2.2 通过服务启动后从controller层获取

package com.ljf.spring.boot.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ljf.spring.boot.demo.hello.world.config.*;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController 
    @Autowired
    private ProdcuctConfig pv ;
    @RequestMapping("/testpv")
    @ResponseBody
    public String test()
        System.out.println("contorller:获取到bean:"+pv.getDeviceRunStateVo());
        return "ok";
    

访问:

结果:

以上是关于springboot 获取jar包中定义的bean的主要内容,如果未能解决你的问题,请参考以下文章

如何在springboot项目中删除引用jar包中的无用bean

如何在springboot项目中删除引用jar包中的无用bean

springboot 启动排除某些bean 的注入

springboot项目中调用jar包中的类时报错 — 没有注入类

springboot项目中调用jar包中的类时报错 — 没有注入类

jar方式运行项目-读取jar包中的文件