栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 获取jar包中定义的bean

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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;


@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;


@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";
    }
}

访问:

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/693969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号