spring容器中bean的初始化方式大體有三種:
@PostConstruct ,@PreDestroy: 從Java EE5規范開始,Servlet中增加了兩個影響Servlet生命周期的注解,@PostConstruct和@PreDestroy。@PostConstruct會在Servlet構造函數之后,初始化之前執行
package com.edu.bean; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ @Component public class Color { @PostConstruct public void init(){ System.out.println("init......"); } @PreDestroy public void destroy(){ System.out.println("destroy........."); } } package com.edu.bean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ public class Test1 { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.edu.bean"); context.close(); } }
輸出:
init......
destroy.........
- @Bean中指定initMethod,destroyMethod方法
package com.edu.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ public class Color { @PostConstruct public void init(){ System.out.println("init......"); } public void init1(){ System.out.println("init1......"); } @PreDestroy public void destroy(){ System.out.println("destroy........."); } public void destroy1(){ System.out.println("destroy1........."); } } package com.edu.bean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ @Configuration class Config { @Bean(name = "color",initMethod = "init1",destroyMethod = "destroy1") public Color getColor(){ return new Color(); } } package com.edu.bean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ public class Test1 { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); context.close(); } }
輸出:
init......
init1......
destroy.........
destroy1.........
證明@PostConstruct 修飾的方法是在initMethod標注的方法之前執行,@PreDestroy修飾的方法是在destroyMethod 標注的方法之前執行
- interface InitializingBean,DisposableBean:重寫afterPropertiesSet,destroy方法
package com.edu.bean; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ @Component public class Color implements InitializingBean, DisposableBean { @PostConstruct public void init(){ System.out.println("init......"); } @PreDestroy public void destroy(){ System.out.println("destroy........."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("init1........."); } @Override public void destroy() throws Exception { System.out.println("destroy1......"); } } package com.edu.bean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/6/17 */ public class Test1 { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.edu.bean"); context.close(); } }
輸出:
init......
init1.........
destroy.........
destroy1......