Spring_配置 Bean(2)
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)的更多相关文章
- Spring_配置Bean & 属性配置细节
1.Spring容器 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.Sp ...
- Spring_配置 Bean(1)
- Spring_通过 FactoryBean 配置 Bean
beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...
- Spring_通过工厂方法配置 Bean
beans-factory.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&q ...
- Spring_通过Bean的Factory配置Bean
package com.tanlei.bean.FactoryBean; import org.springframework.beans.factory.FactoryBean; public cl ...
- Spring下如何配置bean
本次讲述项目背景: 创建Service类,Service下用到dao类.通过在Spring中配置bean,实现在项目启动时,自动加载这个类 本次只讲述配置bean的注意事项,故只给出简单实例: 创建S ...
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
- Spring学习记录(九)---通过工厂方法配置bean
1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...
- Spring--通过注解来配置bean【转】
Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...
随机推荐
- 关于 HTML5 的文件上传处理,兼容性问题,以及 BLOB 对象的使用 (转载)
研究过程中关于本主体的相关参考 好文:https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/ 好文:http: ...
- Android 切换主题以及换肤的实现
Android 切换主题以及换肤的实现 一.介绍 现在市面上有很多 APP 有切换主题和皮肤的功能!特别是阅读类的 APP! 上面两张图分别是 知乎 APP 和Fuubo APP的两张截图!都带有切换 ...
- [MongoDB]学习笔记--User管理
1. 创建一个超级用户 use admin db.createUser( { user: "adminUserName", pwd: "userPassword" ...
- 160728、Spark Streaming kafka 实现数据零丢失的几种方式
定义 问题开始之前先解释下流处理中的一些概念: At most once - 每条数据最多被处理一次(0次或1次) At least once - 每条数据最少被处理一次 (1次或更多) Exactl ...
- [荐][转]为何应该使用 MacOS X(论GUI环境下开发人员对软件的配置与重用)
一周前我和 Tinyfool 闲聊苹果操作系统,都认为对于开发人员来说,苹果操作系统(MacOS)是上佳的选择.Tinyfool 笔头很快,当即就写了一篇长文章,我则笔头很慢,今天才全部码好.他的文章 ...
- Python WSGI v1.0 中文版(转)
add by zhj: WSGI全称Web Server Gateway Interface,即Web网关接口.其实它并不是OSI七层协议中的协议,它就是一个接口而已,即函数,而WSGI规定了该接口的 ...
- Pandas 之 过滤DateFrame中所有小于0的值并替换
Outline 前几天,数据清洗时有用到pandas去过滤大量数据中的“负值”: 把过滤出来的“负值”替换为“NaN”或者指定的值. 故做个小记录. 读取CSV文件 代码: import pandas ...
- CNI flannel插件
// plugins/meta/flannel/flannel.go func cmdAdd(args *skel.CmdArgs) error 调用n, err := loadFlannelNetC ...
- WEB安全验收参考文档——From Github
文章https://xianzhi.aliyun.com/forum/read/793.html 里面涉及到了web安全验收参考文档: 其实github上老外对此也做过一些整理.详情参考:https: ...
- node.js---sails项目开发(6)--- 实现分页功能
只需要添加一个文件即可 api/blueprints/find.js 代码如下 /** * Module dependencies */ var util = require('util') ...