【Spring笔记】使用javaconfig配置

@Configuration:注册到Spring容器中,因为本类就是Component

@Bean相当于之前写的一个bean标签。

id是方法名,返回值是resultType,return 是返回要注入到bean的对象。

@Component说明类注册到Spring容器中

@Value属性注入值

如果完全使用配置类方式去做,就只能 通过AnnotationConfig上下文来获取容器,通过配置类的class加载。

扫描二维码关注公众号,回复: 13989225 查看本文章
 AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(KuangConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser);

在配置类中引入其他配置类:

 这种纯java的配置方式,在SpringBoot中随处可见。

猜你喜欢

转载自blog.csdn.net/m0_52043808/article/details/124439425
今日推荐