4.0.0
com.scu.tgm
dubbo
war
1.0
dubbo Maven Webapp
http://maven.apache.org
dubbo
4.1.6.RELEASE
junit
junit
4.12
test
org.springframework
spring-test
${spring.version}
org.springframework
spring-webmvc
${spring.version}
Archetype Created Web Application
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springContext.xml
1
dispatcher
/
org.springframework.web.context.ContextLoaderListener
/WEB-INF/views/
.jsp
package com.scu.tgm.dubbo.entity;
public class User {
private String name;
private Integer age;
public void init() {
age=24;
}
public void say() {
System.out.println("---");
System.out.println(this.toString());
}
省去了setter和getter
}
Springmvc执行流程:
1.用户发送一个请求到DispatcherServlet;
2.通过HanderMapping对象去查找请求地址对应的Handler(Handler是HandlerExecutionChain对,HandlerExecutionChain=自己的Controller+N个拦截器interceptor)
3.如果handler处理器存在,那么先会去查找能执行这个控制器对应HanderAdpter(处理适配器)
4.先执行对应拦截器,并且拦截器是放行的;
5.通过HanderAdpter(处理适配器)去调用我们控制器代码;并且接收ModelAndView
6.通过视图解析器,根据我们提供的视图名,创建对应视图对象(forward:/WEB-INF/_01_hello.jsp,redirect:/WEB-INF/_01_hello.jsp) velocity
7.通过视图和模型进行合成内容;
8.返回结果给客户端;
package dubbo;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.scu.tgm.dubbo.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:springContext.xml" })
public class SpringTestDemo {
@Autowired
BeanFactory beanFactory2;
@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//spring管理bean是使用的反射方式,依赖注入IOC,与对象的构造方法无关因为获取的是DeclaredConstructor
Class userClass=Class.forName("com.scu.tgm.dubbo.entity.User");
Constructor declaredConstructor = userClass.getDeclaredConstructor();
User user=(User)declaredConstructor.newInstance();
//这个是手动获取resource,与@Autowired一样
Resource resource=new ClassPathResource("springContext.xml");
BeanFactory beanFactory=new XmlBeanFactory(resource);
//一般我们使用ApplicationContext,因为BeanFactory只提供管理bean功能,ApplicationContext继承BeanFactory并且
//丰富了它的功能,使用ClassPathXmlApplicationContext来获取该对象
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("springContext.xml");
User userBean3 = (User)applicationContext.getBean("user",User.class);
User userBean2 = (User)beanFactory2.getBean("user",User.class);
User userBean = (User)beanFactory.getBean("user",User.class);
userBean.say();
userBean2.say();
userBean3.say();
}
}
测试的时候遇到一个错误:
@RunWith(SpringJUnit4ClassRunner.class)报错找不到SpringJUnit4ClassRunner.class,需要导入的包:
导入后依然报错,需要手动导入import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;