启动类main方法调用service、工具类调用dao、@Value到static属性

1.启动类main方法调用service

1.方式一

1.1 注入时候

如果不在启动类使用下述代码,请在代码所在类添加@Component注解,否则使用时会空指针。
在这里插入图片描述
注意:在@PostConstruct注解标注的方法中不能用getBean()方法获取bean达到给静态属性赋值的目的,因为getBean()在初始化之后才可获取到bean,@PostConstruct注解标注的方法属于初始化的一步。

1.2 调用时候

在这里插入图片描述

2.方式二

(service类要加@Service注解)
普通启动类:

package com.kaytune;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan("com.kaytune.dao")
@SpringBootApplication
@EnableTransactionManagement
public class UmrEmployeesearchapiRedis {
  public static void main(String[] args) {
    SpringApplication.run(UmrEmployeesearchapiRedis.class, args);
  }
}

在启动类获取Bean:

package com.kaytune.syncmembers2redis;

import com.kaytune.syncmembers2redis.schedulerservice.SyncBindingsSchedulerService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@MapperScan("com.kaytune.syncmembers2redis.mapper")
public class Syncmembers2redisApplication {
  public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(Syncmembers2redisApplication.class, args);
    SyncBindingsSchedulerService sb = context.getBean(SyncBindingsSchedulerService.class);
    sb.sync();
  }
}

ApplicationContext context =SpringApplication.run(Syncmembers2redisApplication.class, args);
SyncBindingsSchedulerService sb
= context.getBean(SyncBindingsSchedulerService.class);
sb.sync();

2. 工具类调用dao

【在util工具类中有时候也会遇到注入不进去service或dao层导致空指针的情况,主要是因为注入的service、dao在static方法中调用导致的。也可以用大标题1中方法1解决-注意工具类要加@Component注解】
在这里插入图片描述

3.@Value注入配置文件属性到static修饰的属性

在这里插入图片描述

4.@Autowired注入Bean到static修饰的属性

当类加载器加载静态变量时,Spring上下文尚未加载。所以类加载器不会在bean中正确注入静态类,并且会失败。
解决方法有三种:
1.将类由@Component修饰,不采用static方法,属性就不用被static修饰了

2.将@Autowired加到构造方法上[所在类需要加@Component注解]

   private static RedisTools redisTools;
   private static DesUtil desUtil;
   @Autowired
   public RsaUtil(RedisTools redisTools, DesUtil desUtil) {
   RsaUtil.redisTools = redisTools;
   RsaUtil.desUtil = desUtil;
   }

3.方式2类似“@Value注入配置文件属性到static修饰的属性”[所在类需要加@Component注解]

  private static RedisTools redisTools;
  private static DesUtil desUtil;
  @Autowired
  private RedisTools redisToolsTemp;
  @Autowired
  private DesUtil desUtilTemp;
  @PostConstruct
  public void beforeInit() {
    redisTools = redisToolsTemp;
    desUtil = desUtilTemp;
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值