我是Spring Framework的新手。请指导我对Spring的基本了解。我了解Java和JSF,但我对Struts或其他框架一无所知。我已经搜索了互联网并且能够下载PDF,但我不理解Spring流程,就像我理解JSF流程一样。请为我提供一个简单的方法来了解Spring的流程。
答案 0 :(得分:9)
答案 1 :(得分:3)
我也是Spring Framework的新手。 到目前为止,以下文档是最基本的文档。我也正在读它,希望它可以帮到你。
答案 2 :(得分:2)
我也是Spring的新手,不久之前有类似的问题。首先,我想向你推荐Craig Walls的“Spring in Action”一书,我发现它非常有用且易于理解, http://www.tutorialspoint.com/spring/帮助我解决了很多问题。如果我理解你的问题,那么我们可以将“Spring的流程”划分为Spring IoC容器和Spring bean的生命周期。这里是关于Spring bean生命周期的exapmle的非常小的概述。一个bean在Spring中创建和销毁之间经历了几个步骤 容器。这些步骤是:
每个步骤都为自定义提供了自己的机会。这里有一些代码只是“跟踪”bean的生活:
对于bean ClassA:
public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}
对于CustomPostProcessor:
public class CustomPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: beforeInitialization on: "
+ beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: afterInitialization on: "
+ beanName);
return bean;
}
}
在主类中我们创建ApplicationContext,获取bean并打印消息:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"META_INF/spring/app-context.xml");
ClassA objA = (ClassA) context.getBean("classA");
System.out.println(objA.getMessageA());
context.registerShutdownHook();
}
在app-context.xml中我们有:
<bean id="classA" class="ClassA" init-method="customInitMethod"
destroy-method="customDestroyMethod">
<property name="messageA" value="messagA: Hello Spring!" />
</bean>
<bean class="CustomPostProcessor" />
据我所知,输出行以这种方式对应于生命周期阶段:
ClassA:默认构造函数。
ClassA:名为的消息集方法。
ClassA:bean name set:classA
ClassA:beanFacrory set。
ClassA:应用程序上下文集:
CustomPostProcessor:beforeInitialization on:classA
ClassA:调用afterPropertiesSet()因为InitializingBean接口。
ClassA:调用customInitMethod()方法。
CustomPostProcessor:afterInitialization on:classA
ClassA:调用消息get方法。 messagA:你好春天!
ClassA:destroy()因为DisposableBean接口而调用。
ClassA:调用customDestroyMethod()方法。