JAVA在Spring框架多线程时调用服务类方法

一 Spring框架多线程调用服务类的方法 

     我们在开发Web的项目时,有时候我们想在线程的环境下使用@Service@Component 注解的服务类方法,多线程下是不能直接调用,以下总结一些常用的技巧转换成可调用的形式。

二  Spring框架下多线程下调用动态服务类实现

1 使服务类转静态化

   使服务类静态化,让服务类变成静态可调用的,这样从而可以使多线程调用相应的方法

  方式一:使用@PostConstruct注解(推荐使用)

@PostConstruct注解是javax.annotation包下的一个注解,它用于标记一个方法,在框架扫描所有的类实例化装入容器,且依赖注入完成后,执行该方法。代码如下:



import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/***
 * 本地缓存
 * @author hua
 * @date 2024-05-23 09:10
 */
@Service
public class LocalCacheServiceImpl implements CacheService {


    public static LocalCacheServiceImpl localCacheServiceImpl;


    @PostConstruct // 初始化
    public void init() {
        localCacheServiceImpl = this;
    }

    private static Cache<String, String> cache = CacheBuilder.newBuilder()
            .initialCapacity(3000)
            .concurrencyLevel(5)
            .expireAfterWrite(30, TimeUnit.MINUTES)
            .build();


    @Override
    public void put(String key, String value) {
        cache.put(key,value);
    }

    @Override
    public String get(String key) {
        return cache.getIfPresent(key);
    }

    
}

方式二: 使用@Autowired注解

@Autowired注解同样可以把实例化对象注入到当前的静态字段值,与之关联。代码如下:



import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/***
 * 本地缓存
 * @author hua
 * @date 2024-05-23 09:10
 */
@Service
public class LocalCacheServiceImpl implements CacheService {


    public static LocalCacheServiceImpl localCacheServiceImpl;

    @Autowired
    public void setLocalCacheServiceImpl(LocalCacheServiceImpl localCacheServiceImpl) {
        LocalCacheServiceImpl.localCacheServiceImpl = localCacheServiceImpl;
    }


    private static Cache<String, String> cache = CacheBuilder.newBuilder()
            .initialCapacity(3000)
            .concurrencyLevel(5)
            .expireAfterWrite(30, TimeUnit.MINUTES)
            .build();


    @Override
    public void put(String key, String value) {
        cache.put(key,value);
    }

    @Override
    public String get(String key) {
        return cache.getIfPresent(key);
    }

    
}

2  使用ApplicationContexAware获取Bean

在线程类实现ApplicationContextAware接口,这样可以获取到Spring的ApplicationContext,进而从中获取到需要的服务Bean,实现代码如下:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MyTask implements Runnable, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run() {
        MyService myService = applicationContext.getBean(MyService.class);
        myService.serviceMethod();
    }
}

3 通过构造函数注入

直接在创建线程的地方,将Service Bean作为构造参数或者通过setter方法传给线程。

@Service
public class MyService {
    public void serviceMethod() {
      
    }
}


public class MyTask implements Runnable {

    private final MyService myService;

    //通过构造函数把服务对像传进行去
    public MyTask(MyService myService) {
        this.myService = myService;
    }

    @Override
    public void run() {
        myService.serviceMethod();
    }
}

// 在启动线程的地方
@Autowired
private MyService myService;

public void startMyTask() {
    Thread thread = new MyTask(myService);
    thread.start();
}

三 总结:

        上述方法中,第1种实现最简单方便,第2种较为常见,第3 种容易理解。选择哪种方法取决于你的具体需求和项目的现有架构。在大多数情况下,项目使用第1种方式一,有时候选则传递Bean实例(第3种方法)。

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qyhua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值