下载spring包,在eclipse搭建spring环境。

这步我在eclipse中无法导入包,看网上的:

http://sishuok.(和谐)com/forum/blogPost/list/2428.html

建一个java project

三个java文件,一个xml文件

 package com.guigu.spring.beans;

 public class HelloWord {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("hello: "+name);
}
}
 import java.applet.AppletContext;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
// 传统调用方式
// HelloWord h = new HelloWord();
// h.setName("evan");
// h.hello();
//1、读取配置文件实例化一个IoC容器 ,会动态创建对象,调用构造函数,xml中有属性还会调用setname()
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//2、从容器中的id获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWord hello = (HelloWord) context.getBean("helloword");
//3、执行业务逻辑
hello.hello();
} }
 <?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- id 表示你这个组件的名字,class表示组件类 -->
<bean id="helloword" class="com.guigu.spring.beans.HelloWord">
<property name="name" value="Spring"></property>
</bean>
</beans>

helloword是一个id标识,它属于com.guigu.spring.beans.HelloWord类,是它的一个实例。一个类可以有多个实例。所以可以有多个bean的类相同,表示一个类的不同实例。

bean中可以添加属性,用property属性

IoC容器

用的是ApplicationContext(应用上下文),它是由BeanFactory(Bean工厂)扩展而来,BeanFactory提供了IoC容器最基本功能如: getBean();

ApplicationContext扩展了BeanFactory,还提供了与Spring AOP集成、国际化处理、事件传播及提供不同层次的context实现。

【  ApplicationContext 的初始化和BeanFactory 有一个重大的区别:BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean 时才实例目标Bean,即调用getBean才初始化;而ApplicationContext 则在初始化应用上下文时就实例化所有单实例的Bean 。】

(了解更多去大牛博客:http://elf8848.iteye.com/blog/324796)

ApplicationContext的实现有两个:

• ClassPathXmlApplicationContext从类路径获取配置文件。

 ApplicationContext context = new ClassPathXmlApplicationContext("application.xml") 

• FileSystemXmlApplicationContext从文件系统获取配置文件。

ApplicationContext context = new FileSystemXmlApplicationContext("c:/application.xml");  

ApplicationContext获取bean的方法:

1、通过id获取容器中的bean

 ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
HelloWord hello = (HelloWord) context.getBean("helloword");

2、通过类类型获取。但是只能在HelloWord类只有一个bean对象才行,不然就不知道到底获取哪一个而报错

  ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
HelloWord hello = context.getBean(HelloWord.class); //不用类型强转,因为已经知道返回类型了

IOC容器注入依赖资源,三种注入方式:

1、属性注入(最常用)

2、构造器注入

3、工厂方法注入(很少用,不推荐)

属性注入:通过setter方法注入;使用<property>,使name属性指定bean的属性名称

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

表示bean所在类有一个name属性,值是Spring

构造器注入 

用bean里的constructor-arg属性

例子--新建一个Car类

 public class Car{
private String brand;
private String corp;
private double price;
private int maxSpeed;
}
public Car(String brand,String corp,double price){
super();
this.brand=brand;
this.corp=corp;
this.price=price;
}

xml中添加,index表示对应的属性,若不写,默认按顺序匹配

 <bean id="car" class="com.guigu.spring.beans.Car">
<constructor-arg value="aodi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>

main函数中

   ApplicationContext ctx= new ClassPathXmlApplicationContext("application.xml");
HelloWord car=ctx.getBean(Car.class);
system.out.println(car); //调用Car中的toString函数,已省略

输出结果:Car [brand=aodi, corp=ShangHai, price=300000, maxSpeed=0];

若构造函数是多个呢?重载的构造函数,xml中对应哪一个呢?

 public class Car{
private String brand;
private String corp;
private double price;
private int 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;
}
  <bean id="car2" class="com.guigu.spring.beans.Car">
<constructor-arg value="aodi" type="java.lang.String"></constructor-arg> //除基本类型外,其余都要写全类名
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg> //基本类型,不用谢全类名
</bean>
    ApplicationContext ctx= new ClassPathXmlApplicationContext("application.xml");
HelloWord car=ctx.getBean("car");
system.out.println(car);
HelloWord car=ctx.getBean("car2");
system.out.println(car);

输出结果:

Car [brand=aodi, corp=ShangHai, price=300000.0, maxSpeed=0];

Car [brand=aodi, corp=ShangHai, price=0.0, maxSpeed=240];

总结:通过构造器方法时,可以用参数的位置和参数的类型来区分重载构造函数,位置和参数是可以同时使用,如下

  <bean id="car" class="com.guigu.spring.beans.Car">
<constructor-arg value="aodi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>

细节1:value用节点表示

   <bean id="car" class="com.guigu.spring.beans.Car">
<constructor-arg value="aodi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg type="int">
<value>240</value>
</constructor-arg>
</bean>

这个可以解决value是特殊字符的问题 如:<ShangHai> ,直接字面写就报错,用value节点再结合<![CDATA[]]解决

    <bean id="car" class="com.guigu.spring.beans.Car">
<constructor-arg value="aodi" index="0"></constructor-arg>
<constructor-arg index="1">
<value><![CDATA[<ShangHai>]]></value> // 值是:<ShangHai>
</constructor-arg>
<constructor-arg value="240" type="int">
</constructor-arg>
</bean>

细节2:属性是引用类型 ref元素或ref属性

若又一个Person类,有一个Car属性,Car就是前面的对象

   public class Person{
private String name;
private Car car;
} public void setName(String name){
this.name=name;
}
public getName(){
return name;
}
public void setCar(Carcar){
this.car=car;
}
public getCar(){
return car;
} }

