先说一下两个概念
IoC:
DI:
ApplicationContext的实现类型有两种
1)ClassPathXmlApplicationContext:从类路径下加载配置文件
2)FileSystemXmlApplicationContext:从文件系统中加载配置文件
ApplicationContext初始化上下文是就就实例化所有单例的bean
从IoC容器中获取bean的实例
//从IoC容器中获取bean HelloWorld helloWorld = (HelloWorld) cxt.getBean("helloWorld"); //缺点:当容器中配置多个类的实例,此种方法获取bean报异常 HelloWorld helloWorld = cxt.getBean(HelloWorld.class);
bean的注入有三种
1)属性setter方式注入
<!-- 配置Bean class: bean的全类名,可以通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无惨构造函数 id:在IoC容器中唯一,不存在则使用全类名做标记(com.hous.spring.HelloWorld) --> <bean id="helloWorld" class="com.hous.spring.HelloWorld"> <property name="name" value="shanshanbox.com"></property> </bean>
2)构造器的方式注入
<!-- 通过构造器方式配置bean --> <bean id="car" class="com.hous.spring.Car"> <constructor-arg value="pasate" index="0"></constructor-arg> <constructor-arg value="shanghai" index="1"></constructor-arg> <constructor-arg value="300000" type="double"></constructor-arg> </bean> <!-- 可以通过参数位置和参数类型,区分重载的构造器 --> <bean id="car2" class="com.hous.spring.Car"> <constructor-arg value="pasate" index="0"></constructor-arg> <constructor-arg value="shanghai" type="java.lang.String"></constructor-arg> <constructor-arg value="200" type="int"></constructor-arg> </bean>
注意:字符串表示的值可以通过<value>元素标签或者value属性进行注入
基本类型及其封装类,String等类型都可以使用字面值注入
若字面值中包含特殊字符串,可以使用<![CDATA[]>把字面值包裹起来
<!-- 可以通过参数位置和参数类型,区分重载的构造器 --> <bean id="car2" class="com.hous.spring.Car"> <constructor-arg value="pasate" index="0"></constructor-arg> <!-- 含有特殊字符可以使用<![CDATA[]]>包裹起来 --> <constructor-arg type="java.lang.String"> <value><![CDATA[<shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>280</value> </constructor-arg> </bean>
在IoC容器中,关于bean之间的引用,可以使用ref属性或者<ref>标签
<bean id="person" class="com.hous.spring.Person"> <property name="name" value="hous"></property> <property name="age" value="25"></property> <!-- 使用bean的ref属性或标签建立bean之间的引用关系 --> <!-- <property name="car" ref="car"></property> --> <!-- <property name="car"> <ref bean="car" /> </property> --> <!-- 内部bean不能被外部bean引用 --> <property name="car"> <bean class="com.hous.spring.Car"> <constructor-arg value="mazida" index="0"></constructor-arg> <constructor-arg value="changan" index="1"></constructor-arg> <constructor-arg value="300000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="com.hous.spring.Person"> <constructor-arg value="zhangsan"></constructor-arg> <constructor-arg value="28"></constructor-arg> <constructor-arg ref="car"></constructor-arg> </bean>
在IoC容器中可以直接使用<null />赋值,或者使用car.price=300级联赋值
注意:级联赋值需要对bean对象初始化,后才可以使用级联赋值
关于集合的注入
Spring中可以通过内置的xml标签,如(<list>或<set>或<map>等配置集合属性)
在内部可以使用value指定简单值,使用ref引用别的bean
java.util.Map可以使用<map>标签,内部可以有多个<entry>做为子标签,每个<entry>有一个key和一个value值,可以将Map的键和值做为<entry>属性定义,简单常量使用key和value,bean的引用可以通过key-ref和value-ref引用
java.util.Properties可以使用<props>标签定义,内部存在多个<prop>做为子标签,必须定义key属性
1)使用List集合
<bean id="person" class="com.hous.spring.Person"> <property name="name" value="hous"></property> <property name="age" value="25"></property> <property name="cars"> <list> <ref bean="car" /> <ref bean="car2" /> </list> </property> </bean>
package com.hous.spring; import java.util.List; public class Person { private String name; private int age; private List<Car> cars; public String getName() { return name; } public Person() { super(); } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]"; } }
<bean id="person" class="com.hous.spring.Person"> <property name="name" value="shanshanbox"></property> <property name="age" value="25"></property> <property name="cars"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean>
<bean id="dataSource" class="com.hous.spring.DataSource"> <property name="properties"> <props> <prop key="driverClass">com.mysql.jdbc.Driver</prop> <prop key="url">jdbc:mysql://test</prop> <prop key="user">root</prop> <prop key="pass">root</prop> </props> </property> </bean>
配置独立的集合bean供多个bean使用,还有可以使用p命名空间赋值
<!-- 配置单例的集合bean供多个bean使用,需要引入util命名空间 --> <util:list id="cars"> <ref bean="car"/> <ref bean="car2"/> </util:list> <bean id="person" class="com.hous.spring.Person"> <property name="name" value="shanshanbox"></property> <property name="age" value="25"></property> <property name="cars" ref="cars"></property> </bean> <!-- 通过p命名空间为bean的属性赋值,需要引用p命名空间 --> <bean id="person2" class="com.hous.spring.Person" p:name="box" p:age="28" p:cars-ref="cars" />
好了,就这么多。来一首根羊的小诗送个大家,呵呵
黑夜给了我一双黑色的眼睛,我要用它来寻找光明,
即使在那遥远的地方,也有驼铃的响起,
我的思念飘向远方的故乡。
相关推荐
### Spring的Bean配置详解 #### 一、Spring配置文件根元素`<beans>`解析 - **功能概述**:`<beans>`是Spring配置文件的根元素,用来包含一个或多个`<bean>`元素,用于定义Spring容器管理的各种Bean。 #### 二、`...
在本文中,我们将深入探讨Spring中的Bean配置,包括IoC和DI的概念,Bean的配置方式,以及不同类型的IOC容器。 **Spring的Bean配置**: 在Spring中,Bean是应用中的对象,它们由Spring IoC容器负责创建、管理和装配...
本文将深入解析如何利用`PropertyPlaceholderConfigurer`进行bean配置的properties操作,包括单个properties文件的配置、多个properties文件的集成以及多个`PropertyPlaceholderConfigurer`的协同工作,旨在帮助...
在这个例子中,我们将探讨如何将Spring Security与MySQL数据库结合使用,通过bean配置来实现用户认证和权限管理。首先,我们需要理解Spring Security的基本架构,它由一系列组件构成,如AuthenticationManager负责...
以下是对Spring Bean配置的详细解释: 1. `<beans>` 根元素:这是Spring配置文件的起点,它包含了所有的Bean定义。Spring容器会解析这个文件并创建相应的Bean实例。 2. `<bean>` 元素:这是Spring配置中最常见的...
在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要关注如何通过XML、注解以及Java配置方式来管理Bean。 首先,让我们从XML配置说起...
在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要聚焦在第三部分,这通常涉及到更高级和灵活的配置方式。 首先,Spring允许我们...
在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要聚焦在XML配置方式,因为这是Spring早期版本中最常用的方式,尽管在现代Spring...
在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、依赖注入以及生命周期管理。在"Spring--2.Spring 中的 Bean 配置-2-2"这个主题下,我们将深入探讨如何在Spring中进行Bean的详细配置。 1. **...
在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、依赖注入以及生命周期管理。本文将深入探讨Spring中的Bean配置,主要基于标题"Spring--2.Spring 中的 Bean 配置-1"及其相关的上下文。 首先...
创建SpringBean配置工具类(安全)如: <bean id=... scope="prototype"></bean>
在实际开发中,我们可以使用Eclipse的Spring插件来简化Bean配置的创建和管理,同时结合Maven来构建和运行Spring应用。 通过以上内容,你应该对Spring框架中的Bean XML配置有了初步的理解。掌握这些知识点后,你将...
用于在使用spring框架的时候,放在src/java/resource的目录下,对spring的bean进行配置。
现在,考虑到压缩包文件名称`spring_02_bean`,这可能是一个关于Spring Bean配置的代码示例或教程。在这样的资源中,你可能会找到如何在XML配置文件中声明Bean、设置别名以及指定作用范围的实际示例代码。通过阅读和...
MyBatis-Generator是一款强大的工具,它可以帮助开发者自动化地生成MyBatis相关的XML配置文件,以及对应的Java Bean和DAO类。这个工具极大地提高了开发效率,减少了手动编写这些基础代码的时间,使得开发者能够更加...
在Spring框架中,Bean的继承配置是一个非常关键的概念,它允许我们通过定义一个基础Bean配置,然后由其他Bean继承这个配置,实现配置的复用和扩展。这种方式在大型项目中非常常见,因为它使得代码更加模块化,易于...
04-手写Bean配置-spring-v41.rar可能包含了一个简单的实现,你可以下载并研究,以更直观地了解Bean定义配置化的实现方式。 在实际开发中,Bean定义的配置化不仅提高了代码的可维护性和测试性,还使我们能够轻松地...
在Spring 3.x框架中,有三种主要的Bean配置方式:基于XML的配置、基于注解的配置和基于Java类的配置。每种方式都有其特定的用途和优势,可以根据项目需求选择合适的方式。 首先,**基于XML的配置**是最传统的配置...