配置形式:基于xml文件的方式;基于注解的方式

  Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean

  依赖注入的方式:属性注入,构造器注入

一   属性注入

1. Person.java

 public class Person{
String name; public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("hello" + name);
} public Person(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Person[name=" + name + "]";
} }

Main.java

  public class Main {
public static void main(String[] args) {
Person person= new Person();
person.setName("wenxl");
person.hello();
}
}

要调用person.hello(),需要先实例化一个Person对象,而在引入spring后,只需要在ApplicationContext.xml中定义一个bean,则在main方法中,只需要通过ByType或ByName方式,即可获得Person对象。配置如下,其中property属性是为person设置初始值,name为对应的成员变量,value为对应值

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 id="person" class="com.text.Person">
<property name="name" value="wenxl"></property>
</bean> </beans>
 public class Main {
public static void main(String[] args) {
//1. 创建spring的ioc容器对象
//applicationContext代表ioc容器
//ClassPathXmlApplicationContext代表ac接口的实现类,用于加载配置
ApplicationContext atx = new ClassPathXmlApplicationContext("ApplicationContext.xml"); //2. 从ioc容器中获取bean实例 //利用类型返回ioc容器中的bean,但要求ioc容器中只有一个该类型的bean
Person person = atx.getBean(Person.class);
//根据id获取ioc容器中的bean
Person person2 = (Person) atx.getBean("person"); person.hello();
person2.hello();
}
}

二   构造器注入

引入Car类

public class Car {
String name;
int speed;
int price;
public void setName(String name) {
this.name = name;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void setPrice(int price) {
this.price = price;
} public Car(String name, int speed, int price) {
this.name = name;
this.speed = speed;
this.price = price;
}
}

如下,则将按照name,speed,price的顺序,分别设置值Baoma,123,123

 <bean id="car" class="com.text.Car">
<constructor-arg value="Baoma"></constructor-arg>
<constructor-arg value="123"></constructor-arg>
<constructor-arg value="123"></constructor-arg>
</bean>

当然,也可以自行设置顺序

 <bean id="car" class="com.text.Car">
<constructor-arg index="0" value="Baoma"></constructor-arg>
<constructor-arg index="1" value="123"></constructor-arg>
<constructor-arg index="2" value="123"></constructor-arg>
</bean>

还可以采用type来表示对应的数据类型进而相互匹配,如将price改为double类型,则

 <bean id="car" class="com.text.Car">
<constructor-arg type="java.lang.String" value="Baoma"></constructor-arg>
<constructor-arg type="int" value="123"></constructor-arg>
<constructor-arg type="double" value="123"></constructor-arg>
</bean>

当包含<等特殊字符时,可用CDATA包裹。

 <bean id="car" class="com.text.Car">
<constructor-arg type="java.lang.String" >
<value><![CDATA[<audi>]]></value>
</constructor-arg>
<constructor-arg type="int" value="123"></constructor-arg>
<constructor-arg type="double" value="123"></constructor-arg>
</bean>

当需要在bean中引用另外一个bean时,采用ref或内部bean(不可被外部引用),在person类中,增加一个car对象

     <bean id="person" class="com.text.Person">
<property name="name" value="Tom"></property>
<!--
<property name="car">
<ref bean="car" />
</property>
-->
<!--
<property name="car" ref="car"></property>
-->
<property name="car">
<bean class="com.text.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="50"></constructor-arg>
<constructor-arg value="5000" type="double"></constructor-arg>
</bean>
</property>
</bean>

级联属性

     <bean id="person" class="com.text.Person">
<constructor-arg value="lili"></constructor-arg>
<constructor-arg ref="car"></constructor-arg>
<property name="car.price" value="10000"></property>
</bean>

当Person类中新增的为List<car>,此时,property改为

 <property name="car">
<list>
<ref bean="car" />
<ref bean="car2"/>
</list>
</property>

当Person类中新增的为Map<String, car>,则property改为

