springboot 启动时自动加载一些java类信息

一 概述

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

访问:

结果:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot启动时加载数据到Redis中有多种方法,以下是两种常用的方法: 方法一:使用CommandLineRunner接口 在启动中实现CommandLineRunner接口,并重写run方法,在该方法中调用需要加载数据到Redis的逻辑代码。例如: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { @Autowired private RedisTemplate<String, Object> redisTemplate; public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { // 加载数据到Redis loadDataToRedis(); } private void loadDataToRedis() { // 从数据库或其他数据源获取数据 List<String> dataList = getDataFromDatabase(); // 将数据存入Redis redisTemplate.opsForList().leftPushAll("myDataList", dataList); } private List<String> getDataFromDatabase() { // 从数据库获取数据的逻辑 // ... return dataList; } } ``` 方法二:使用@PostConstruct注解 在需要加载数据到Redis的的方法上添加@PostConstruct注解,该方法Spring Bean初始化完成后自动执行。例如: ```java @Service public class MyService { @Autowired private RedisTemplate<String, Object> redisTemplate; @PostConstruct public void loadDataToRedis() { // 从数据库或其他数据源获取数据 List<String> dataList = getDataFromDatabase(); // 将数据存入Redis redisTemplate.opsForList().leftPushAll("myDataList", dataList); } private List<String> getDataFromDatabase() { // 从数据库获取数据的逻辑 // ... return dataList; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值