您现在的位置是:主页 > news > 易语言怎么做视频网站/广州seo网站推广平台

易语言怎么做视频网站/广州seo网站推广平台

admin2025/5/23 1:14:58news

简介易语言怎么做视频网站,广州seo网站推广平台,韩国美食做视频网站,凡客诚品官方客服有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;通过 Bean的 initMethod/destroyMethod 属性指定初始化之后 /销毁之前调用的操作方法;在指定方法上加上PostConstruct 或PreDestroy注解来制定该方法是在…

易语言怎么做视频网站,广州seo网站推广平台,韩国美食做视频网站,凡客诚品官方客服有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;通过 Bean的 initMethod/destroyMethod 属性指定初始化之后 /销毁之前调用的操作方法;在指定方法上加上PostConstruct 或PreDestroy注解来制定该方法是在…

有三种方式

  • 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
  • 通过 @Bean的 initMethod/destroyMethod 属性指定初始化之后 /销毁之前调用的操作方法;
  • 在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

具体代码

@Configuration
@ComponentScan("com.example.config")
public class AppConfig {@Bean(initMethod = "pigInitMethod",destroyMethod = "pigDestroyMethod")public Pig Pig(){return new Pig();}
}
public class Pig implements InitializingBean, DisposableBean {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@PostConstructpublic void postConstruct(){System.out.println("这是@PostConstruct注解标注得方法");}//对象被从ioc容器中移除之前调用@PreDestroypublic void preDestroy() {System.out.println("@PreDestroy(销毁逻辑)注解");}private void pigInitMethod() {System.out.println("使用@Bean注解指定初始化执行");}private void pigDestroyMethod() {System.out.println("使用@Bean注解指定初始化和销毁方法执行");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("实现InitializingBean的afterPropertiesSet()初始方法");}@Overridepublic void destroy() throws Exception {System.out.println("实现DisposableBean接口自定义销毁方法");}
}

可以再添加一个自定义的 BeanPostProcessor

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(bean.getClass().getName()+"Bean 初始化之前");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(bean.getClass().getName()+"Bean 初始化之前后");return bean;}
}

测试代码

@Test
public void testInitMethod(){AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class );System.out.println("打印输出Pig对象"+ context.getBean( Pig.class ));context.close();}

输出结果

com.example.po.PigBean 初始化之前      
这是@PostConstruct注解标注得方法
实现InitializingBean的afterPropertiesSet()初始方法
使用@Bean注解指定初始化执行
com.example.po.PigBean 初始化之前后
打印输出Pig对象com.example.po.Pig@52045dbe
@PreDestroy(销毁逻辑)注解
实现DisposableBean接口自定义销毁方法
使用@Bean注解指定初始化和销毁方法执行

由输出结果可以看出

@PostConstruct 或@PreDestroy注解 优先级最高

实现 InitializingBean/DisposableBean接口 优先级次之

@Bean的 initMethod/destroyMethod属性指定初始化 优先级最低