(待解答)Spring报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' is defined

新建了一个Spring工程,有2个类:HelloWorld、Car。在Main.java文件里获取类时,发现只能获取到HelloWorld类,不能获取到Car类。

HelloWorld.java:

 1 package com.tt.spring.beans;
 2
 3 public class HelloWorld {
 4
 5     private String name;
 6
 7     public void setName(String name) {
 8         System.out.println("hello: "+ name);
 9         this.name = name;
10     }
11
12     public void hello(){
13         System.out.println("hello: " + name);
14     }
15
16     public HelloWorld() {
17         System.out.println("HelloWorld‘s Constructor");
18     }
19
20
21 }

Car.java:

 1 package com.tt.spring.beans;
 2
 3 public class Car {
 4
 5     private String brand;
 6     private String corp;
 7     private int price;
 8     private int maxSpeed;
 9
10
11     public Car() {
12         System.out.println("Car‘s Constructor");
13     }
14
15     public Car(String brand, String corp, int price) {
16         super();
17         this.brand = brand;
18         this.corp = corp;
19         this.price = price;
20     }
21
22     @Override
23     public String toString() {
24         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
25     }
26
27 }

ApplicationContext.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     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://www.springframework.org/schema/context/spring-context.xsd
 8                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9
10    <context:component-scan base-package="com.tt.spring.beans"></context:component-scan>
11     <!--
12                      配置bean
13         class:bean的全类名,通过反射的方式在IOC容器中创建Bean实例,所以要求Bean中必须有无参的构造器
14         id:标识容器中的bean,id唯一
15     -->
16     <bean id="helloWorld" class="com.tt.spring.beans.HelloWorld">
17          <property name="name" value="Spring"></property>
18     </bean>
19
20     <!-- 通过构造方法来配置bean的属性 -->
21     <bean id="car" class="com.tt.spring.beans.Car">
22          <constructor-arg value="Audi"></constructor-arg>
23          <constructor-arg value="chengdu"></constructor-arg>
24          <constructor-arg value="30000"></constructor-arg>
25     </bean>
26
27 </beans>

Main.java:

 1 package com.tt.spring.beans;
 2
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6 public class Main {
 7
 8     public static void main(String[] args){
 9         /*
10         //创建HelloWorld的一个对象
11         HelloWorld helloWorld = new HelloWorld();
12         //为name属性赋值
13         helloWorld.setName("tt");
14         */
15         //调用hello方法
16
17         //1.创建Spring 的 IOC 容器对象
18         //ApplicationContext代表 IOC 容器
19         //ClassPathXmlApplicationContext:是ApplicationContext 接口的实现类,该实现类从类路径下来加载配置文件。
20         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
21
22         //2.从IOC容器中获取Bean实例
23         //利用id定位到IOC容器中的bean
24         HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
25
26         //利用类型返回 IOC容器中的Bean,但要求 IOC 容器中必须只能有一个该类型的Bean
27         //HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
28
29          System.out.println(helloWorld);
30
31         //3.调用Hello方法
32         //helloWorld.hello();
33
34         Car car = (Car) ctx.getBean("car");
35         //Car car = ctx.getBean(Car.class);
36         System.out.println(car);
37
38     }
39 }

运行Main.java代码,打印信息如下:

HelloWorld‘s Constructor
hello: Spring
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘car‘ is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at com.tt.spring.beans.Main.main(Main.java:34)

(待解答)Spring报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' is defined

时间: 2024-10-27 05:30:08

(待解答)Spring报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' is defined的相关文章

报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;springSessionRepositoryFilter&#39; is defined

报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成tomcat插件,还没尝试过)的时候会出现很奇怪的问题.下面的一个报错就是最近经常遇到的一个问题.因为每次遇到它时,都会花很长时间去排查,所以在此把它记录下来,以便后面再遇到此问题时,顺利过坑.报错如下: org.springframework.beans.factory.NoSuchBeanDefin

Spring报错:Exception in thread &quot;main&quot; java.lang.IllegalArgumentException at org.springframework.asm.ClassReader.&lt;init&gt;(Unknown Source)

简单搭建了一个Spring Maven工程就报错: 看到网上说是JDK 7 和 Spring3.x :JDK编译级别设置成1.7,仍然没有得到解决,采用版本为  3.2.0.RELEASE <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId>

Spring Boot 启动报错 Exception in thread &quot;main&quot; java.lang.StringIndexOutOfBoundsException: String index out of range: 37

使用命令 java -jar springBoot.jar  启动项目,结果报错如下: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37 at java.lang.String.substring(String.java:1963) at org.springframework.boot.loader.jar.JarFile.create

xom报错 Exception in thread &quot;main&quot; java.net.UnknownHostException: file

Exception in thread "main" java.net.UnknownHostException: file at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.Socket.connect(Socket.java:579

在eclipse中用java调用python报错 Exception in thread &quot;main&quot; ImportError: Cannot import site module and its dependencies

最近做项目需要用java调用python,配置了jython后,运行了例子代码: 获得一个元组里面的元素: import org.python.util.PythonInterpreter; public class FirstJavaScript { public static void main(String args[]) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('

MyBatis笔记----报错Exception in thread &quot;main&quot; org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ij34.model.UserMapper.selectUser

信息: Refreshing org[email protected]41cf53f9: startup date [Wed Apr 05 16:48:12 CST 2017]; root of context hierarchy 四月 05, 2017 4:48:12 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definiti

使用Grizzy+Jersey搭建一个RESTful框架()报错Exception in thread &quot;main&quot; java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;

报错的类涉及UriBuilder,我搜索类发现, 这个类存在于两个包中,我在baidu的时候,也有人提到是jar包冲突,我就删除了 这个依赖,问题解决了. 环境搭建过程请见地址https://blog.csdn.net/jinxfei/article/details/4714809 原文地址:https://www.cnblogs.com/BaleW/p/8668712.html

报错Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: javax/xml/bind/...

首先我的jdk是11.05的 这个是由于: 这个是 由于缺少了javax.xml.bind,在jdk10.0.1中没有包含这个包,所以我自己去网上下载了jdk 8,然后把jdk10.0.1换成jdk 8问题就解决了 网址:jdk下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 重新运行后就没有报错了,希望能解决大家的问题 但其实我个人认为最好的解决的方法是在项目加个 

myeclipse运行tomcat报错Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: PermGen space

将myeclipse所配置的tomcat的jdk进行设置:-Xms512m -Xmx512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m,如下图: