今天主要是通过一个简单的登录程序学习一些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)的更多相关文章

  1. 【Spring】浅谈spring推荐构造器注入

    一.前言 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...

  2. 【Spring】浅谈spring为什么推荐使用构造器注入

    一.前言 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...

  3. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  4. 从prototype beandefinition 谈 spring 的关闭流程和 prototype 的特性

    背景介绍: 服务端期望使用 面向对象编程, 和 spring 结合的话只能是通过 prototype 的 bean 定义,并通过 getBean 获取. 优雅停机探究: 代码说明: 1. 类关系 Si ...

  5. 1.1浅谈Spring(一个叫春的框架)

    如今各种Spring框架甚嚣尘上,但是终归还是属于spring的东西.所以在这里,个人谈一谈对spring的认识,笔者觉得掌握spring原理以及spring所涉及到的设计模式对我们具有极大的帮助.我 ...

  6. 浅谈Spring中的Quartz配置

    浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...

  7. 浅谈Spring的两种配置容器

    浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41   作者:黄小鱼ZZZ     ...

  8. 浅谈Spring发展史

    1 码农的春天----------Spring来了 Spring官网 :http://www.springframework.org 关于Spring的发展起源要回溯到2002年,当时正是Java E ...

  9. 黑马_13 Spring Boot:01.spring boot 介绍&&02.spring boot 入门

    13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 SpringBoot基础 1.1 原有 ...

随机推荐

  1. Redis3.2+Tomcat实现集群的Session管理 -- tomcat-redis-session-manager的编译和开发部署环境搭建

    已经有不少文章介绍使用tomcat-redis-session-manager来实现Redis存储Tomcat的Session,实现分布式Session管理.但是现在官方编译的tomcat-redis ...

  2. ECNAScript6简介

    ECMAScript6.0(以下简称ES6)是JavaScript语言的下一代标砖,已经在2015年6月正式发布了,它的目标,是使得JavaScript语言可以用来编写负责的大型应用程序 ,成为企业级 ...

  3. JAVA模拟HTTP post请求上传文件

    在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...

  4. [ Arch Linux ] Arch更新源高速源整理之测速并自动排序高速源 加快更新效率

    方法一: 工具:rankmirrors 0x01 备份原来的源,并创建一个测试源 cd /etc/pacman.d cp mirrorlist mirrorlist.bakcp mirrorlist ...

  5. 关于ES3、ES5、ES6以及ES7所有数组的方法(api)的总结

    起因:工作用经常用到操作数组的方法,这里进行一下总结,我尽量以简洁的语言概括每个方法(api)的作用.如果您想快速定位,可以Control+F 然后搜相应的方法即可定位 :) ES3的数组方法 joi ...

  6. c++初步实现的一个LRU

    #include<iostream>#include<map> using namespace std; typedef struct Node{ int data; stru ...

  7. SQL Server优化常用SQL语句

    --所有没有主键的表 select name from sysobjects where xtype='U' and id not in ( select i.parent_obj from syso ...

  8. T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316-- ...

  9. JavaScript中关于地址的获取

    //取当前页面名称(不带后缀名) function pageName(){ var a = location.href; var b = a.split("/"); var c = ...

  10. springmvc 解决跨域CORS

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...