直接在类上添加@Component注解也可以创建bean,Configuration、Controller、Service、Repository直接继承了Component,只是在应用中语义不同,其他一样。比如Configuration作为创建bean的配置类使用,在其中的方法中用@Bean来创建bean,Controller在应用层,Service和Repository分别在服务层和数据库层
User.java
package com.edu.spring; import org.springframework.stereotype.Component; @Component public class User { }