running programmer——spring-01(初谈spring)

2023-03-14,,

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

I.spring的核心配置applicationContext.xml

关于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean> <bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean> <!-- more bean definitions go here --> </beans>

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

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

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" //这表示默认命名空间
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophdp
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

  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>注入:

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

   2.xmlns:p:简单注入:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://127.0.0.1:3306/sampledb"
...
/>

或者:

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

II.spring的测试框架JUnit

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

running programmer——spring-01(初谈spring)的相关教程结束。

《running programmer——spring-01(初谈spring).doc》

下载本文的Word格式文档,以方便收藏与打印。