标签:erp ons 配置 public row cycle 通过 创建对象 逻辑
public class Car { public Car() { super(); System.out.println("Car constructor..."); } public void init() { System.out.println("car ... init..."); } public void destory() { System.out.println("car .. destory..."); } }
/** * bean的生命周期 * bean创建 --- 初始化 --- 销毁过程 * 容器管理bean的生命周期: * 我们可以自定义初始化和销毁方法, 容器在bean进行到当前生命周期的时候调用我们自定义的初始化和销毁方法. */ @Configuration public class MainConfigOfLifeCycle { @Bean(initMethod = "init", destroyMethod = "destory") public Car car() { return new Car(); } }
@Component public class Cat implements InitializingBean, DisposableBean { public Cat() { super(); System.out.println("cat constructor..."); } @Override public void destroy() throws Exception { System.out.println("cat...destory..."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("cat...afterPropertiesSet"); } }
@ComponentScan("test.bean") @Configuration public class MainConfigOfLifeCycle { //... }
@Component public class Dog { public Dog() { super(); System.out.println("Dog constructor..."); } //对象创建并赋值之后调用 @PostConstruct public void init() { System.out.println("Dog...@PostConstruct"); } //容器移除对象之前 @PreDestroy public void destroy() { System.out.println("Dog...@PreDestroy"); } }
//将后置处理器加入到容器中 @Component public class MyBeanPostProcessor implements BeanPostProcessor { /** * bean: 要处理的组件中的bean. * beanName: bean的id */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization" + bean + "==>" + beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization" + bean + "==>" + beanName); return bean; } }
public class Person { @Value("张三") private String name; @Value("#{20-2}") private Integer age; //... }
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中. @PropertySource(value = { "classpath:/person.properties" }) @Configuration public class MainConfigOfPropertyValues { //... }
public class Person { @Value("张三") private String name; @Value("#{20-2}") private Integer age; @Value("${person.nickName}") private String nickName; //... }
@Test public void test01() { //1.创建ioc容器 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class); ConfigurableEnvironment environment = ac.getEnvironment(); String property = environment.getProperty("person.nickName"); System.out.println(property); ac.close(); }
标签:erp ons 配置 public row cycle 通过 创建对象 逻辑
原文地址:https://www.cnblogs.com/binwenhome/p/13071318.html