 <property name="car">
<map>
<entry key="aa" value-ref="car" ></entry>
<entry key="bb" value-ref="car2"></entry>
</map>
</property>

使用props和prop子节点来为properties属性赋值

引入DataSource类

 public class DataSource {
Properties properties; public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} @Override
public String toString() {
return "DataSource [properties=" + properties + "]";
} }
     <bean id="dataSource" class="com.text.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

Spring-bean(一)的更多相关文章

  1. Spring8:一些常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  2. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

  3. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  4. spring bean的生命周期

    掌握好spring bean的生命周期,对spring的扩展大有帮助.  spring bean的生命周期(推荐看)  spring bean的生命周期

  5. spring bean的重新加载

    架构体系 在谈spring bean的重新加载前,首先我们来看看spring ioc容器. spring ioc容器主要功能是完成对bean的创建.依赖注入和管理等功能,而这些功能的实现是有下面几个组 ...

  6. Spring Bean

    一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...

  7. 【转】Spring bean处理——回调函数

    Spring bean处理——回调函数 Spring中定义了三个可以用来对Spring bean或生成bean的BeanFactory进行处理的接口,InitializingBean.BeanPost ...

  8. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

  9. spring bean生命周期管理--转

    Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...

  10. Spring Bean配置默认为单实例 pring Bean生命周期

    Bean默认的是单例的. 如果不想单例需要如下配置:<bean id="user" class="..." scope="singleton&q ...

随机推荐

  1. 从源码理解 ThreadLocal

    为每个线程保存各自的拷贝,可以通过在Thread类中定义一个成员变量来保存每个线程值,这样也是线程安全的. 通过定义一个成员变量 sn 来实现,这里并没有使用ThreadLocal类来实现: publ ...

  2. 包、修饰符、内部类、匿名内部类(java基础知识十)

    1.package关键字的概述及作用 * A:为什么要有包     * 将字节码(.class)进行分类存放  * B:包的概述     *   * C:包的作用     * 包名要定义在第一行,   ...

  3. MYSQL进阶学习笔记十二:MySQL 表分区!(视频序号:进阶_29,30)

    知识点十三:MySQL 表的分区(29) 一.什么要采用分区: 分区的定义: 当数据量过大的时候(通常是指百万级或千万级数据的时候),这时候需要将一张表划分几张表存储.一些查询可以得到极大的优化,这主 ...

  4. [原创]java获取word里面的文本

    需求场景 开发的web办公系统如果需要处理大量的Word文档(比如有成千上万个文档),用户一定提出查找包含某些关键字的文档的需求,这就要求能够读取 word 中的文字内容,而忽略其中的文字样式.表格. ...

  5. ubuntu安装wine+plsql

    1.在ubuntu下装了win7的虚拟机,在使用plsql进行开发的时候发现很慢很卡,经常半天反应不过来.机器是不差的,1w5的thinkstation,实在受不了这种 速度,想着在ubuntu下搞一 ...

  6. SPOJ:PATHETIC STRINGS(分配问题&贪心)

    Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the sa ...

  7. SPOJ:Ada and Graft (set合并&优化)

    As you might already know, Ada the Ladybug is a farmer. She grows a big fruit tree (with root in 0). ...

  8. MongoDB之shard_副本集和分片部署

    机器角色分配和拓扑环境如下: -------------------配置副本集s1-------------------------------1.创建目录在s1h1上创建如下目录[root@node ...

  9. Java AWT组件开发和Swing界面编程

    一.AWT组件开发 1.AWT AWT是抽象窗口工具箱的缩写,它为编写图形用户界面提供了用户接口,通过这个接口就可以继承很多方法,省去了很多工作.AWT还能使应用程序更好地同用户进行交互. AWT中的 ...

  10. margin -------总结(block inline 可置换元素)

    margin在块元素.内联元素中的区别 block元素(块元素)大致有:P|H1|H2|H3|H4|H5|H6|UL|OL|PRE| DL | DIV | NOSCRIPT | BLOCKQUOTE ...