引言

service、mapper类均可使用@Autowired注入,那么一个普通的类如何注入呢?

秘籍

方法一:

@Component
public class DemoUtil {
    public String get(){
        return this.toString();
    }
}

方法二:

  • 在主启动类中加入下面的代码
@Bean
public DemoUtil demoUtil() {
    return new DemoUtil();
}

测试一下

@RestController
public class DemoTest {
    @Autowired
    private DemoUtil demoUtil;

    @RequestMapping("/get")
    public String test(){
        return demoUtil.get();
    }
}
  • 输出com.service.DemoUtil@63911626

hhhhh