用ref="car2" 指向之前的car2对象,来引用;同样可以改成节点式<ref bean="car2"/>

  <bean id="person" class="com.guigu.spring.beans.Person">
<property name="name" value="Tom"></proerty>
<property name="car" ref="car2"></proerty>
</bean>

细节3:内部bean,达到和引用的目的。但这个bean不能被外部引用,只能自己内部使用

   <bean id="person" class="com.guigu.spring.beans.Person">
<property name="name" value="Tom"></proerty>
<property name="car">
<bean class="com.guigu.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</proerty>
</bean>

细节4:赋空值: <null/>;或者,直接不写就行。

  <bean id="person" class="com.guigu.spring.beans.Person">
<property name="name" value="Tom"></proerty>
<property name="car">
<bean class="com.guigu.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg><null/></constructor-arg>
</bean>
</proerty>
</bean>

细节5:像struts一样,可以为级联属性赋值。它调用了改属性的set方法,所以类中必须要有改属性的set方法。

   <bean id="person" class="com.guigu.spring.beans.Person">
<constructor-arg
name
="name" value="Tom"></constructor-arg>  <constructor-arg name="car" ref="car2">  <property name="car.maxSpeed" value="230"></property> //   </constructor-arg>  </bean>

注意:和struts不同,不能不初始化直接赋值

     <bean id="person" class="com.guigu.spring.beans.Person">
<property name="name" value="Tom"></proerty>
<!--
<property name="car">
<bean class="com.guigu.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</proerty>
-->
<property name="car.maxSpeed" value="260"></property>
</bean>

这里的意思是,不创建car对象,直接对它的属性赋值。

在struts中可以,会自动先创建一个car对象,然后赋值。二spring不可以,必须先初始化一个对象,再赋值。

细节6:集合属性赋值

一个person可以有多个car,怎么表示?  如下,用节点<list>  。数组list和Set与集合类似。也可以用内部bean

     public class Person{
private String name;
private List<Car> cars;
} public void setName(String name){
this.name=name;
}
public getName(){
return name;
}
public void setCar(List<Car> cars){
this.cars=cars;
}
public List<Car> getCars(){
return cars;
} }
    <bean id="person1" class="com.guigu.spring.beans.Person">
<constructor-arg name="name" value="Tom"></constructor-arg>
<property name="car">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property >
</bean>

