Spring 学习笔记(一)
2015-08-13 09:12
597 查看
一、创建 bean 的四种方式
1. 调用类的默认构造方法
二、bean 的作用域 scope
三、初始化和销毁 bean (init-method、destory-method)
也可以为应用上下文中所有的 bean 定义默认的初始化和销毁方法
四、注入 bean 的属性
1. 属性注入(通过 setter 方法注入)
2. 装配集合 list、set、array 包括 <value /> <ref /> <null />
3. 装配 map 集合
4. 装配 properties 集合(java.util.Properties)
5. 装配 null 值
6. 使用 Spring 命名空间 P 简化书写:
7. 使用 SpEL 表达式装配
<!-- 引用其它 bean -->
<!-- 操作类 -->
1. 调用类的默认构造方法
<bean id = "myBean" class = "com.study.spring.MyBean" />2. 通过构造函数注入
<bean id = "myBean" class = "com.study.spring.MyBean" > <constructor-arg value = "myParam" /> </bean>3. 通过构造函数注入对象的引用
<bean id = "myBean" class = "com.study.spring.MyBean" /> <bean id = "myBean2" class = "com.study.spring.MyBean2" > <constructor-arg ref = "myBean" /> </bean>4. 通过工厂方法创建 bean
<bean id = "myBean" class = "com.study.spring.MyBean" factory-method = "myFactoryMethod" />
二、bean 的作用域 scope
<bean id = "myBean" class = "com.study.spring.MyBean" scope = "prototype" />
三、初始化和销毁 bean (init-method、destory-method)
<bean id = "myBean2" class = "com.study.spring.MyBean2" init-method = "method_name" destory-method = "method-name" />
也可以为应用上下文中所有的 bean 定义默认的初始化和销毁方法
四、注入 bean 的属性
1. 属性注入(通过 setter 方法注入)
<bean id = "myBean2" class = "com.study.spring.MyBean2" > <property name = "simpleType" value = "simpleValue" /> <!-- 简单属性注入,Spring 会自动判断和转化简单属性的类型 --> <property name = "myBean" ref = "myBean" /> <!-- 引用其它 bean 属性的注入 --> <property name = "orther"> <bean class = "com.study.spring.other" /> <!-- 内部 bean 的注入 --> </property> <constructor-arg> <bean class = "com.study.spring.other" /> <!-- 也可以使用构造方法注入 --> </constructor-arg> </bean>
2. 装配集合 list、set、array 包括 <value /> <ref /> <null />
3. 装配 map 集合
4. 装配 properties 集合(java.util.Properties)
5. 装配 null 值
6. 使用 Spring 命名空间 P 简化书写:
<bean id = "user" class = "com.study.spring.User" > p:userName = "goddess" p:otherBean-ref = "otherBean" <!-- 类型引用比简单属性多了 "-ref" --> </bean>
7. 使用 SpEL 表达式装配
<!-- 装配简单属性 --> <property name = "inttype" value = "#{23}" /> <property name = "scientific" value = "#{1e4}" /> <property name = "floatType" value = "#{11.1}" /> <property name = "stringtype" value = "#{'goddess'}" /> <property name = "booleantype" value = "#{true}" />
<!-- 引用其它 bean -->
<!-- 操作类 -->
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- java Spring整合Freemarker的详细步骤
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java当中的定时器的4种使用方式
- java中 spring 定时任务 实现代码