Bean的简单认识:
在spring配置文件中,使用bean元素来装配一个Bean,它的常用属性有一下九种
1、 id:指它该Bean的唯一标识。
2、 class:指定该Bean的全限定名。
3、 name:为该Bean指定一到多个别名。多个别名可以用空格、”,”或”;”分隔。
4、 autowire:指定该Bean的属性的装配方式(后面会详细介绍)
5、 scope:指定该Bean的存在范围(同上)
6、 init-method:指定该Bean的初始化方法
7、 destroy-method:指定该Bean的销毁方法
8、 abstract:指定该Bean是否为抽象的,如果是抽象的,则spring不在为其创建实例。
9、 parent:指定该Bean的父类标识或别名。
装配Bean的各种类型的属性值
1、 简单类型属性值的装配
Bean中通常有一些简单的类型属性,比如String和int这些简单的类型都可以通过bean元素的子元素property来设置
比如:
<?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-2.5.xsd">
<bean id="hello" class="cn.csdn.service.GreetingServiceImpl" init-method="init">
<property name="say" value="hello"/>
</bean>
<bean id="test" class="cn.csdn.service.Test"></bean>
</beans>
2、 引用其他Bean的装配
如果Bean的某个属性是复杂的类型,既它将引用自其他的Bean。那么在Spring配置文件中可以使用property元素的ref属性来引用
比如:
<?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-2.0.xsd">
<!-- 创建一个DAO的bean id是唯一的标识 class指明类路径 property是bean的属性 -->
<bean id="greetingDAOImpl" class="cn.csdn.dao.GreetingDAOImpl">
<property name="say">
<value>hello</value>
</property>
</bean>
<!-- 创建一个Service的bean ref引用对象-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<property name="greetingDAOImpl" ref="greetingDAOImpl"></property>
</bean>
</beans>
还有一种不常用的方法是在property元素中嵌入一个bean元素来指定所引用的Bean
比如:
<?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-2.0.xsd">
<!-- 创建一个DAO的bean id是唯一的标识 class指明类路径 property是bean的属性 -->
<bean id="greetingDAOImpl" class="cn.csdn.dao.GreetingDAOImpl">
<property name="say">
<value>hello</value>
</property>
</bean>
<!-- 创建一个Service的bean ref引用对象-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<property name="greetingDAOImpl">
<bean class=”cn.csdn.dao.GreetingDAOImpl”></bean>
</property>
</bean>
</beans>
这种装配方式的确定是用户无法在其他地方重用这个内嵌的Bean
未完待续……………………………………(请继续关注GoodWell的东东)
分享到:
相关推荐
写一个简单的bean类 ```java package mybatis.study.start.bean; import lombok.Setter; /** * @program: test * @description: helloworld * @author: cutedog * @create: 2019-02-18 22:48 **/ @Setter ...
第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。 第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring框架...
管理 Bean 从第一个 Spring 应用程式开始,逐步了解何谓依赖注入,以及如何使用 Spring 的容器功能来管理 Bean,了解 Bean 在 Spring 容器中的生命周期。 第一个 Spring 程式 BeanFactory、 ...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。 第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring...
13.3spring中的事务处理 265 13.3.1spring事务处理概述 266 13.3.2编程式事务处理 266 13.3.3声明式事务处理 267 13.3.4标注式事务处理 268 13.4项目实战——公司人事管理 269 本章小结 276 课后练习 276 第...
Struts1只是在第一次请求的时候创建一个action实例,以后每次相同的请求都直接从内存中去读取,它是单例模式,安全性较差。 Struts2是如何实现MVC模式的? 答:在Struts2里面是将每次页面的请求进行处理,然后将请求...
Tomcat是一个免费的开源的Serlvet容器,在Tomcat中,应用程序的部署很简单,你只需将你的WAR放到Tomcat的webapp目录下,Tomcat会自动检测到这个文件,并将其解压。你在浏览器中访问这个应用的Jsp时,通常第一次会很...
3.2 ProcessEngineConfiguration bean ............................................................................................................................... 9 3.3 数据库配置 .....................