首页 > 学院 > 开发设计 > 正文

9、spring的bean基础(1)

2019-11-08 02:28:39
字体:
来源:转载
供稿:网友

9、sPRing的bean基础(1)

本文主要讲解以下几个知识点

1、在spring中引用bean的例子2、注入值到bean属性3、加载多个配置文件4、spring 内部bean的示例5、spring bean的作用域

1、在spring中引用bean的例子

引用同一个配置文件下的bean

<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-2.5.xsd"> <bean id="HelloWorld" class="..."> <property name="outputGenerator" > <ref local="Custom"/> </property> </bean> <bean id="Custom" class="..." /></beans>

引用其他配置文件的bean

<!-- first.xml --> <bean id="HelloWorld" class="..."> <property name="outputGenerator" > <ref bean="Custom"/> </property> </bean> <!-- second.xml --> <bean id="Custom" class="..."/>

2、注入值到bean属性

假设现在有一个bean类如下所示:

package com.main;public class HelloWorld{ private String name; private int state; //setter and getter methods //toString methods}

在Spring框架中有三种方式可以注入值到bean属性 第一种:normal方式

<bean id="HelloWorld" class="com.main"> <property name="name"> <value>jack</value> </property> <property name="state"> <value>1</value> </property> </bean>

第二种:快捷方式

<bean id="HelloWorld" class="com.main"> <property name="name" value="jack"/> <property name="state" value="1"/> </bean>

第三种:P标签方式

注意:此时如果要使用p标签,必须要在spring xml配置文件中声明 xmlns:p=”http://www.springframework.org/schema/p”

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloWorld" class="com.main" p:name="jack" p:type="1" /></beans>

3、spring加载多个配置文件

背景说明:在实际开发的项目中,bean配置文件非常多,因此需要一个配置文件仓库来统一管理和使用某个bean配置文件,将会减少很多查找工作。

假设项目中存在以下两个bean配置文件 first.xml(路径为classpath:/com/main/first/first.xml)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="FirstBean" class="com.main.first" /></beans>

second.xml(路径为classpath:/com/main/second/second.xml)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="SecondBean" class="com.main.first" /></beans>

把上述两个bean配置文件组织在一个xml文件中,暂且命名为Spring-Module.xml

那么Spring-Module.xml的配置如下:

<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-2.5.xsd"> <import resource="com/main/first/first.xml"/> <import resource="com/main/second/second.xml"/></beans>

至此就完成了加载多个配置文件的工作; 从Spring-Module.xml中获取bean的方法如下

applicationContext context = new ClassPathXmlApplicationContext(Spring-Module.xml);Object object = (Object)context.getBean("value");

4、内部bean实例

说明:在一些场景中,我们或许需要为某一个bean的属性设定一个内部bean(即只允许自己使用),通常有以下两种方式可以实现;

第一种:通过setter方法构造内部bean

<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-2.5.xsd"> <bean id="Customer" class="..."> <property name="person"> <!--声明内部bean --> <bean class="..."> <!-- 内部bean的属性 --> <property name="name" value="yiibai" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean></beans>

第二种:通过构造方法构造内部bean

<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-2.5.xsd"> <bean id="Customer" class="..."> <constructor-arg> <!--声明内部bean --> <bean class="..."> <!-- 内部bean的属性 --> <property name="name" value="yiibai" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean></beans>

5、spring bean的作用域

spring bean的作用域的作用就在于:告诉spring容器应该选择哪一种类型的bean实例返回给调用者。支持的作用域有以下五种;

单例-每次都返回同一个bean实例原型-每一次都返回一个新的实例请求-返回每个HTTP请求的一个Bean实例会话-返回每个HTTP会话的一个bean实例全局会话-返回全局HTTP会话的一个bean实例

单例(在bean中不设置scope属性)

<bean id="normalBean" class="..."/>

原型

<bean id="normalBean" class="..." scope="prototype" />

使用注解来使用作用域

package com.main;@Service@Scope("prototype")public class{ //property //methods}

要使用注解形式的作用域,必须要启用组件扫描

<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.main" /></beans>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表