J2EE之Spring中Bean的基础配置
代码:
Car.java:
package com.mooc.spring.beans;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
}
}
HelloWord.java:
package com.mooc.spring.beans;
public class HelloWord {
private String name;
public void setName(String name) {
System.out.println("setname:"+name);
this.name = name;
}
public void hello(){
System.out.println("hello"+name);
}
public HelloWord(){
System.out.println("HelloWord's Constractor....");
}
}
main.java:
package com.mooc.spring.beans;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Locale;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
public class main {
public static void main(String[] args) {
/*
//创建HelloWord的一个对象
HelloWord helloWord=new HelloWord();
//为那么赋值
helloWord.setName("atguigu");
helloWord.hello();
*/
//1.创建SpringIOC的容器对象
//ApplicationContext 代表IOC容器
//ClassPathXmlApplicationContext:从 类路径下加载配置文件,是ApplicationContext接口的实现类
//FileSystemXmlApplicationContext: 从文件系统中加载配置文件,是ApplicationContext接口的实现类
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从IOC容器中获取Bean对象
// HelloWord helloWord=(HelloWord) context.getBean("helloWord");
HelloWord helloWord=context.getBean(HelloWord.class);//适用类型获取,只适用于唯一的一哥bean
System.out.println(helloWord);
//调用hello方法
helloWord.hello();
Car car=(Car) context.getBean("car");
System.out.println(car);
car =(Car) context.getBean("car2");
System.out.println(car);
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
id:标示容器中的bean,id必须唯一 HelloWord helloWord=(HelloWord) context.getBean("helloWord");
-->
<bean id="helloWord" class="com.mooc.spring.beans.HelloWord">
<property name="name" value="Spring"></property>
</bean>
<!-- 通过构造方法来配置Bean的属性 ,用index确定顺序,以type确定类型,以区分重载构造器-->
<bean id="car" class="com.mooc.spring.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"> </constructor-arg>
</bean>
<bean id="car2" class="com.mooc.spring.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"> </constructor-arg>
</bean>
</beans>