J2EE之Spring中Bean的基础配置
2016-10-30 18:25
447 查看
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>
相关文章推荐
- Spring - Bean的概念及其基础配置
- 三Spring中Bean的基础配置1
- Spring基础——在Spring Config 文件中配置 Bean
- J2EE之Spring中Bean的配置细节
- <一>Spring基础之Bean在XML中的配置
- Spring基础知识及bean的配置
- Spring基础知识及bean的配置
- spring基础-Bean的装配方式(二)基于Annotation的配置
- Spring基础——在Spring Config 文件中配置 Bean(看了一半,看整个系列)
- spring学习基础(ioc底层原理,bean的配置和注解)
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
- 三Spring中的Bean配置基础2
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
- Spring-bean基础配置
- Spring 基础教程之四:JavaBean基本配置详解
- 转:Spring Bean 配置说明
- Spring中bean的基本xml配置
- J2EE入门视频教程第一讲——环境配置及J2EE基础
- Spring2.0简明手册(系列之一 Bean的配置及使用)
- 从spring配置文件中读取bean的方法