BrowserSkill 部署与原理实战指南:让 AI Agent 复用你的登录态浏览器
2026/9/25 15:42:52
@Slf4j 是 Lombok 提供的注解,用于自动生成日志对象。
不使用 @Slf4j(传统方式):
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UserService { private static final Logger log = LoggerFactory.getLogger(UserService.class); public void test() { log.info("这是一条日志"); log.error("错误日志"); } }使用 @Slf4j(简化方式):
import lombok.extern.slf4j.Slf4j; @Slf4j public class UserService{ public void test(){ log.info("这是一条日志"); //直接用log,无需手动创建 log.error("错误日志"); } }@RequestMapping 是 Spring MVC 的注解,用于将请求 URL 映射到控制器方法。
基本用法:
@Controller @RequestMapping("/user") public class userController{ @RequestMapping("/login") public Stirng login{ return "login"; } @RequestMapping(value="/register",method=RequestMethod.POST) public String register{ return "register"; } }指定请求方法
@RequestMapping(value = "/info", method = RequestMethod.GET) // 只接受 GET 请求 public String getInfo() { return "info"; } // 或者使用更简洁的注解 @GetMapping("/info") // 等同于上面的写法 @PostMapping("/create") // 等同于 @RequestMapping(method = RequestMethod.POST) @PutMapping("/update") // 等同于 @RequestMapping(method = RequestMethod.PUT) @DeleteMapping("/delete") // 等同于 @RequestMapping(method = RequestMethod.DELETE)指定请求参数:
@RequestMapping(value="/search",params="keyword") //必须有keyword参数 public String search(){ return "result"; }两者都用于依赖注入,但来源、默认策略和使用场景不同。
| 特性 | @Autowired | @Resource |
|---|---|---|
| 来源 | Spring 框架 | JDK(JSR-250 标准) |
| 默认注入方式 | 按类型(byType) | 按名称(byName) |
| 找不到时的处理 | 抛出异常(可用 required=false 避免) | 抛出异常 |
| 支持 | 支持 @Qualifier 指定名称 | 支持 name 属性指定名称 |
| 使用场景 | Spring 项目推荐 | 更通用,不依赖 Spring |
默认按类型注入:
@Service public class userService{ // Spring 会查找类型为 UserMapper 的 Bean @Autowierd private UserMapper userMapper; }按名称注入(需要配合 @Qualifier):
@Service public class UserService{ //如果有多个UserMapper实现,需要指定名称 @Autowired @Qualifier("userMapperImpl") //指定Bean名称 privarte UserMapper userMapper; }允许为空(避免启动失败):
@Autowired(required=false) //如果找不到Bean,不会报错 private UserMapper userMapper;构造函数注入(推荐方式):
@Service public class UserService{ private final UserMapper userMapper; @Autowired //Spring4.3+可以忽略 public UserService(UserMapper userMapper){ this.userMapper=userMapper; } }默认按名称注入:
@Service public class UserService { // 先按名称 "userMapper" 查找,找不到再按类型查找 @Resource private UserMapper userMapper; }指定名称注入:
@Service public class UserService { @Resource(name = "userMapperImpl") // 明确指定 Bean 名称 private UserMapper userMapper; }按类型注入(需要指定 type):
@Resource(type = UserMapper.class) // 按类型注入 private UserMapper userMapper;推荐使用 @Autowired:
使用 @Resource:
最佳实践:
@Service public class UserService { // 推荐:使用构造函数注入,无需 @Autowired(Spring 4.3+) private final UserMapper userMapper; private final RedisTemplate<String, Object> redisTemplate; public UserService(UserMapper userMapper, RedisTemplate<String, Object> redisTemplate) { this.userMapper = userMapper; this.redisTemplate = redisTemplate; } }