本文是介绍spring源码的开始,先了解最基础的使用,最深入源码。
spring源码下载地址 https://github.com/spring-projects/spring-framework.git
依赖 spring-context
dependencies {
implementation(project(":spring-context"))
}
package com.shura.beans;
import org.springframework.stereotype.Component;
@Component
public class UserBean {
}
主要用于定义Bean的扫描路径 通过ComponentScan注解定义
package com.shura.config;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan({"com.shura.beans"})
public class AppConfig {
}
package com.shura;
import com.shura.beans.UserBean;
import com.shura.config.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author shura
* @since 2023/9/10 19:43
*/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserBean bean = context.getBean(UserBean.class);
System.out.println(bean);
}
}
输出
com.shura.beans.UserBean@1e81f4dc
以上就是spring的一个入门使用,最开始入门的时候可能最先接触的是 ClassPathXmlApplicationContext ,考虑到后面的SpringBoot 使用的是 AnnotationConfigApplicationContext,后面的源码介绍也主要以 AnnotationConfigApplicationContext为主
欢迎关注,学习不迷路!