Appearance
本指南面向 Java 开发者,介绍如何在 Kiro 中充分利用 AI 辅助能力。内容覆盖 JDK 环境准备、Open VSX 扩展推荐(含 Spring Boot 套件)、通过对话让 Kiro 搭建 Maven/Gradle 项目结构的提示词示例,以及用 steering 文件向 Kiro 传递架构规范(六边形架构、CQRS、DDD)和 Spring Boot 开发约定。此外还介绍了 hooks 自动化(测试生成、Checkstyle)和 Maven MCP server 的配置方法。
Kiro 为 Java 项目提供强大的 AI 辅助能力,帮助你更高效地编写、调试和维护代码。
环境准备
开始 Java 开发前,请确认以下依赖已就绪:
- JDK:推荐安装 Amazon Corretto(JDK 17 或更高 LTS 版本),这是 OpenJDK 的免费生产级发行版。
- 构建工具:Maven 或 Gradle,用于依赖管理和构建自动化。
- Git:版本控制。
推荐扩展
Kiro 兼容 Open VSX 扩展市场,以下扩展可显著提升 Java 开发体验:
| 扩展 | 说明 |
|---|---|
| Extension Pack for Java | 包含语言支持、调试器、测试运行器、Maven、Project Manager 等核心工具 |
| Spring Boot Extension Pack | Spring Boot Tools + Spring Initializr + Spring Boot Dashboard |
| Gradle for Java | Gradle 项目管理与任务运行 |
| Maven for Java | Maven 项目管理与任务运行 |
| Markdown Preview Enhanced | Markdown 实时预览,便于查看文档 |
让 Kiro 帮你搭建开发环境
项目配置与结构
Kiro 可以帮你初始化配置文件、搭建项目结构,以及配置开发环境:
"Create a new Maven project for a Spring Boot application"
"Set up a Gradle build file with JUnit 5 and Mockito dependencies"
"Configure a multi-module Maven project structure"
"Help me install and configure the latest JDK for my operating system"
"Set up a Spring Boot project with proper layered architecture"
"Create a pom.xml with Spring Security and JPA dependencies"代码分析与重构
Kiro 可以审查代码质量、辅助重构、引入设计模式:
"Analyze this method for potential bugs or performance issues"
"Refactor this code to use the Builder pattern"
"Convert this imperative code to use Java Streams"调试辅助
遇到报错时,可以直接把错误信息或堆栈跟踪发给 Kiro:
"Explain this NullPointerException in my code"
"Help me debug this ConcurrentModificationException"
"Analyze this stack trace and suggest a fix"Steering 文件配置
Steering 允许你把项目上下文和规范传递给 Kiro,使 AI 生成的代码更贴合团队实际。Kiro 可以自动生成三个基础 steering 文件:
product.md— 产品目标与核心功能tech.md— 技术栈与开发规范structure.md— 项目目录结构说明
创建自定义 steering 文件
- 在侧边栏打开 Kiro 视图
- 在 "Agent Steering" 区域点击
+按钮 - 输入文件名(描述性标题)
- 按 Markdown 格式填写内容
自定义 steering 文件存储在 .kiro/steering/ 目录,Kiro 会在每次交互时自动读取。
项目规范 steering 示例
创建 java-conventions.md,定义团队架构规范:
markdown
# Java Project Conventions
## Architecture Patterns
- Use hexagonal architecture for complex domains
- Implement CQRS for read/write separation when needed
- Apply Domain-Driven Design principles for business logic
## Testing Strategy
- Write unit tests for all business logic
- Use TestContainers for integration tests
- Maintain 80% code coverage minimum
- Follow the AAA pattern (Arrange, Act, Assert)
## Error Handling
- Use custom exceptions for business logic errors
- Implement global exception handlers with @ControllerAdvice
- Log errors with correlation IDs for traceability
- Return consistent error response formats
## Performance Guidelines
- Use connection pooling for database access
- Implement caching strategies for frequently accessed data
- Use async processing for long-running operations
- Monitor and optimize database queriesSpring Boot 专项 steering 示例
创建 spring-boot-patterns.md:
markdown
# Spring Boot Development Guidelines
## Component Structure
- Use @RestController for REST endpoints
- Use @Service for business logic
- Use @Repository for data access
- Use @Component for other beans
## Dependency Injection
- Prefer constructor injection over field injection
- Use final fields for injected dependencies
- Avoid circular dependencies
## API Design
- Follow REST principles for endpoint design
- Use appropriate HTTP methods (GET, POST, PUT, DELETE)
- Return appropriate HTTP status codes
- Use DTOs for request/response objectsAgent Hooks 自动化
Kiro 的 agent hooks 可以自动化常见 Java 开发任务,例如:
- 保存 Java 文件时自动生成 JUnit 测试
- 运行 Checkstyle 或 SpotBugs 代码质量检查
- 检查 Maven/Gradle 依赖是否过期
- 为公开方法自动生成或更新 JavaDoc 注释
- 校验 Spring Boot 配置文件
- 使用 Google Java Format 格式化代码
MCP 服务器集成
Kiro 支持 Model Context Protocol (MCP) 服务器,为 Java 开发提供额外的专项工具能力。完整配置说明参见 MCP 文档。
Maven MCP Server
在项目 MCP 配置中添加以下内容,即可在 Kiro 中直接运行 Maven 命令:
json
{
"mcpServers": {
"maven": {
"command": "uvx",
"args": ["maven-mcp-server@latest"]
}
}
}配置后可以:
- 通过 Kiro 运行 Maven 命令
- 获取 AI 驱动的构建问题解释
- 管理依赖和项目配置
示例用法:
"Run Maven tests for my project"
"Add Spring Boot starter dependencies to my pom.xml"更多 MCP 服务器可参考 AWS MCP Servers 和 Awesome MCP Servers。
快捷调试方式
| 操作 | 快捷键 | 说明 |
|---|---|---|
| Inline Chat | Cmd/Ctrl + I | 针对当前代码提问或请求修复 |
| Add to Chat | Cmd/Ctrl + L | 将整个文件加入对话,进行全文分析 |
| Quick Fix | 悬停错误 → Quick fix → Ask Kiro | 自动把报错代码加入对话并开始调试 |
参考资源
常见问题
Q:steering 文件是否会影响 Kiro 的响应速度?
不会。steering 文件只是在每次对话时提供背景上下文,文件内容越精简越好。建议只写架构决策和团队约定,避免把代码格式化规则(用 Checkstyle/Spotless 处理更合适)写进 steering 文件。
Q:Maven MCP Server 需要 Java 环境吗?
Maven MCP Server 通过 uvx 运行,需要先安装 uv(Python 包管理器)。本地 Maven 环境仍需单独配置,MCP server 主要起到与 Kiro 对话的桥接作用。
Q:如何让 Kiro 在生成代码时自动遵循我们的包命名规范?
在 java-conventions.md 中明确写出包命名规则,例如 com.company.{module}.{layer}。steering 文件里的具体示例比抽象描述效果更好。