Spring - 初始化bean
来源:互联网 发布:华为p9优化 编辑:程序博客网 时间:2024/05/22 13:48
对于Spring框架来说,控制反转(Inversion of Control)和依赖注入(Dependency injection)是其核心,这里通过一个 bean 初始化的过程来理解一下IoC和DI的概念。
# URL
http://www.springsource.org/download/community
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/
# 依赖 jar
commons-logging-1.1.3.jar
spring-beans-3.2.3.RELEASE.jar
spring-context-3.2.3.RELEASE.jar
spring-core-3.2.3.RELEASE.jar
spring-expression-3.2.3.RELEASE.jar
# 一个最简单的 spring 配置文件
# src/spring.xml<?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.xsd" > <bean id="person" class="org.demo.bean.Person" /> </beans># Java 代码
package org.demo.bean;import java.util.Map;import java.util.Properties;import java.util.Set;public class Person {private boolean propBool;private byte propByte;private short propShort;private int propInt;private long propLong;private float propFloat;private double propDouble;private String propStr;private Set<String> propSet;private Map<String, String> propMap;private Properties props;private String propSystemProp;private String propSystemEnv;/** get set methods */}# 测试代码
package org.demo;import org.demo.bean.Person;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");Person p1 = ctx.getBean(Person.class); // 通过类型来获取 beanPerson p2 = (Person)ctx.getBean("person"); // 通过 id来获取 beanSystem.out.println(p1);System.out.println(p2);}}# 这里获取到的 p1、p2 是同一个实例,因为 spring 容器中的 bean 默认是单例,也就是 singleton,如果需要每次调用 getBean 得到的都是不同的实例,则需要将 bean 的 scope 属性设置成 prototype ,如下:
<bean id="person" class="org.demo.bean.Person" scope="prototype" />
# 设置 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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:property-placeholder /> <bean id="person" class="org.demo.bean.Person"> <property name="propBool" value="true" /> <property name="propByte" value="1" /> <property name="propShort" value="2" /> <property name="propInt" value="3" /> <property name="propLong" value="4" /> <property name="propFloat" value="5.0" /> <property name="propDouble" value="6.2" /> <property name="propStr" value="zhangSan" /> <property name="propSet"> <set> <value>10</value> <value>12</value> </set> </property> <property name="propMap"> <map> <entry key="key1" value="value1" /> <entry key="key2" value="value2" /> </map> </property> <property name="props"> <props> <prop key="propKey1">propValue1</prop> <prop key="propKey2">propValue2</prop> </props> </property> <property name="propSystemProp" value="${java.io.tmpdir}" /> <property name="propSystemEnv" value="${JAVA_HOME}" /> </bean> </beans># 注意:通过 ${propertyName} 这种方式不仅可以引用JVM的系统属性(在JVM启动的时候通过参数 -DpropertyName=propertyValue 进行设置),还可以引用操作系统的环境变量(例如 windows 中的 系统属性 -> 高级 -> 环境变量 -> 系统变量)。使用 ${...} 时需要配置有 <context:property-placeholder />,否则${...} 将当成普通字符串进行处理。
# 注入依赖对象
# 方案一(通过<property> 元素来注入依赖对象)
package org.demo.service;import org.demo.bean.Person;public class PersonService {private Person person;/** get set methods */}
<?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.xsd" > <bean id="person" class="org.demo.bean.Person"> <property name="propStr" value="zhangSan" /> </bean> <bean id="personService" class="org.demo.service.PersonService" > <property name="person" ref="person" /> </bean> </beans># 方案二(通过@Autowired注解来注入依赖对象)
package org.demo.service;import org.demo.bean.Person;import org.springframework.beans.factory.annotation.Autowired;public class PersonService { @Autowiredprivate Person person; /** get set methods */}
<?xml version="1.0" encoding="UTF-8"?><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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <!-- 这里表示启用注解,例如: --> <!-- @Repository、@Service、@Controller、@Component、@Autowired、@Resource --> <context:annotation-config /> <bean id="person" class="org.demo.bean.Person"> <property name="propStr" value="zhangSan" /> </bean> <bean id="personService" class="org.demo.service.PersonService" /> </beans># 方案三(通过@Resource注解来注入依赖对象)
@Resourceprivate Person person;# 注入 ApplicationContext
# 方案一(通过 @Autowired/@Resource 注解来注入)
@Resource// @Autowiredprivate ApplicationContext ctx;# 方案二(通过实现接口 ApplicationContextAware 来注入)
package org.demo.service;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class PersonService implements ApplicationContextAware { private ApplicationContext ctx; @Overridepublic void setApplicationContext(ApplicationContext pCtx)throws BeansException {this.ctx = pCtx;}/** get set methods */}
其他 Aware 接口请参阅:http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#aware-list
# bean 的生命周期 - 在初始化 bean 的过程中执行一段自定义的代码
# 方案一(通过@PostConstruct 注解来实现)
@PostConstructpublic void postConstruct() {System.out.println("-- postConstruct --");}# 方案二(通过接口 InitializingBean 来实现)
public class PersonService implements InitializingBean{ @Overridepublic void afterPropertiesSet() throws Exception {System.out.println("-- afterPropertiesSet --");}}# 方案三(通过在 spring 配置文件中指定一个初始化方法来实现)
<bean id="personService" class="org.demo.service.PersonService" init-method="init" />或者
<?xml version="1.0" encoding="UTF-8"?><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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-init-method="init2"> <!-- ... --></beans>
# bean 的生命周期 - 在销毁 bean 之前执行一段自定义的代码
# 与初始化过程中对应的依次为:@PreDestroy注解、DisposableBean接口、destroy-method/default-destroy-method
# bean 的生命周期 - SmartLifecycle
package org.demo.service;import org.springframework.context.SmartLifecycle;public class PersonService implements SmartLifecycle { private boolean isRunning = false;@Overridepublic boolean isAutoStartup() {System.out.println("-- isAutoStartup --");return true;}@Overridepublic int getPhase() {System.out.println("-- getPhase --");return -1;}@Overridepublic boolean isRunning() {System.out.println("-- isRunning " + isRunning + " --");return isRunning;} @Overridepublic void start() {isRunning = true;System.out.println("-- start --");}@Overridepublic void stop(Runnable stopCallback) {System.out.println("-- stop runnable --");stopCallback.run();isRunning = false;} @Overridepublic void stop() {System.out.println("-- stop --");}}详细信息请参阅:http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-lifecycle-processor
END......
- Spring Bean 初始化过程
- Spring - 初始化bean
- Spring Bean 初始化过程
- Spring Bean 初始化过程
- spring bean 初始化
- spring手动初始化bean
- spring初始化bean InitializingBean
- Spring Bean 初始化过程
- Spring Bean 初始化过程
- spring bean初始化
- Spring Bean初始化过程
- Spring-Bean初始化顺序
- spring bean初始化时机
- Spring Bean初始化过程
- spring bean 初始化逻辑
- Spring初始化Bean方法
- Spring何时初始化bean
- spring初始化bean InitializingBean
- MyEclipse编写ExtJS卡死问题解决方法
- SDUT2143 图结构练习——最短路径
- 半平面交
- How specialist ECT consultants inform patients about memory loss
- Linux 多重操作系统学习
- Spring - 初始化bean
- MySQL 导入Excel
- 介绍 SMS over SGs
- WIN7下使用无线网卡共享网络(利用笔记本无线网卡做无线AP)
- 分享一款开源的http工具
- UNIX / Linux 50个最常用命令(含例子)
- 几个典型的蓝屏故障的原因和解决办法
- 资深HR给应届生的黄金面试技巧
- 栈的基本操作-指针