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. 1.0_搭建PHP服务器环境的步骤

    a,下载安装一款静态web服务器,ex:HTTPD,IIS; b,下载安装PHP引擎: c,在静态服务器配置一下,声明PHP引擎的存在. 注意,以上三步可以用xampp套件来完成. 启动Apache, ...

  2. 服务器证书日期无效 SSL_DATE_INVALID

    一大早上,企业微信中提示不能使用:服务器证书日期无效  错误码:SSL_DATE_INVALID 网上资料也比较少,咨询了一下企业微信客服,建议访问网址:https://www.ssllabs.com ...

  3. ef AddDays报错

    ef func写法,在语句中不能使用adddays方法 )); 这样写就是不行 可以改为: ); 下面是我的一个案例,虽然到了最后都没有实现功能! public List<ContractBud ...

  4. 【BZOJ4723】[POI2017]Flappy Bird DP

    [BZOJ4723][POI2017]Flappy Bird Description <飞扬的小鸟>是一款风靡的小游戏.在游戏中,小鸟一开始位于(0,0)处,它的目标是飞到横坐标为X的某个 ...

  5. JPA的坑多服务主键重复

    配置如下 @Id @GeneratedValue(generator="generator") @GenericGenerator(name="generator&quo ...

  6. Exchange 2016 系统要求

    Exchange 2016 和早期版本的 Exchange Server 共存方案 Exchange 2016支持混合部署方案 Exchange 2016 的网络和目录服务器要求 目录服务体系结构: ...

  7. 我有一台 PC,上面有摄像头,怎么进行一场直播?

    如何推流与播放_Web端直播实践_最佳实践_视频直播-阿里云 https://help.aliyun.com/document_detail/57251.html?spm=a2c4g.11186623 ...

  8. 2015-03-10——简析javascript对象

    对于构造函数,它是Function对象的一个实例,可以定义自己的静态成员先实例化出对象,后执行function中内部代码 静态成员:  var abc = function () {};  //既是一 ...

  9. Delphi日期设置为NULL

    在某些情况下,需要将日期字段的值置空,这种操作比较麻烦.在操作的时候,可将变量的值(t:TDateTime)设置为0,在操作的过程中进行判断,当t的值为0或-1的时候,时间值为1899年的Delphi ...

  10. Linux入门之运维(1) 系统监控 vmstat top

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...