本文作为SpringIOC的进阶指南,深入讲解高级特性、性能优化、最佳实践等进阶内容。在掌握基础知识的基础上,进一步提升您的SpringIOC技能水平,解决实际开发中的复杂问题。
一、高级特性
1.1 Bean作用域深入
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 27 28 29 30 31 32 33 34
| @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) @Component public class SingletonBean { }
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Component public class PrototypeBean { }
@Scope(WebApplicationContext.SCOPE_REQUEST) @Component public class RequestBean { }
@Scope(WebApplicationContext.SCOPE_SESSION) @Component public class SessionBean { }
@Scope(WebApplicationContext.SCOPE_APPLICATION) @Component public class ApplicationBean { }
|
- 单例:无状态Bean,性能最好,推荐使用
- 原型:有状态Bean,线程不安全对象
- 请求/会话:Web相关的Bean
1.2 依赖注入高级用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") @Component public class ConditionalBean { }
@Profile("dev") @Component public class DevBean { }
@Profile("prod") @Component public class ProdBean { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Service public class OrderService { @Autowired private List<PaymentProcessor> processors; @Autowired private Map<String, PaymentProcessor> processorMap; @Autowired private Set<Validator> validators; }
|
1 2 3 4 5 6 7 8 9 10 11
| @Service public class UserService { @Autowired(required = false) private OptionalService optionalService; @Autowired private Optional<OptionalService> optionalService; }
|
1.3 Bean生命周期回调
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
| @Component public class LifecycleBean implements InitializingBean { @Override public void afterPropertiesSet() { System.out.println("afterPropertiesSet执行"); } @PostConstruct public void init() { System.out.println("@PostConstruct执行"); } public void customInit() { System.out.println("自定义init方法执行"); } }
@Bean(initMethod = "customInit") public LifecycleBean lifecycleBean() { return new LifecycleBean(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Component public class LifecycleBean implements DisposableBean { @Override public void destroy() { System.out.println("destroy执行"); } @PreDestroy public void cleanup() { System.out.println("@PreDestroy执行"); } public void customDestroy() { System.out.println("自定义destroy方法执行"); } }
|
二、性能优化
2.1 Bean创建优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Configuration @Lazy public class LazyConfig { }
@Lazy @Component public class LazyBean { }
@Component public class ServiceBean { @Lazy @Autowired private HeavyBean heavyBean; }
|
1 2 3 4 5 6 7 8 9 10
| @Primary @Component public class PrimaryServiceImpl implements Service { }
@Autowired @Qualifier("specificServiceImpl") private Service service;
|
2.2 循环依赖处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Service public class ServiceA { @Autowired private ServiceB serviceB; }
@Service public class ServiceB { @Autowired private ServiceA serviceA; }
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Service public class PrototypeServiceA { @Autowired private PrototypeServiceB serviceB; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Service public class ServiceA { @Lazy @Autowired private ServiceB serviceB; }
@Service public class ServiceA { private ServiceB serviceB; @Autowired public void setServiceB(ServiceB serviceB) { this.serviceB = serviceB; } }
|
三、架构设计
3.1 设计模式应用
1 2 3 4 5 6 7 8 9 10 11 12
| ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = context.getBean(UserService.class);
@Configuration public class FactoryConfig { @Bean public UserService userService() { return UserServiceFactory.createUserService(); } }
|
1 2 3 4 5
| @Component public class SingletonService { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public interface PaymentStrategy { void pay(BigDecimal amount); }
@Component("creditCard") public class CreditCardStrategy implements PaymentStrategy { }
@Component("alipay") public class AlipayStrategy implements PaymentStrategy { }
@Service public class PaymentService { @Autowired private Map<String, PaymentStrategy> strategies; public void pay(String type, BigDecimal amount) { PaymentStrategy strategy = strategies.get(type); strategy.pay(amount); } }
|
3.2 模块化设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Configuration @Import({DatabaseConfig.class, CacheConfig.class, SecurityConfig.class}) public class AppConfig { }
@Configuration public class DatabaseConfig { @Bean public DataSource dataSource() { } }
@Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { } }
|
四、实战技巧
4.1 调试技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ApplicationContext context = ...; String[] beanNames = context.getBeanDefinitionNames(); for (String name : beanNames) { System.out.println(name); }
Object bean = context.getBean("beanName"); System.out.println(bean.getClass().getName());
BeanDefinition definition = ((BeanFactory) context).getBeanDefinition("beanName"); System.out.println(definition.getScope());
|
1 2 3 4 5 6
| management: endpoints: web: exposure: include: beans,env
|
访问:http://localhost:8080/actuator/beans
4.2 问题排查
Bean创建失败
依赖注入失败
作用域问题
1 2 3 4 5 6
|
@Lookup protected PrototypeBean getPrototypeBean() { return null; }
|
五、总结
通过本文的学习,您已经掌握了SpringIOC的进阶知识。在下一篇文章中,我们将通过实际项目案例,展示SpringIOC的实战应用。
本文标题: SpringIOC进阶篇
发布时间: 2019年04月23日 00:00
最后更新: 2025年12月30日 08:54
原始链接: https://haoxiang.eu.org/c486f11/
版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0许可协议,转载请注明出处!