从本周开始,逐步的根据项目学习下Spring5.0的源码
1 测试项目搭建
使用IDEA+MAVEN+Spring5.0搭建个最简单的测试项目
![](https://img.haomeiwen.com/i9180253/329d459c009c943e.png)
源码如下:
- AppConfig
@Configuration
@ComponentScan("com.test")
public class AppConfig {
}
- Dao
public interface Dao {
void query();
}
- IndexDao
@Component
public class IndexDao implements Dao{
@Override
public void query() {
System.out.printf("IndexDao query");
}
}
- Service
public interface Service {
void query();
}
- IndexService
@Component
public class IndexService implements Service {
@Autowired
private Dao dao;
@Override
public void query() {
System.out.printf("IndexService query");
dao.query();
}
}
- TestMain
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Service service = (Service) context.getBean("indexService");
service.query();
}
}
- pom.xml
<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
网友评论