注意:集合中Map又不一样,因为它是键值对,用<map>、entry

 public class NewPerson{
private String name;
private Map<String, Car> cars; public String getName(){
return name;
}
public void setName(){
this.name=name;
}
public Map<String, Car> getCars(){
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars=cars;
}
     <bean id="newPerson" class="com.guigu.spring.beans.Person">
<property name="name" value="evan"></property >
<property name="cars">
<map>
<entry key="AA" value-ref="car"/>
<entry key="BB" value-ref="car2"/>
</map>
</property >
</bean>

细节7:配置properties 。 一个DataSource类,xml配置properties。如连接数据库

 public class DataSource{
private Properties properties;
public Properties getProperties(){
return properties;
}
public void setProperties(Properties properties){
this.properties = properties;
}
 <bean id="dataSource" class="com.guigu.spring.beans.DataSource">
<property name="properties">
<props>
<prop key="user">root<prop>
<prop key="password">1234<prop>
<prop key="jdbcUrl">jdbc:mysql://test<prop>
</prop>
<properties>
</bean>
 DataSource datasource = ctx.getBean(DataSource.class);
System.out.print(datasource.getProperties);

输出:

{user=root, password=1234, jdbcUrl=jdbc://test}

细节8:上面集合的配置,如果多个对象要使用,怎么提高内部属性的利用率<props>、<map>。用一个util命名空间

//单独拿出来,可以供多个bean调用
<util:list id="cars">
  <ref bean="car"/>
  <ref bean="car2"/>
</util:list>
//引用
<bean id="person3" class="com.guigu.spring.beans.Person">
<property name="name" value="jack"></property >
<property name="cars" ref="cars"></property >
</bean>

Spring学习记录(二)---容器和bean属性配置的更多相关文章

  1. Spring学习笔记(二)之装配Bean

    一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...

  2. Spring学习系列(二) 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

  3. Spring学习记录(二)

    1.Spring中的AOP思想 aop思想:横向重复,纵向抽取. AOP(Aspect-OrientedProgramming,面向切面编程),AOP包括切面(Aspect),通知(Advice),连 ...

  4. Spring 学习指南 第三章 bean的配置 (未完结)

    第三章 bean 的配置 ​ 在本章中,我们将介绍以下内容: bean 定义的继承: 如何解决 bean 类的构造函数的参数: 如何配置原始类型 (如 int .float 等) .集合类型(如 ja ...

  5. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  6. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  7. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  8. 我的Spring学习记录(四)

    虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...

  9. 我的Spring学习记录(五)

    在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...

随机推荐

  1. 十个免费的web应用安全检测工具

    Websites are getting more and more complex everyday and there are almost no static websites being bu ...

  2. 64位主机64位oracle下装32位客户端ODAC(NFPACS版)

    64位主机64位oracle下装32位客户端ODAC(NFPACS版) by dd 1.下载Oracle Data Access Components(ODAC) Xcopy的两个版本: x86:(我 ...

  3. 【刷题笔记】火车购票-----java方案

    问题描述请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20排.每一排5个座位.为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10号 ...

  4. C#调用C++动态库方法及动态库封装总结

    我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...

  5. Android 学习笔记之一 “Unable to establish loopback connection”

    今天碰到一个错误:Unable to establish loopback connection,在网上找各种方法都解决不了,后来看一个帖子说是要关闭系统防火墙,尝试了下还是不行.最后是进任务管理器杀 ...

  6. 获取文件Md5值

    private static string GetFileMD5(string filePath) { string _md5Value = string.Empty; try { if (Syste ...

  7. javascript 全局对象--w3school

    JavaScript全局对象 1.  decodeURI()解析某个编码的URI. 2.decodeURInComponent()解析一个编码的URI组件. 3.encodeURI()把字符串编码为U ...

  8. 速度极快的导出excel

    public class Export2Excel { #region [导出文件,使用文件流] /// <summary> /// 导出文件,使用文件流.该方法使用的数据源为DataTa ...

  9. 关于UIScrollerView的基本用法和代理

    - (void)viewDidLoad { [super viewDidLoad];  scrollView = [[UIScrollView alloc] initWithFrame:CGRectM ...

  10. DirectX游戏编程(一):创建一个Direct3D程序

    一.环境 Visual Studio 2012,DirectX SDK (June 2010) 二.准备 1.环境变量(如没有配置请添加) 变量名:DXSDK_DIR 变量值:D:\Software\ ...