`

Bean的配置

 
阅读更多

先说一下两个概念

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的Bean配置Spring的Bean配置说明说明

    Spring的Bean配置

    在本文中,我们将深入探讨Spring中的Bean配置,包括IoC和DI的概念,Bean的配置方式,以及不同类型的IOC容器。 **Spring的Bean配置**: 在Spring中,Bean是应用中的对象,它们由Spring IoC容器负责创建、管理和装配...

    bean配置跑spring security(mysql数据库)_spring security例子

    在这个例子中,我们将探讨如何将Spring Security与MySQL数据库结合使用,通过bean配置来实现用户认证和权限管理。首先,我们需要理解Spring Security的基本架构,它由一系列组件构成,如AuthenticationManager负责...

    spring的Bean配置说明

    以下是对Spring Bean配置的详细解释: 1. `&lt;beans&gt;` 根元素:这是Spring配置文件的起点,它包含了所有的Bean定义。Spring容器会解析这个文件并创建相应的Bean实例。 2. `&lt;bean&gt;` 元素:这是Spring配置中最常见的...

    Spring--2.Spring 中的 Bean 配置-4

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要关注如何通过XML、注解以及Java配置方式来管理Bean。 首先,让我们从XML配置说起...

    EntityBean配置过程.txt

    ------------------EntityBean配置(连接Oracle)------------- 1.One Table 1.1 添加数据源 在: \jboss-4.2.2.GA\docs\examples\jca\oracle-ds.xml,拷贝oracle-ds.xml 到: \jboss-4.2.2.GA\server\all\deploy 配置...

    SPRING:bean配置properties

    SPRING:bean配置properties

    Spring--2.Spring 中的 Bean 配置-3

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要聚焦在第三部分,这通常涉及到更高级和灵活的配置方式。 首先,Spring允许我们...

    Spring--2.Spring 中的 Bean 配置-2-1

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、装配以及管理。本节我们将深入探讨Spring中的Bean配置,主要聚焦在XML配置方式,因为这是Spring早期版本中最常用的方式,尽管在现代Spring...

    Spring--2.Spring 中的 Bean 配置-2-2

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、依赖注入以及生命周期管理。在"Spring--2.Spring 中的 Bean 配置-2-2"这个主题下,我们将深入探讨如何在Spring中进行Bean的详细配置。 1. **...

    Spring--2.Spring 中的 Bean 配置-1

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建、初始化、依赖注入以及生命周期管理。本文将深入探讨Spring中的Bean配置,主要基于标题"Spring--2.Spring 中的 Bean 配置-1"及其相关的上下文。 首先...

    创建SpringBean配置工具类

    创建SpringBean配置工具类(安全)如: &lt;bean id=... scope="prototype"&gt;&lt;/bean&gt;

    spring bean XML配置入门

    在实际开发中,我们可以使用Eclipse的Spring插件来简化Bean配置的创建和管理,同时结合Maven来构建和运行Spring应用。 通过以上内容,你应该对Spring框架中的Bean XML配置有了初步的理解。掌握这些知识点后,你将...

    spring框架的Bean配置文件

    用于在使用spring框架的时候,放在src/java/resource的目录下,对spring的bean进行配置。

    JPA实体bean配置,jpa增删改api,jpasql增删改.doc

    JPA实体bean配置,jpa增删改api,jpasql增删改.doc

    MyBatis-Generator辅助XML生成,dao,bean配置XML生成

    MyBatis-Generator是一款强大的工具,它可以帮助开发者自动化地生成MyBatis相关的XML配置文件,以及对应的Java Bean和DAO类。这个工具极大地提高了开发效率,减少了手动编写这些基础代码的时间,使得开发者能够更加...

    Spring In Action 继承Bean的配置

    在Spring框架中,Bean的继承配置是一个非常关键的概念,它允许我们通过定义一个基础Bean配置,然后由其他Bean继承这个配置,实现配置的复用和扩展。这种方式在大型项目中非常常见,因为它使得代码更加模块化,易于...

    Spring 3.x中三种Bean配置方式比较详解

    在Spring 3.x框架中,有三种主要的Bean配置方式:基于XML的配置、基于注解的配置和基于Java类的配置。每种方式都有其特定的用途和优势,可以根据项目需求选择合适的方式。 首先,**基于XML的配置**是最传统的配置...

Global site tag (gtag.js) - Google Analytics