Spring報錯: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'(待解答)


在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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is Spring 報錯 :org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'statementController' available EurekaClient項目啟動報錯Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e Spring AOP 報錯org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXXXXX' defined in class path resource.......... org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined 報錯!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined 【報錯】org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketHandlerMapping' defined in class path resource org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.my.service.ProductService] for bean with name 'productService' defi報錯解決方法 spring整合hibernate無法創建Bean----org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDao' defined in file
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM