Spring

单Spring框架的请求流程:客户端发起请求--web.xml根据请求路径初始化对应的Servlet--执行service()方法--返回响应。流程就是上篇讲的Servlet的请求流程。

Spring与Servlet多一个applicationContext-*.xml的配置,也是因为多了这个配置文件使项目管理javaBean变得简单方便。

中间件启动时需要将加载spring Ioc的配置,通过在web.xml中添加以下配置实现

<!-- spring配置开始 -->
		<context-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-*.xml</param-value>
		</context-param>
		<listener>
			<listener-class>
				org.springframework.web.context.ContextLoaderListener
			</listener-class>
		</listener>
		<!-- spring配置
			classpath指向的路径是项目部署后的WEB-INF下的classes和lib目录
			使用classpath和classpath*的区别:前者只会去classpath路径下查找,后者还会
                        去jar中的class路径中查找,相对前者查找范围广,耗资源,有多个相同文件是会被覆盖
			/applicationContext-*.xml 表示任意路径下的以applicationContext-开头的文件
		 -->

加载配置applicationContext-*.xml的底层实现

public class ContextLoaderListener extends ContextLoader implements ServletContextListener
{
  private ContextLoader contextLoader;
  // ServletContext初始化时执行
  public void contextInitialized(ServletContextEvent event)
  {
    this.contextLoader = createContextLoader();
    if (this.contextLoader == null) {
      this.contextLoader = this;
    }
    this.contextLoader.initWebApplicationContext(event.getServletContext());
  }
  
}
public class ContextLoader
{
  public static final String CONTEXT_CLASS_PARAM = "contextClass";
    // 这就是 <param-name>contextConfigLocation</param-name> 的原因
  public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
  public static final String LOCATOR_FACTORY_SELECTOR_PARAM = "locatorFactorySelector";
  public static final String LOCATOR_FACTORY_KEY_PARAM = "parentContextKey";
  private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";
  private static final Properties defaultStrategies;
  public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
  {
    if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
      throw new IllegalStateException(
        "Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!");
    }
    Log logger = LogFactory.getLog(ContextLoader.class);
    servletContext.log("Initializing Spring root WebApplicationContext");
    if (logger.isInfoEnabled()) {
      logger.info("Root WebApplicationContext: initialization started");
    }
    ...
    ...
    ...
  }   
}

再说applicationContext-*.xml的配置内容,spring的核心是IOC和aop,IOC管理众多的bean的属性注入,aop实现横向切面编程

<!-- 开启注解注入,用于激活**已经在spring容器中注册了过的bean**上的注解,相当于在容器中注册以下注解的处理器
	@Autowired
	<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
	@Resource 、@ PostConstruct、@ PreDestroy
	<bean class="org.springframework.beans.factory.annotation. CommonAnnotationBeanPostProcessor"/> 
	@PersistenceContext
	<bean class="org.springframework.beans.factory.annotation.PersistenceAnnotationBeanPostProcessor"/>
	@Required
	<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
	以上注解处理器还处理不了@Component、@Controller、@Service等这些注解,需要使用<context:component-scan base-package=”XX.XX”/> 
	<context:component-scan base-package=”XX.XX”/> 除了拥有 <context:annotation-config/>的功能外,还能激活@Component、@Controller、@Service等这些注解,
	原因是以为其中增加了这些额外注解的注解处理器BeanPostProcessor,当两者都存在时,<context:annotation-config/>会被忽略,
	以<context开头,处理容器的注解,< mvc:主要针对Spring MVC来使用的
	 -->
	<context:annotation-config/>
	
	<!-- 读取配置文件,将属性值放入另外一个标准的java Proerties文件中,xml文件中使用时用${key}占位取值 
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		 在检查特定的配置文件之前先检查系统配置文件 
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"></property>
		 忽略未解析到的占位符 
		<property name="ignoreUnresolvablePlaceholders" value="true"></property>
		<property name="fileEncoding" value="utf-8"></property>
		<property name="locations">
			<list>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>-->
	
	<!-- 以上加载配置文件的简化方式 
	<context:property-placeholder location="classpath:db.properties"  ignore-unresolvable="true"/>-->
	<!-- 加载多个配置文件 -->
	<context:property-placeholder location="classpath:*db.properties"  ignore-unresolvable="true" ignore-resource-not-found="false"/>
	
	<!-- 数据库连接 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver.name}"></property>
		<property name="url" value="${db.url}"></property>
		<property name="username" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
		 <!--initialSize: 初始化连接-->    
        <property name="initialSize" value="5"/> 
         <!--maxIdle: 最大空闲连接-->    
        <property name="maxIdle" value="20"/>
        <!--minIdle: 最小空闲连接-->    
        <property name="minIdle" value="5"/> 
        <!--maxActive: 最大连接数量-->    
        <property name="maxActive" value="200"/>
        <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒-->  
        <property name="maxWait" value="3000"/>
	</bean>
	
	<bean id="xzqhService" class="com.muchen.web.XzqhService">
	</bean>
    <bean id="xzqhService2" class="com.muchen.web.XzqhService2">
	</bean>
	
	<!--  proxy-target-class="false",默认只能代理接口(jdk动态代理),为true时才能代理目标类(CGLib动态代理),
	spring使用的是两者结合的方式,当目标类没有接口时使用cglib动态代理,
	expose-proxy="true":编程中通过AopContext类进行检索获得某代理 -->
	<aop:aspectj-autoproxy proxy-target-class="false"  expose-proxy="true"></aop:aspectj-autoproxy>
        <!-- 注册事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启事务注解,注册 TransactionalEventListenerFactory。属性transaction-manager指定使用哪个事务管理器来管理事务。
	spring是采用动态代理的方式实现对bean的管理和切片,动态代理过程中通过拦截方式(比如对@Transactional的拦截),
	在拦截后加入目标逻辑,也就是aop横切,然后生成代理对象(织入weaving,织入后编译的.class文件反编译后代码相比编译前的要多,也就是得到了增强),
	通过代理对象之间的调用才能触发切面。
	spring中基本使用声明式的方式对事务进行管理,也就是利用aop横切,所以必须使用代理才能触发切面(如:事务)。 
