You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在纯Java项目中使用Toothpick依赖注入框架?

我刚搞定了纯Java Gradle项目集成Toothpick DI的问题,看你踩了不少坑,给你整理一套能直接跑的配置和解决方案:

一、正确的Gradle依赖配置(纯Java环境)

首先要完全抛弃所有Android相关依赖,Toothpick的核心和注解处理器原生支持纯JRE/JDK环境。在你的build.gradle(Groovy)或build.gradle.kts(Kotlin)里添加以下配置:

Groovy 版本

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    // Toothpick核心运行时
    implementation 'com.github.stephanenicolas.toothpick:toothpick-runtime:3.1.0'
    // 注解处理器(必须添加,用于生成依赖注入的辅助类)
    annotationProcessor 'com.github.stephanenicolas.toothpick:toothpick-compiler:3.1.0'
    // 可选:JUnit测试依赖
    testImplementation 'com.github.stephanenicolas.toothpick:toothpick-testing:3.1.0'
    testImplementation 'junit:junit:4.13.2'
}

Kotlin DSL 版本

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.github.stephanenicolas.toothpick:toothpick-runtime:3.1.0")
    annotationProcessor("com.github.stephanenicolas.toothpick:toothpick-compiler:3.1.0")
    testImplementation("com.github.stephanenicolas.toothpick:toothpick-testing:3.1.0")
    testImplementation("junit:junit:4.13.2")
}

注意:一定要保证runtime和compiler的版本完全一致,版本不兼容是你之前编译失败的核心原因之一。

二、解决NetBeans无法识别生成代码的问题

Toothpick的注解处理器会把生成的MemberInjectorRegistry、FactoryRegistry等类放在build/generated/sources/annotationProcessor/java/main目录下。NetBeans有时候不会自动识别这个目录为源码根,你可以手动操作:

  • 右键项目 -> 选择「Properties」
  • 切换到「Sources」标签页
  • 点击「Add Folder」,找到build/generated/sources/annotationProcessor/java/main目录,添加为源码根
  • 点击「OK」后刷新项目,IDE就能识别生成的代码了
三、极简可运行示例代码

下面是一个最基础的纯Java示例,确保你能看到注解处理器生成代码:

1. 待注入的服务类

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class UserService {
    public String getUserName() {
        return "Toothpick Pure Java Demo";
    }
}

2. 依赖注入的目标类

import javax.inject.Inject;

public class UserController {
    @Inject
    private UserService userService;

    public void printUserName() {
        System.out.println("User Name: " + userService.getUserName());
    }
}

3. 主类(初始化Toothpick并执行注入)

import toothpick.Toothpick;
import toothpick.configuration.Configuration;

public class Main {
    public static void main(String[] args) {
        // 初始化Toothpick,启用注解处理器生成的代码(必须开启)
        Toothpick.setConfiguration(Configuration.forProduction().disableReflection());
        
        // 创建根作用域
        var scope = Toothpick.openScope("ROOT");
        
        // 注入UserController实例
        var controller = Toothpick.inject(new UserController(), scope);
        controller.printUserName();
    }
}

运行Main类,你应该能看到控制台输出User Name: Toothpick Pure Java Demo,同时build/generated目录下会生成对应的Registry类。

四、你之前踩坑的原因分析
  • Android模拟包的问题:Toothpick在纯Java环境下不需要任何Android依赖,添加模拟包会让注解处理器误以为是Android环境,生成Android专属的Registry类,导致纯Java环境无法解析。
  • Smoothie示例的问题:Smoothie是Toothpick的Android扩展库,本身依赖Android SDK,替换依赖后没有正确配置纯Java的注解处理器,导致没有生成代码。
  • 标准注解配置没生成文件:要么是没有在代码中使用Toothpick的注解(比如@Inject、@Singleton),注解处理器只有在检测到这些注解时才会生成代码;要么是依赖版本不匹配,或者没有正确添加annotationProcessor依赖。

内容的提问来源于stack exchange,提问作者will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:23:39