我是靠谱客的博主 高挑巨人,这篇文章主要介绍Spring基于java方式的配置,现在分享给大家,希望可以做个参考。

@configuration注解可以完全使用java的方式配置Spring

完全不使用xml配置,全权交由java来做

  • 创建实体类Student
复制代码
1
2
3
4
5
6
7
8
9
@AllArgsConstructor @NoArgsConstructor @Data @Component public class Student{ private int id; private String name; }
  • 创建配置类MyConfig
复制代码
1
2
3
4
5
6
7
8
9
10
@Configuration @Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的 inculde 标签 public class MyConfig { @Bean public Student student(){ return new Student(1,"xxxxxxxx"); } }

测试:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test01 { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Student student = applicationContext.getBean("student", Student.class); System.out.println(student); } @Test public void test2(){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Object student = applicationContext.getBean("student"); System.out.println(student); } }

最后

以上就是高挑巨人最近收集整理的关于Spring基于java方式的配置的全部内容,更多相关Spring基于java方式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部