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

SpringBoot--将组件注入到静态工具类--方法/实例

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

SpringBoot--将组件注入到静态工具类--方法/实例

原文网址:SpringBoot--将组件注入到静态工具类--方法/实例_IT利刃出鞘的博客-CSDN博客

简介

说明

本文用示例介绍Spring如何将组件注入到静态工具类。

需求

在Controller层想使用一个静态工具,这个静态工具要使用其他组件。

错误方法:在静态字段上注入

Controller

package com.example.controller;

import com.example.component.WelcomeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/test")
    public String test() {
        WelcomeUtil.welcome();
        return "test success";
    }
}

工具类

package com.example.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WelcomeUtil {
    @Autowired
    private static Hello hello;

    public static void welcome() {
        hello.sayHello();
    }
}

引入的组件

package com.example.component;

import org.springframework.stereotype.Component;

@Component
public class Hello {
    public void sayHello() {
        System.out.println("Hello");
    }
}

测试

访问:http://localhost:8080/test

后端结果

方案1:构造器注入

其余代码与上边相同

工具类

package com.example.component;

import org.springframework.stereotype.Component;

@Component
public class WelcomeUtil {
    private static Hello hello;

    public WelcomeUtil(Hello hello) {
        WelcomeUtil.hello = hello;
    }

    public static void welcome() {
        hello.sayHello();
    }
}

测试

访问:http://localhost:8080/test

后端结果

方案2:setter注入

其余代码与上边相同

工具类

package com.example.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WelcomeUtil {
    private static Hello hello;

    public static void welcome() {
        hello.sayHello();
    }

    @Autowired
    public void setHello(Hello hello) {
        WelcomeUtil.hello = hello;
    }
}

测试

访问:http://localhost:8080/test

结果

后端结果

方案3:ApplicationContext工具类

ApplicationContext工具类

package com.example.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext context;
 
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        ApplicationContextHolder.context = context;
    }
 
    public static ApplicationContext getContext() {
        return context;
    }
}

工具类

package com.example.component;

import com.example.util.ApplicationContextHolder;
import org.springframework.stereotype.Component;

@Component
public class WelcomeUtil {
    public static void welcome() {
        ApplicationContextHolder.getContext()
                .getBean(Hello.class).sayHello();

    }
}

测试

访问:http://localhost:8080/test

结果

后端结果

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

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

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