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. java的Date类型转换为MySQL数据库的Date类型

    最近遇到一个问题,需要把java中的日期类型存放为MySQL数据库的日期类型,两个日期之间需要进行转化才能进行存储,转化代码如下: package com.alphajuns.demo1; impor ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.4——使用Espresso测试Activity

    问题: 你想要使用Google的Espresso测试Activity. 解决方案: 在Gradle配置里面增加Espresso,书写测试脚本. 讨论: Espresso测试库已经被添加进“Androi ...

  3. 57、Design Support Library 介绍及环境搭建

    一.Material Design几个要素 扁平化.简洁: 水波反馈: 良好体验的过渡动画: 材料空间位置的直观变化: 二.Android Studio配置 在 build.gradle 文件中加入, ...

  4. 认识tornado(二)

    前面我们对 Tornado 自带的 hello world 作了代码组织上的解释,但是没有更加深入细致地解释.这里我们直接从main()函数开始,单步跟随,看看tornado都干了些什么. 下面是 m ...

  5. 【IE兼容性】代码中多语言样式+IE不兼容解决

    一.代码中样式根据不同语言对IE做不兼容解决 二.代码逻辑: 1. 后台返回语言信息: result.addObject("language",getLocaleStr());   ...

  6. Pycharm如何取消自动换行

    1.只对当前文件有效的操作是: 菜单栏->View -> Active Editor -> Use Soft Wraps (不选中) 2.要是想对所有文件都起到效果,就要在setti ...

  7. python框架Scrapy报错TypeError: 'float' object is not iterable解决

    原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: pip3 install Twisted== 注:Twisted16..0安装后,会自动卸载高版本的Twi ...

  8. 解决ajax post json数据 后端无法收到的问题

    如图,想把input框中的文字以json格式post出去,结果后端收不到 使用wireshark抓包,根本没有抓到发往服务器的包,说明错误在前端. 后来发现ajax post json数据的时候key ...

  9. 安卓使用Canvas绘制工作日程表

    有一个项目要使用工作表,选择使用canvas来绘制.实现显示工作日程的选择,可点击加入和取消,效果图:http://jwzhangjie.com/workplan.gif 自己定义控件FormView ...

  10. Python高级教程-列表生成式

    List Comprehensions(列表生成式) 列表生成式,是Python内置的非常简单却强大的可以用来创建list的生成式. 例如,要生成list:[1,2,3,4,5,6,7,8,9,10] ...