@Transactional在类上表明该类所有方法开启事务,开启事务的方法必须使用public修饰-->
	<tx:annotation-driven transaction-manager="transactionManager"/>

请求根据web.xml配置的路径对应到相应的servlet中

public class MainServlet extends HttpServlet{
	
	public void doGet(HttpServletRequest request, HttpServletResponse response){
		String msg = request.getParameter("msg");
		try {
			StringBuffer buffer = new StringBuffer("");
            // 从容器获取bean实例
			ApplicationContext ac = new  ClassPathXmlApplicationContext("applicationContext-base.xml");
			XzqhService x = (XzqhService) ac.getBean("xzqhService");
			List<Map<String, Object>> list = x.queryXzqh(msg);
			for(Map<String, Object> map : list){				
				buffer.append(map.get("QHMC")+"\n");
			}
			response.getWriter().write(buffer.toString());
		} catch (IOException e) { 
			e.printStackTrace();			
		}
	}

}

然后从数据层获取所需数据

public class XzqhService {
	private SimpleJdbcTemplate jdbcTemplate;	
	private DataSource dataSource;
	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		// dataSource属性是属于SimpleJdbcTemplate的父类的,而且SimpleJdbcTemplate不止一个构造方法
		// 所以不方便在applicationContext.xml中以构造器和属性注入
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}	
	public List<Xzqh> queryXzqh(String msg){		
		String sql = "SELECT * FROM XZQH where qhjb=?";
//		return jdbcTemplate.queryForList(sql, msg);
		/* jdbcTemplate的增删改查无非都是对底层Statement等的封装操作
		 * BeanPropertyRowMapper:将jdbcTemplate查询的结果集ResultSet转化到实体对象上
		 * 转化规则:将实体对象属性名称转变:NAME-->name,myName-->my_name,总之实体对象属性名称转化后能与结果集中的字段
		 * 名称一样就能给此属性赋值,因此java属性命名规范驼峰法,数据库字段命名下划线 _ 如:MY_NAME
		 */		
		BeanPropertyRowMapper<Xzqh> xzqhRowMapper = new BeanPropertyRowMapper<Xzqh>(Xzqh.class);
		return jdbcTemplate.query(sql, xzqhRowMapper, msg);
	}
    	/*事务的传播特性
	 *  (1)propagation_requierd:如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,这是Spring默认的选择。
	 *	(2)propagation_required_new:新建事务,如果当前存在事务,把当前事务挂起。
	 *	(3)propagation_nested:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与propagation_required类似的操作。
	 *		子事务propagation_nested 相互独立,母事务回滚,子事务回滚,子事务回滚,母事务不一定回滚。
	 *	(4)propagation_supports:支持当前事务,如果没有当前事务,就以非事务方法执行。
	 *	(5)propagation_not_supported:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
	 *	(6)propagation_mandatory:使用当前事务,如果没有当前事务,就抛出异常。
	 *	(7)propagation_never:以非事务方式执行操作,如果当前事务存在则抛出异常。
	 */
//	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
	public void m1(){
		jdbcTemplate.update("update XZQH set qhjb=? where qhbm='3405042002151' ", "1111111");
		// 获得当前对象代理方式一
		ApplicationContext ac = new  ClassPathXmlApplicationContext("applicationContext-base.xml");
		XzqhService zxc = (XzqhService) ac.getBean("xzqhService");
		zxc.m2();
		// 获得当前对象代理方式二
		XzqhService zx1c = (XzqhService)(AopContext.currentProxy());
		zx1c.m2();
//		m2();
	}
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=Throwable.class)
	public void m2(){
		jdbcTemplate.update("update XZQH set qhjb=? where qhbm='3405042002152' ", "1111111");	
		throw new NullPointerException();
	}
}

servlet得到数据所需数据后将数据写入HTTPServletResponse缓冲区,方法结束后中间件将缓冲区数据取出组成http响应返回客户端。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值