Spring整理_03 完全注解开发

1.创建实体类,并配置Component注解:

User.java:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {
    private String name;
    private Integer age;
}

2.创建配置类

配置类中最主要的注解:@Configuration

@Configuration//这个也会被Spring容器托管,注册到容器中,因为他本身就是一个@Component
//@Configuration代表这是一个配置类,相当于之前的beans.xml
//这里也可以使用配置文件中的功能
//比如:
@ComponentScan("com.jiabowen.pojo")//扫描包
//如果有两个或多个类就使用Import
@Import(Config2.class)
public class Config {
    //注册一个bean,相当于我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User getUser(){
        return new User();
    }
}

 

posted @ 2021-09-28 21:36  贾博文  阅读(69)  评论(0)    收藏  举报