Main(测试方法)

public class Main {

public static void main(String[] args) {

//1.创建Spring 的IOC容器对象;

//spring提供了两种IOC容器的实现

//1.BeanFactory:面向spring本身,,(就是底层的东西)

//2.ApplicationContext: 面向使用spring框架的开发者,她提供了更多高级特性,是BeanFactory的子接口

// ----ConfigurableApplicationContext:ApplicationContext的子接口;增加了两个方法,refresh()和close()

// -------主要实现类ClassPathXmlApplicationContext:从类路径下加载配置文件

//--------主要实现类FileSystemXmlApplicationContext:从文件系统中加载配置文件

//applicationContext 代表IOC容器,

ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");

//2.从IOC容器中获取beans实例

HelloWorld helloWorld =(HelloWorld) cxt.getBean("helloWorld");

//3.调用hello方法

helloWorld.hello();

Car car = (Car) cxt.getBean("car");

System.out.println(car);

Car car2 = (Car)cxt.getBean("car2");

System.out.println(car2);

Person person = (Person) cxt.getBean("person");

System.out.println(person);

Person1 person3 = (Person1)cxt.getBean("person3");

System.out.println("集合属性:::"+person3);

Person2 person2 = (Person2)cxt.getBean("person2");

System.out.println("Map属性:::"+person2);

DataSource dataSource = (DataSource) cxt.getBean("dataSource");

System.out.println(dataSource);

Person1 person4 = (Person1)cxt.getBean("person4");

System.out.println("集合属性:::"+person4);

Person2 person5 = (Person2)cxt.getBean("person5");

System.out.println("Map属性:::"+person5);

Person1 person6 = (Person1)cxt.getBean("person6");

System.out.println("p属性:::"+person6);

}

/**************************************************************************************/

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"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.0.xsd

">

<!-- 配置bean -->

<!-- class的路径一般都是包名+类名,中间用“.”分开

class:bean的全类目名,通过反射的方式在IOC容器中穿件Bean,所以要求bean中有无参数的构造器

id:标识容器中的bean,id值唯一

-->

<!-- property 是属性配置,这里是属性注入  -->

<bean id="helloWorld" class ="lixiuming.spring.beans.HelloWorld">

<property name="name" value="Spring"></property>

</bean>

<!-- 通过构造方法来配置bean属性 ,可以指定参数的位置和参数的类型,来区分重载的构造器  value值可以拿到外面,遇到特殊字符用CDATA-->

<bean id="car" class="lixiuming.spring.beans.Car">

<constructor-arg  index="0">

<value><![CDATA[<Shanghai>]]></value>

</constructor-arg>

<constructor-arg  index="1">

<value>shanghai</value>

</constructor-arg>

<constructor-arg value="30000000" index="2"></constructor-arg>

</bean>

<bean id="car2" class="lixiuming.spring.beans.Car">

<constructor-arg value="Audi" index="0"></constructor-arg>

<constructor-arg value="Shanghai" index="1"></constructor-arg>

<constructor-arg value="240" index="2" type="int"></constructor-arg>

</bean>

<!-- 配置person

——引用其他外部Bean    ref="car2"

——内部Bean创建   内部的bean 不能被外部引用

——级联属性

-->

<bean id="person" class="lixiuming.spring.beans.Person">

<property name="name" value="LiXiuming"></property>

<property name="age" value="18" ></property>

<!-- <property name="car" ref="car2"></property>

--> <!-- 内部Bean引用 -->

<property name= "car">

<bean class="lixiuming.spring.beans.Car">

<constructor-arg value="Fode"></constructor-arg>

<constructor-arg value="Changan"></constructor-arg>

<constructor-arg value="200"  type="int"></constructor-arg>

</bean>

</property>

<property name="car.price" value="300000"></property>

</bean>

<!--  配置集合属性 -->

<bean id="person3"  class="lixiuming.spring.beans.Person1">

<property name="name" value="Mike"></property>

<property name="age" value="20"></property>

<property name="car" >

<list>

<ref bean="car"></ref>

<ref bean="car2"></ref>

<bean class="lixiuming.spring.beans.Car">

<constructor-arg value="Fode"></constructor-arg>

<constructor-arg value="Changan"></constructor-arg>

<constructor-arg value="200"  type="int"></constructor-arg>

</bean>

</list>

</property>

</bean>

<!-- 配置Map属性值 -->

<bean id="person2" class="lixiuming.spring.beans.Person2">

<property name="name" value="tom"></property>

<property name="age" value="25"></property>

<property name="car">

<map>

<entry key="aa" value-ref="car"></entry>

<entry key="bb" value-ref="car2"></entry>

</map>

</property>

</bean>

<!-- 配置Property属性值 需要导入util 命名空间-->

<bean id="dataSource" class="lixiuming.spring.beans.DataSource"  >

<property  name="properties">

<props>

<prop key="user"> root</prop>

<prop key="password"></prop>

<prop key="jdbcUrl">jdbc:mysql:///test</prop>

<prop key="driverClass">com.mysql.jdbc.Driver</prop>

</props>

</property>

</bean>

<!-- 配置单例的集合bean以供多个bean调用 -->

<util:list id="cars">

<ref bean="car"/>

<ref bean="car2"/>

</util:list>

<bean id="person4" class="lixiuming.spring.beans.Person1">

<property name="name" value="Jack"></property>

<property name="age" value="18"></property>

<property name="car" ref="cars"> </property>

</bean>

<!-- 配置单例的map  bean以供多个bean调用 -->

<util:map id="carsmap">

<entry key="aa" value-ref="car"></entry>

<entry key="bb" value-ref="car2"></entry>

</util:map>

<bean id="person5" class="lixiuming.spring.beans.Person2">

<property name="name" value="Jack"></property>

<property name="age" value="18"></property>

<property name="car" ref="carsmap"> </property>

</bean>

<!-- 配置P命名空间为bean的属性赋值,需要先导入P命名空间 -->

<bean id="person6" class="lixiuming.spring.beans.Person1" p:age="30" p:name="aaa" p:car-ref="cars"></bean>

<!-- 对比配置servlet

<servlet>

<servlet-name>HelloServlet</servlet-name>

<servlet-class>com.bro.HelloServlet3</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloServlet</servlet-name>

<url-pattern>/HelloServlet</url-pattern>

</servlet-mapping>

-->

</beans>

/**************************************************************************************/

附录

public class HelloWorld {

private String name;

public void setName(String name){

this.name = name;

System.out.println("setName:"+name);

}

public void hello(){

System.out.println("hello:"+name);

}

}

/**************************************************************************************/

public class Car {

private String brand;

private String corp;

private double price;

private int maxSpeed;

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

public String getCorp() {

return corp;

}

public void setCorp(String corp) {

this.corp = corp;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public int getMaxSpeed() {

return maxSpeed;

}

public void setMaxSpeed(int maxSpeed) {

this.maxSpeed = 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 + "]";

}

}

/**************************************************************************************/

public class DataSource {

private Properties properties;

public Properties getProperties() {

return properties;

}

public void setProperties(Properties properties) {

this.properties = properties;

}

@Override

public String toString() {

return "DataSource [properties=" + properties + "]";

}

/**************************************************************************************/

public class Person {

private String name;

private int age;

private Car car;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Car getCar() {

return car;

}

public void setCar(Car car) {

this.car = car;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";

}

}

/**************************************************************************************/

public class Person1 {

private String name;

private int age;

private List<Car> car;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public List<Car> getCar() {

return car;

}

public void setCar(List<Car> car) {

this.car = car;

}

@Override

public String toString() {

return "Person1 [name=" + name + ", age=" + age + ", car=" + car + "]";

}

}

/**************************************************************************************/

public class Person2 {

private String name;

private int age;

private Map<String,Car>car;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Map<String, Car> getCar() {

return car;

}

public void setCar(Map<String, Car> car) {

this.car = car;

}

@Override

public String toString() {

return "Person2 [name=" + name + ", age=" + age + ", car=" + car + "]";

}

}

/**************************************************************************************/

spring 配置bean的更多相关文章

