阳台煮酒

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在Spring工程里,有一个Car类的bean,Main.java主程序,MyBeanPostProcessor.java是Bean后置处理器。

文件目录结构如下:

 

Car.java

 1 package com.tt.spring.beans.cycle;
 2 
 3 public class Car {
 4     
 5     private String brand;
 6 
 7     public String getBrand() {
 8         return brand;
 9     }
10 
11     public void setBrand(String brand) {
12         this.brand = brand;
13         System.out.println("setBrand...");
14     }
15     
16     public Car(){
17         System.out.println("Car's Constructor...");
18     }
19     
20     public void init(){
21         System.out.println("init...");
22     }
23     
24     public void destroy(){
25         System.out.println("destroy...");
26     }
27 
28 }

Main.java:

 1 package com.tt.spring.beans.cycle;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Main {
 6     
 7     public static void main(String[] args){ 
 8         
 9         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
10         
11         Car car = (Car) ctx.getBean("car");
12         System.out.println(car);
13         
14         ctx.close();
15     }
16 
17 }

MyBeanPostProcessor.java

 1 package com.tt.spring.beans.cycle;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.config.BeanPostProcessor;
 5 
 6 public class MyBeanPostProcessor implements BeanPostProcessor {
 7 
 8     @Override
 9     public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
10         // TODO Auto-generated method stub
11         System.out.println("postProcessAfterInitialization: "+arg0+","+arg1);
12         return arg1;
13     }
14 
15     @Override
16     public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
17         // TODO Auto-generated method stub
18         System.out.println("postProcessBeforeInitialization: "+arg0+","+arg1);
19         return arg1;
20     }
21 
22 }

bean-cycle.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:util="http://www.springframework.org/schema/util"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10 
11    <bean id="car" class="com.tt.spring.beans.cycle.Car"
12          init-method="init"
13          destroy-method="destroy">
14          <property name="brand" value="Audi"></property>
15    </bean>
16    
17    <!-- 配置bean的后置处理器 -->
18    <bean class="com.tt.spring.beans.cycle.MyBeanPostProcessor"></bean>
19 </beans>

之前在xml文件中不配置bean的后置处理器时,运行正常,会init()和destroy()。但是添加了bean的后置处理器之后,

报错如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'car' defined in class path resource [beans-cycle.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tt.spring.beans.cycle.Main.main(Main.java:11)
Caused by: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 12 more

 

posted on 2017-02-15 21:10  阳台煮酒  阅读(12237)  评论(1编辑  收藏  举报