Springboot中如何在Utils类中使用@Autowired注入bean

Easter79
• 阅读 1141

Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:

1. 使用@Component注解标记工具类StatisticsUtils:

2. 使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:

3. 在工具类中编写init()函数,并使用@PostConstruct注解标记工具类,初始化Bean:

public class StatisticsUtils {
 
    @Autowired
    private IdeaMemberDao ideaMemberDao;
    @Autowired
    private ProjectMemberDao projectMemberDao;
    @Autowired
    private IdeaMgrDao ideaMgrDao;
    @Autowired
    private ProjectMgrDao projectMgrDao;
 
    public static StatisticsUtils statisticsUtils;
 
    @PostConstruct
    public void init() {
        statisticsUtils = this;
        statisticsUtils.ideaMemberDao = this.ideaMemberDao;
        statisticsUtils.projectMemberDao = this.projectMemberDao;
        statisticsUtils.ideaMgrDao = this.ideaMgrDao;
        statisticsUtils.projectMgrDao = this.projectMgrDao;
 
    }
点赞
收藏
评论区
推荐文章
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Spring Boot的@Service和@Autowired和@ComponentScan注解
SpringBoot的@Service和@Autowired和@ComponentScan注解SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描,即只扫描该类所在的包及其子包。1.如果需要扫描其上层包中的类,则将其移动到其上层包中2.使用@ComponentSc
Stella981 Stella981
2年前
Spring 学习笔记(三):Spring Bean
1Bean配置Spring可以看做是一个管理Bean的工厂,开发者需要将Bean配置在XML或者Properties配置文件中。实际开发中常使用XML的格式,其中<bean中的属性或子元素如下:id:Bean在BeanFactory中的唯一标识,在代码中通过BeanFac
Wesley13 Wesley13
2年前
spring3.x注解自动注入
一、各种注解方式1.@Autowired注解(不推荐使用,建议使用@Resource)@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下Xml代码
Stella981 Stella981
2年前
Spring 4.3 的新功能和增强
核心容器改进核心容器额外提供了更丰富的元数据来改进编程。默认Java8的方法检测为bean属性的getter/setter方法。如果目标bean只定义了一个构造函数,则它无需要指定@Autowired注解@Configuration类支持构造函数注入。任何SpEL
Easter79 Easter79
2年前
SpringMvc中@resource和@autowired区别
在java代码中使用@Autowired或@Resource注解方式进行装配这两个注解的区别是:@Autowired默认按类型装配,@Resource(https://my.oschina.net/u/929718)默认按名称装配,当找不到与名称匹配的bean才会按类型装配。如果我们想使用按名称装
Wesley13 Wesley13
2年前
1.1Spring Boot 环境配置和常用注解
SpringBoot常用注解:@Service:注解在类上,表示这是一个业务层bean@Controller:注解在类上,表示这是一个控制层bean@Repository:注解在类上,表示这是一个数据访问层bean@Component:注解在类上,表示通用bean,value不写默认就是类名首字母小写@Auto
Easter79 Easter79
2年前
Spring高级应用之注入嵌套Bean
在Spring中,如果某个Bean所依赖的Bean不想被Spring容器直接访问,可以使用嵌套Bean。和普通的Bean一样,使用<bean元素来定义嵌套的Bean,嵌套Bean只对它的外部的Bean有效,Spring容器无法直接访问嵌套的Bean,因此定义嵌套Bean也无需指定id属性。如下配置片段是一个嵌套Bean的示例:<bean id
Stella981 Stella981
2年前
Spring 12 种 常用注解!
1.声明bean的注解@Component组件,没有明确的角色@Service在业务逻辑层使用(service层)@Repository在数据访问层使用(dao层)@Controller在展现层使用,控制器的声明(C)2.注入bean的注解@Autowired:由Spring提供@Inj
Wesley13 Wesley13
2年前
Spring方法注入
在spring中注入方式有3中:1,构造函数注入2,set方法注入3,接口注入(方法注入)在spring中的bean默认范围都是单例,但是在特定的情况下,我们需要有如下的业务需要,单例bean1需要依赖非单例bean2,由于bean1始终是单例,所以如果不做出改变,每次获取的bean2也是同一个,容器就没办法给我们提供
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k