新建了一个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