【Spring】Spring之浅析Spring框架的搭建
Spring是什么
Spring是一个开源的容器框架,用于配置bean并且维护bean之间关系的。其主要的思想就是IOC(Inversion Of Control,控制反转)或者称作DI(Dependency Injection,依赖注入)。IOC的解释就是程序中对象的获取方式发生反转,最初由new方式创建对象转化为由容器创建,注入,这样可以降低对象之间的耦合度。依赖注入,就是IOC的另一种说法,程序中被注入的对象依赖IOC容器中配置的对象。
Spring的结构图:

搭建Spring框架步骤
- 下载Spring开发包,Spring-4.3.4完整开发包下载。
- 在JavaProject下面建立一个lib目录,将开发包下面的libs目录里的所有以spring开头的jar文件,和commons-logging文件都拷贝到新建的lib里面。
- 将拷贝的文件全部选中,然后添加到Build path里。
简单Demo
我的结构目录:

建立一个User类
package cn.shop.bean; import java.sql.Date; public class User { private int id;
private String login_name;
private String password;
private Date birth; public User() {
super();
} public User(int id, String login_name, String password, Date birth) {
super();
this.id = id;
this.login_name = login_name;
this.password = password;
this.birth = birth;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getLogin_name() {
return login_name;
} public void setLogin_name(String login_name) {
this.login_name = login_name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} @Override
public String toString() {
return "User [id=" + id + ", login_name=" + login_name + ", password=" + password + ", birth=" + birth + "]";
} }User.java
建立一个UserDao类
package cn.shop.dao; import java.util.List; import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport; import cn.shop.bean.User; public class UserDao extends JdbcDaoSupport { public List<User> selectAllUser(){
return getJdbcTemplate().query("select * from xdl_user", new BeanPropertyRowMapper<User>(User.class));
} }UserDao
该类继承了JdbcDaoSupport类,该类里面有一个dataSource属性,该属性是用来创建jdbcTemplate用的。因为UserDao类直接继承了JdbcDaoSupport,因此可以拥有dataSource,便于后面在配置文件中设置。
上面除了query,还有queryForObject、update、execute。
new BeanPropertyRowMapper<User>(User.class)
这样代码采用了反射技术,就是把查询到结果集反射到User类的属性上。因此该类的属性应该和数据表中的字段名对应,可以忽略大小写。
当然除了使用BeanPropertyRowMapper,还可以使用RowMapper。
建立一个Test类
package cn.shop.test; import java.util.List; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.shop.bean.User;
import cn.shop.dao.UserDao; public class UserDaoTest { @Test
public void Test() throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao=ac.getBean("userDao", UserDao.class);
List<User> users = userDao.selectAllUser();
System.out.println(users);
}
}UserDaoTest.java
ApplicationContext是一个接口,由ClassPathXmlApplicationContext寻找applicationContext这个文件,在开发包里一个index.html的网页文件,读者可以在里面查找相应Spring版本的API文档。
ClassPathXmlApplicationContext类用来加载文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
建立applicationContext文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!--导入数据库相关文件-->
<util:properties id="db" location="classpath:db.properties"></util:properties> <bean id="database" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 驱动地址 -->
<property name="driverClassName" value="#{db.driverClassName}"></property>
<!-- 数据库的连接地址 -->
<property name="url" value="#{db.url}"></property>
<!-- 数据库的帐号 -->
<property name="username" value="#{db.username}"></property>
<!-- 数据库的密码 -->
<property name="password" value="#{db.password}"></property>
</bean> <bean id="userDao" class="cn.shop.dao.UserDao">
<!--
在调用set方法进行赋值,
Spring容器是通过反射技术 ,根据我们传递的name参数 , 得到对应的set方法名称, 将其调用
-->
<property name="dataSource" ref="database"></property>
</bean>
</beans>applicationContext.xml
建立db.properties文件
driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
password=517839
username=system关于上面beans的一些属性的使用,读者可以参见:Spring创建对象的三种方式
Spring-tool-suite的是什么
Spring-tool-suite就是一个方便Spring开发的工具,在使用之前是需要进行安装的,Eclipse安装Spring-tool-suite。
这里介绍的配置Spring的方法,不足以达到SpringMVC的使用,关于SpringMVC的配置可以参见SpringMVC的非注解配置的两种方式
参考文章
http://blog.csdn.net/zoutongyuan/article/details/27073683
本文为博主原创文章,如需转载请注明出处。
【Spring】Spring之浅析Spring框架的搭建的更多相关文章
- spring的了解以及简单框架的搭建
了解spring: Spring是一个开源的控制反转(Inversion of Controller)和面向切面(AOP)的框架,目的是为了简化开发. IOC(控制反转): public class ...
- SSM(Spring+SpringMVC+Mybatis)+Mysql 框架整合搭建流程以及其间注意事项
复习SSM框架,太久没用自己手动撘一个,发现自己还是有很多地方忘记了和没注意的事项... 首先,直接给出总流程: 零.引jar包 1.引包(或者写maven.pom) 一.数据库部分 设计数据库各表结 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Spring+SpringMVC+Mybatis(SSM)框架集成搭建
Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...
- Struts2+Spring+Hibernate(SSH)框架的搭建
首先需要下载struts2 ,spring4,hibernate5 的资源包; struts2资源包下载路径:http://www.apache.org/spring资源包下载路径:http://p ...
- asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用
摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc
在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试
这一部分的主要目的是 配置spring-service.xml 也就是配置spring 并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+M ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
随机推荐
- Window.sessionStorage
The sessionStorage property allows you to access a session Storage object for the current origin. ...
- OpenGL中的原子操作需要注意的地方
OpenGL中的原子操作需要注意的地方 仔细阅读看画红线的部分
- opencv直方图拉伸
1.首先计算出一幅图像的直方图 //计算直方图 cv::MatND ImageHist::getHist(const cv::Mat &image){ cv::Mat im; if(image ...
- 【Python】使用geopy由经纬度找地理信息
from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.reverse("3 ...
- C#.NET常见问题(FAQ)-如何让文本框textbox内容靠右显示
对于TextBox,我可以设置Text-Align属性为right,就可以让文字靠右了 对于Label而言,需要修改AutoSize为False,并修改TextAlign为MiddleRight, ...
- ccc如何在一台windows主机上搭建mysql主从复制
参考:http://www.cnblogs.com/wzjbk/p/6266899.htmlc 进入mysql: 进入到mysql的bin目录下才可以输入 mysql -hlocalhost -uro ...
- Android中创建option menu
1.首先在res目录下新建一个menu文件夹,右击res目录->New->Directory,输入文件夹名menu,点击OK. 接着在这个文件夹下再新建一个名叫main的菜单文件,右击me ...
- CheeseZH: Stanford University: Machine Learning Ex5:Regularized Linear Regression and Bias v.s. Variance
源码:https://github.com/cheesezhe/Coursera-Machine-Learning-Exercise/tree/master/ex5 Introduction: In ...
- 庞果英雄会第二届在线编程大赛·线上初赛:AB数
题目链接 给定两个正整数a,b,分别定义两个集合L和R, 集合L:即把1~a,1~b中整数乘积的集合定义为L = {x * y | x,y是整数且1 <= x <=a , 1 <= ...
- JMeter运行通过Chrome打开的website
部分website在chrome上运行正常,但在IE环境运行会存在问题.而是用 JMeter运行通过chrome打开的website时候,需要处理一下. 可以参考下面几篇文章: http://ninj ...