running programmer——spring-01(初谈spring)
今天主要是通过一个简单的登录程序学习一些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)的更多相关文章
- 【Spring】浅谈spring推荐构造器注入
一.前言 Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...
- 【Spring】浅谈spring为什么推荐使用构造器注入
一.前言 Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...
- Spring之初体验
Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...
- 从prototype beandefinition 谈 spring 的关闭流程和 prototype 的特性
背景介绍: 服务端期望使用 面向对象编程, 和 spring 结合的话只能是通过 prototype 的 bean 定义,并通过 getBean 获取. 优雅停机探究: 代码说明: 1. 类关系 Si ...
- 1.1浅谈Spring(一个叫春的框架)
如今各种Spring框架甚嚣尘上,但是终归还是属于spring的东西.所以在这里,个人谈一谈对spring的认识,笔者觉得掌握spring原理以及spring所涉及到的设计模式对我们具有极大的帮助.我 ...
- 浅谈Spring中的Quartz配置
浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...
- 浅谈Spring的两种配置容器
浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41 作者:黄小鱼ZZZ ...
- 浅谈Spring发展史
1 码农的春天----------Spring来了 Spring官网 :http://www.springframework.org 关于Spring的发展起源要回溯到2002年,当时正是Java E ...
- 黑马_13 Spring Boot:01.spring boot 介绍&&02.spring boot 入门
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 SpringBoot基础 1.1 原有 ...
随机推荐
- JAVA窗口程序实例一
package 甲; import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; ...
- javax.crypto.BadPaddingException: Given final block not properly padded
一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...
- kali Linux添加add-apt-repository
Debian让用户可以通过一个名为add-apt-repository的应用程序,添加和使用PPA软件库,不过Kali Linux在其默认的程序包列表中并不含有该应用程序.就Kali而言,由于这是个特 ...
- [bzoj2743][HEOI2012]采花(树状数组+离线)
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 1832 Solved: 954[Submit][Status] ...
- 元素定义了position:fixed;后怎么居中
div{ position:fixed; width:1200px; margin:0 auto; top:0; bottom:0; left:0; right:0; }
- VB.net的特殊语法(区别于C#.NET)
1:引入命名空间(Imports) Imports System.Exception Imports System.Data.SqlClient Imports System.Security.Cry ...
- .net连接DB2的异常SQL0666 - SQL query exceeds specified time limit or storage limit.错误处理
SQL0666 - SQL query exceeds specified time limit or storage limit. 原因:查询超时 解决办法: set the DbCommand.C ...
- Nginx模块之———— RTMP 模块的在线统计功能 stat 数据流数据的获取(不同节点则获取的方式不同)
一.目前只有一个Live节点存在 单节点获取方式如下: public function getStreamByIp($outerIP, $streamName) { //查询录像模块的IP地址外网,根 ...
- Struts2配置Result(Struts2_result)
一.概要 二.常用四种类型的配置 Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!D ...
- mysql 命令(二)
1.创建数据库,并制定默认的字符集是utf8. CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_g ...