zoukankan      html  css  js  c++  java
  • running programmer——spring-01(初谈spring)

      今天主要是通过一个简单的登录程序学习一些spring做基础的配置和功能。

    I.spring的核心配置applicationContext.xml

    关于bean的配置官方给出的最基础的配置文件如下:

        

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5            http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7   <bean id="..." class="...">
     8     <!-- collaborators and configuration for this bean go here -->
     9   </bean>
    10 
    11   <bean id="..." class="...">
    12     <!-- collaborators and configuration for this bean go here -->
    13   </bean>
    14 
    15   <!-- more bean definitions go here -->
    16 
    17 </beans>
    View Code

    下面简单的介绍下spring bean的相关配置: 

     (1)关于命名空间(xmlns)

      spring中命名空间大概有以下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" //这表示默认命名空间
     3     xmlns:hdp="http://www.springframework.org/schema/hadoop"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:cache="http://www.springframework.org/schema/cache"
     7     xmlns:context="http://www.springframework.org/schema/context"
     8     xmlns:mvc="http://www.springframework.org/schema/mvc"
     9     xmlns:oxm="http://www.springframework.org/schema/oxm"
    10     xmlns:p="http://www.springframework.org/schema/p"
    11     xmlns:c="http://www.springframework.org/schema/c"
    12     xmlns:util="http://www.springframework.org/schema/util"
    13     xsi:schemaLocation="http://www.springframework.org/schema/beans
    14         http://www.springframework.org/schema/beans/spring-beans.xsd
    15         http://www.springframework.org/schema/aophdp
    16         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    17         http://www.springframework.org/schema/cache
    18         http://www.springframework.org/schema/cache/spring-cache.xsd
    19         http://www.springframework.org/schema/context
    20         http://www.springframework.org/schema/context/spring-context-3.1.xsd
    21         http://www.springframework.org/schema/mvc
    22         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    23         http://www.springframework.org/schema/util
    24         http://www.springframework.org/schema/util/spring-util-3.1.xsd
    25         http://www.springframework.org/schema/hadoop 
    26         http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
    View Code

      xmlns:全名是xml namespace,也即是为当前的这个xml指定命名空间。xmlns:xsi:是指当前xml所要遵循的标签规范.如上hdp, xsi, aop, cache, context,   mvc…都是当前xml要使用到的一个标签,后面就是指定标签所要遵循的规范。xsi:schemaLocation:指定的命名空间对应的验证文件,用来定义xml schema的地       址,也就是xml书写时需要遵循的语法。另外这 些命名空间并不需要我们一个一个写,只要我们导入了相应的jar,在Eclipse的工具下从Source切到Namespaces,         我们就可以很方便的勾选我们需要的标签了。

          下面介绍几个常用的标签:

        1,xmlns:p:

         spring的p标签是基于XML Schema的配置方式,目的是为了简化配置方式。
         在XML文件头部添加xmlns:p="http://www.springframework.org/schema/p"即可使用。

         2.<!-- 自动扫描类包 使包中的spring注解起作用 -->

                  <context:component-scan base-package="com.baobaotao.dao"/>

      (2)关于自动启用spring注解(注解将在后面的文章中谈到)

       <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 可以用于总动启动spring中的@Autowired         注解,加了该注解 的属性spring将会将其自动作为一个bean注入到spring容器中。

       <context:component-scan base-package="com.baobaotao.dao"/> 可以自动扫描包使被扫描的包中的注解被启用。

     (3)关于bean的注入

       1.<property>注入:

    1 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    2         <property name="dataSource" ref="dataSource"/>    
    3     </bean>
    View Code

       2.xmlns:p:简单注入:

    1 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    2         destroy-method="close"
    3         p:driverClassName="com.mysql.jdbc.Driver"
    4         p:url="jdbc:mysql://127.0.0.1:3306/sampledb"
    5         ...
    6         />
    View Code

    或者:

    1 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    2     p:dataSource-ref="dataSource"/>
    View Code

    II.spring的测试框架JUnit

     关于spring的测试框架后面将会细说。

  • 相关阅读:
    C++11学习笔记
    孙鑫MFC学习笔记20:Hook编程
    孙鑫MFC学习笔记19:动态链接库
    孙鑫MFC学习笔记18:ActiveX
    孙鑫MFC学习笔记17:进程间通信
    孙鑫MFC学习笔记16:异步套接字
    孙鑫MFC学习笔记:15多线程
    ionic2 使用slides制作滑动效果的类型选择栏
    JavaScript简明教程之Node.js
    ionic2实战-使用Chart.js
  • 原文地址:https://www.cnblogs.com/kemir1105/p/5998559.html
Copyright © 2011-2022 走看看