  1. Spring配置bean的详细知识

    在Spring中配置bean的一些细节.具体信息请参考下面的代码及注释 applicationContext.xml文件 <?xml version="1.0" encodi ...

  2. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  3. Spring配置Bean,为属性赋值

    SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...

  4. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  5. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  6. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  7. spring 配置bean的方法及依赖注入发方式

    Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...

  8. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  9. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

随机推荐

  1. Linux琐碎

    本周接触Linux的内容: 1.netstat -tanlp 显示监听的所有端口并且不解析端口为属于哪个进程 history | grep cmd 从命令历史中找到需要的命令 2. scp命令的使用: ...

  2. 2016 Multi-University Training Contest 3

    5/11 2016 Multi-University Training Contest 3官方题解 2016年多校训练第三场 老年选手历险记 暴力 A Sqrt Bo(CYD) 题意:问进行多少次开根 ...

  3. Qt quick 编程

    greaterThan(QT_MAJOR_VERSION,4):QT += widgets.在Qt 5之前,没有独立的QtWidgets模块,Qt Widgets包含在QtGui模块中. TARGET ...

  4. 1.0 Quartz 2D 简介

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”   Quartz2D须知:   (1)Quartz 2D是苹果官方的二维绘图引擎,同时支持 ...

  5. 邮箱、手机号、中文 js跟php正则验证

    邮箱正则: jS: var regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; //验证 if(regEmail.te ...

  6. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  7. HTML5 头部标签定义

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  8. Spring 000 框架简介 (转载)

    转载自:https://my.oschina.net/myriads/blog/37922 1.使用框架的意义与Spring的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...

  9. [bzoj1072] [SCOI2007]排列perm

    有一种暴力算法就是直接枚举. 正解就是状压dp 令f[i][j]:i:使用的数位的状态j:当前的模数 边界:f[0][0] = 1; f[i|1<<k][j*10+k % n] += f[ ...

  10. 使用jspatch进行热修复的实战总结

    最近正式在线上项目中集成了jspatch进行热修复,这里做一个简单的总结. 工具篇: 首先,用xcode来编辑js非常困难,基本上没有缩进,完全需要手写:经过研究发现使用 Sublime text3 ...