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 唯一
-->
<bean id="helloWord" class="com.hy.spring.beans.HelloWord">
<property name="name" value="spring"></property>
</bean>

<!-- 通过构造方法来配置Bean的属性-->
<bean id="car" class="com.hy.spring.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>
<!-- 使用构造器注入属性值的位置和参数的类型!以区分重载的构造器! -->
<bean id="car1" class="com.hy.spring.beans.Car">
<constructor-arg value="BaoMa" type="String"></constructor-arg>
<constructor-arg value="China" type="String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>

</beans>

Car.java

package com.hy.spring.beans;

public class Car {
private String company;
private String brand;
private double price;
private int maxSpeed;

public Car(String company, String brand, double price) {
super();
this.company = company;
this.brand = brand;
this.price = price;
}

public Car(String company, String brand, int maxSpeed) {
super();
this.company = company;
this.brand = brand;
this.maxSpeed = maxSpeed;
}

@Override
public String toString() {
return "Car [company=" + company + ", brand=" + brand + ", maxSpeed="
+ maxSpeed + ", price=" + price + "]";
}
}

HelloWord.java

package com.hy.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("HelloWorld's Constructor");
}
}

Main.java

package com.hy.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
//1.创建 Spring 的 IOC容器的对象
// ApplicationContext 代表IOC容器
// ClassPathXmlApplicationContext : 是 ApplicationContext接口的实现类。从类路径下加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Bean实例
//利用ID 定位到IOC 容器中的bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//利用类型返回IOC 容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
//HelloWord helloWord = (HelloWord) ctx.getBean(HelloWord.class);
//3.调用hello方法
helloWord.hello();

Car car = (Car) ctx.getBean("car");
System.out.println(car);
car = (Car) ctx.getBean("car1");
System.out.println(car);

}

}

Spring_配置 Bean(2)的更多相关文章

  1. Spring_配置Bean & 属性配置细节

    1.Spring容器 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.Sp ...

  2. Spring_配置 Bean(1)

  3. Spring_通过 FactoryBean 配置 Bean

    beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...

  4. Spring_通过工厂方法配置 Bean

    beans-factory.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&q ...

  5. Spring_通过Bean的Factory配置Bean

    package com.tanlei.bean.FactoryBean; import org.springframework.beans.factory.FactoryBean; public cl ...

  6. Spring下如何配置bean

    本次讲述项目背景: 创建Service类,Service下用到dao类.通过在Spring中配置bean,实现在项目启动时,自动加载这个类 本次只讲述配置bean的注意事项,故只给出简单实例: 创建S ...

  7. Spring学习记录(十)---使用FactoryBean配置Bean

    之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...

  8. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...

  9. Spring--通过注解来配置bean【转】

    Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...

随机推荐

  1. 如何下载ubuntu桌面,并使用

    下载ubuntu,进行linux系统的操作 1.下载ubuntu 百度搜索ubuntu或直达下载链接http://cn.ubuntu.com/download/ 你可以选择,优麒麟16或者Ubuntu ...

  2. sql server剔除某列的汉字,函数。

    create function fun_del_chinese(@col varchar(1000))returns varchar(1000)ASbegin declare @returnchar ...

  3. Kotlin——中级篇(二): 属性与字段详解

    在前面的章节中,详细的为大家讲解到了Kotlin中对类的类的定义.使用.初始化.初始化.类继承等内容,但是在一个类中,几乎上是不可能不出现属性与字段(field)的,这一篇文章就为大家奉上Kotlin ...

  4. Kotlin——高级篇(五):集合之常用操作符汇总

    在上一篇文章Kotlin--高级篇(四):集合(Array.List.Set.Map)基础中讲解到了数组Array<T>.集合(List.Set.Map)的定义与初始化.但是由于篇幅的原因 ...

  5. 在java中public void与public static void有什么区别 ?

    public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...

  6. 巨蟒python全栈开发-第15天 装饰器

    一.今日内容总览 关于函数的装饰器1.装饰器(重点,难点)(要求:反复写,代码不多但是很绕) 开闭原则:(比如,菜单是拆散的,一点点搞的,用友拆散自己的功能,以后就不用开发了) (1)对功能的扩展开放 ...

  7. Exchange Version and UpdateRollups

    Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...

  8. 四.mysql演示银行转账

    代码演示: #conding:utf8 import pymysql import sys class TransferMoney(object): def __init__(self,conn): ...

  9. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)

    在python的开发中,遇到了这个错误: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TA ...

  10. 我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:

    我的Android进阶之旅-->如何解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...