Spring5源码阅读-01测试项目搭建

2020-12-10  本文已影响0人  惊天动地猪儿虫

从本周开始,逐步的根据项目学习下Spring5.0的源码

1 测试项目搭建

使用IDEA+MAVEN+Spring5.0搭建个最简单的测试项目


image.png

源码如下:

@Configuration
@ComponentScan("com.test")
public class AppConfig {
}
public interface Dao {
    void query();
}
@Component
public class IndexDao implements Dao{
    @Override
    public void query() {
        System.out.printf("IndexDao query");
    }
}
public interface Service {
    void query();
}
@Component
public class IndexService implements Service {

    @Autowired
    private Dao dao;

    @Override
    public void query() {
        System.out.printf("IndexService query");
        dao.query();
    }
}
public class TestMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Service service = (Service) context.getBean("indexService");
        service.query();
    }
}
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.0.RELEASE</version>
    </dependency>
  </dependencies>

2 测试结果

十二月 10, 2020 3:46:49 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5a2e4553: startup date [Thu Dec 10 15:46:49 CST 2020]; root of context hierarchy
IndexService queryIndexDao query
Process finished with exit code 0
上一篇 下一篇

猜你喜欢

热点阅读