IoC容器可以自动管理Bean的初始化和销毁方法,用到的就是@PostConstruct(构造后)和@PreDestory(销毁前)注解.
@Service //@Scope("singleton")//默认单例模式 public class AlphaService { public AlphaService(){ System.out.println("实例化AlphaService"); } @PostConstruct public void init(){ System.out.println("初始化AlphaService"); } @PreDestroy public void destory(){ System.out.println("销毁AlphaService"); } }
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = CommunityApplication.class) //这个是为了在test中用main的环境,因为Spring容器的注入有两个条件: //@SpringBootApplication所在包及其子包内,相应的注解这里就是@Service public class CommunityApplicationTests implements ApplicationContextAware { public ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } //上面就是构建IoCr @Test public void testManagementBean() { AlphaService alphaService = applicationContext.getBean(AlphaService.class); System.out.println(alphaService); alphaService = applicationContext.getBean(AlphaService.class); System.out.println(alphaService); } }
2019-12-22 10:30:10.211 INFO 35996 --- [ main] c.n.community.CommunityApplicationTests : Starting CommunityApplicationTests on wan-HP with PID 35996 (started by wan in E:\牛客高薪项目课\所有素材和源码\第一章素材和源码\源码\community) 2019-12-22 10:30:10.212 INFO 35996 --- [ main] c.n.community.CommunityApplicationTests : No active profile set, falling back to default profiles: default 实例化AlphaService 初始化AlphaSevice 2019-12-22 10:30:11.906 INFO 35996 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-12-22 10:30:12.094 WARN 35996 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration) 2019-12-22 10:30:12.312 INFO 35996 --- [ main] c.n.community.CommunityApplicationTests : Started CommunityApplicationTests in 2.451 seconds (JVM running for 3.52) com.nowcoder.community.service.AlphaService@de81be1 com.nowcoder.community.service.AlphaService@de81be1 2019-12-22 10:30:12.833 INFO 35996 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 销毁AlphaService Process finished with exit code 0
这里我们可以看到两次的getBean我们只会实例化一次,即单例。如果我们希望每次getBean都实例化,那么我们就需要切换为多例模式(prototype)
@Service @Scope("prototype") public class AlphaService { public AlphaService(){ System.out.println("实例化AlphaService"); } @PostConstruct public void init(){ System.out.println("初始化AlphaSevice"); } @PreDestroy public void destory(){ System.out.println("销毁AlphaService"); } }
2019-12-22 11:01:16.246 WARN 37288 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration) 2019-12-22 11:01:16.505 INFO 37288 --- [ main] c.n.community.CommunityApplicationTests : Started CommunityApplicationTests in 2.613 seconds (JVM running for 3.686) 实例化AlphaService 初始化AlphaSevice com.nowcoder.community.service.AlphaService@568750b7 实例化AlphaService 初始化AlphaSevice com.nowcoder.community.service.AlphaService@3e28fee1 2019-12-22 11:01:17.058 INFO 37288 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' Process finished with exit code 0
这里我们发现他实例化了两次, 但是我们发现并没有出现销毁方法,这是由于多例对象Spring容器只负责创建而不负责回收,因此不会调用销毁方法。
下面的链接中博主详细测试了多例方法的生命周期:
spring-多例对象prototype-生命周期方法问题