Spring架构模块简介链接:https://blog.csdn.net/weixin_45528987/article/details/106152970
第一步:首先创建一个Maven工程
第二步:在pom下添加依赖Context模块依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
第三步:简简单单的测试
public class SpringTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
@Configuration//表明为配置类
@ComponentScan("com.zxf.beans")//扫描加载bean的包路径
public class MySpringConfig {
@Bean
public Student student(){
return new Student("xiaoming", 20);
}
}
@Component
public class Student {
private String name ;
private int age;
......
}
注意:Spring容器优先加载配置类下的Bean
重要提示
IDEA下的Maven的将source也下载了,方便之后源码阅读时能够查看注释,源码的注释能够助于我们理解源码的意思。