Spring 大家都不陌生,企业应用中很流行的一个平台。从最开始的 Servlet 控制所有,到 MVC 模式的出现。从 SSH (Struts、Spring、Hibernate) 所谓的三剑客 到 SpringMVC、SpringBoot 等等。技术总是不断在更新,开源框架也越来越多。世界上很多的大公司也慢慢走向了开源的道路,其实这也是必经之路。世界上总有那么多不断挑战权威,不断挑战垄断,不断改变生活的人。好啦,闲话不说了,我们开始搭建最基本,最小型化的 Spring 应用环境。Spring 核心的概念是控制反转,依赖注入。控制反转的意思就是控制权交给 Spring 平台,依赖注入就是程序依赖于 Spring Bean 通过 Java 反射机制将所需对象注入到程序中。

一、【工具】 

项目管理 Maven,开发工具 Eclipse Kepler,JDK 1.7。

二、【Jar 包】

spring-context-4.3.8.RELEASE.jar

三、【项目结构】

 

四、【web.xml】

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"> <display-name>mrp</display-name> </web-app>

五、【application.xml】

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="user" class="com.mrp.www.service.User" /> </beans>

六、【User类】

package com.mrp.www.service;

public class User {

    private String name = "xxxx";

    public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

七、【Test测试类】 

package com.mrp.www.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { private static ApplicationContext context; public static void main(String[] args) { context = new ClassPathXmlApplicationContext("application.xml"); User u = (User) context.getBean("user");
System.out.println(u.getName()); } }

八、【 pom.xml】

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>www.mrp.com</groupId>
<artifactId>mrp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mrp Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<build_path>C:\temp\${project.artifactId}</build_path>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring_webmvc_version>3.2.4.RELEASE</spring_webmvc_version>
<spring_version>4.2.6.RELEASE</spring_version>
<jstl_version>1.2</jstl_version>
<standard_version>1.0.6</standard_version>
<jackson_version>1.9.13</jackson_version>
<spring_test_version>3.2.4.RELEASE</spring_test_version>
<hessian_version>4.0.7</hessian_version>
<servlet_version>2.5</servlet_version>
</properties> <dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency> </dependencies> <build>
<finalName>mrp</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
<port>8017</port>
<path>/${project.artifactId}</path>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerVersion>1.6</compilerVersion>
<generatedSourcesDirectory>${build_path}</generatedSourcesDirectory>
<generatedTestSourcesDirectory>${build_path}</generatedTestSourcesDirectory>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>

Spring 平台可以集成很多的开源框架,而且 Spring 轻量级,配置简单,上面的例子仅仅配置了一个 User Bean,现实应用当中可就不止一个 Bean 这么简单了。会涉及到 AOP 编程,事务,数据源,缓存,分布式等等一系列的东西。后面在慢慢写出来吧,希望对大家有所帮助,也让自己有所收获。谢谢大家观看!

 

  

最新文章

  1. 微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案
  2. PHP程序设计
  3. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q91-Q93)
  4. android Camera 如何判断当前使用的摄像头是前置还是后置
  5. Math类和Random类(数学公式相关类)
  6. 细说C#多线程那些事-线程基础
  7. ListView异步加载网络图片完美版之双缓存技术
  8. java基础2
  9. Python金融应用编程(数据分析、定价与量化投资)
  10. 【SCOI2008】着色方案
  11. Peaceful Commission
  12. 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能
  13. day19其他模块
  14. mysql case when then else end 的用法
  15. AWS EC2实例Ubuntu系统设置root用户密码并使用root/ubuntu用户登录
  16. Effective Java - Item 1: Consider static factory methods instead of constructors
  17. Python3.5 执行发邮件脚本失败【惑】==&gt;【搞定】
  18. javascript时间日期操作
  19. Maven自动FTP远程部署
  20. imx6 Android6.0.1 init.rc解析

热门文章

  1. Python+selenium之测试报告(3)
  2. NullPointerException检测
  3. Git随笔:尝试将本地工程上传至Github上的repository仓库,构建远端与本地协同的Git环境
  4. POJ 3311 Hie with the Pie (状压DP)
  5. 复杂UI的组织-创建者模式-uitableview思想
  6. python_79_模块定义导入优化
  7. Ajax的原理及Django上传组件
  8. Linux运维笔记--第一部
  9. 安全和加密——openssl及自建CA
  10. vue 项目白屏解决方案