Spring全家桶笔记:核心概念与配置
Spring概述
Spring是一个轻量级的Java开发框架,旨在简化企业级应用开发。其核心是控制反转(IOC)和面向切面编程(AOP)。
IOC容器
什么是IOC
IOC(Inversion of Control)即控制反转,将对象创建和管理权交给Spring容器,由容器负责注入依赖。
容器实现
- BeanFactory:基础容器,延迟加载
- ApplicationContext:增强版容器,继承BeanFactory
Bean配置方式
1. XML配置
1 2 3 4
| <bean id="userService" class="com.example.UserService"> <property name="userDao" ref="userDao"/> <property name="name" value="张三"/> </bean>
|
2. 注解配置
1 2 3 4 5 6 7 8 9
| @Component public class UserService { @Autowired private UserDao userDao; @Value("${app.name}") private String appName; }
|
3. Java配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Configuration public class AppConfig { @Bean public UserService userService() { UserService service = new UserService(); service.setUserDao(userDao()); return service; } @Bean public UserDao userDao() { return new UserDaoImpl(); } }
|
Bean作用域
| 作用域 |
说明 |
| singleton |
单例(默认) |
| prototype |
多例 |
| request |
HTTP请求 |
| session |
HTTP会话 |
| application |
ServletContext生命周期 |
生命周期
- 实例化 → 2. 属性赋值 → 3. 初始化 → 4. 销毁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Component public class UserService implements InitializingBean, DisposableBean { @PostConstruct public void init() { System.out.println("初始化"); } @PreDestroy public void destroy() { System.out.println("销毁"); } @Override public void afterPropertiesSet() {} @Override public void destroy() {} }
|
依赖注入
注入方式
- 构造器注入:推荐,清晰表示依赖关系
- Setter注入:可选注入
- 字段注入:简洁但不利于测试
1 2 3 4 5 6 7 8 9 10 11 12
| @Service public class OrderService { private final UserService userService; @Autowired private OrderDao orderDao; public void setPaymentService(PaymentService paymentService) { } }
|
自动装配
1 2 3 4 5 6 7 8 9 10
| @Autowired private UserDao userDao;
@Autowired private UserDao userDaoImpl;
public UserService(UserDao userDao) {}
|
AOP面向切面编程
概念
- Aspect:切面
- Join Point:连接点
- Pointcut:切点
- Advice:通知(增强)
- Weaving:织入
通知类型
| 类型 |
说明 |
| @Before |
前置通知 |
| @AfterReturning |
后置通知 |
| @AfterThrowing |
异常通知 |
| @After |
最终通知 |
| @Around |
环绕通知 |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") public void servicePointcut() {} @Before("servicePointcut()") public void before(JoinPoint joinPoint) { System.out.println("方法: " + joinPoint.getSignature()); } @AfterReturning(pointcut = "servicePointcut()", returning = "result") public void afterReturning(Object result) { System.out.println("返回: " + result); } @Around("servicePointcut()") public Object around(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long end = System.currentTimeMillis(); System.out.println("耗时: " + (end - start) + "ms"); return result; } }
|
常用配置
属性配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring: application: name: myapp profiles: active: dev
server: port: 8080
custom: name: test max: 100
|
1 2 3 4 5
| @Value("${custom.name}") private String name;
@Value("${custom.max}") private Integer max;
|
多环境配置
- application.yml(默认)
- application-dev.yml
- application-prod.yml
条件配置
1 2 3
| @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") @ConditionalOnMissingBean(UserService.class) @ConditionalOnClass(MongoTemplate.class)
|
Spring Boot自动配置
原理
@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
@EnableAutoConfiguration 通过spring.factories和META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports加载配置。
自定义-starter
- 创建autoconfigure模块
- 定义配置类
- 创建spring.factories
- 创建starter模块依赖autoconfigure
总结
Spring核心概念包括IOC容器、依赖注入和AOP。理解这些概念是掌握Spring全家桶的基础。Spring Boot通过自动配置简化了开发,需要理解其原理以便更好地定制和调试。