聊聊、Spring 第一篇
2024-09-28 20:35:05
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 编程,事务,数据源,缓存,分布式等等一系列的东西。后面在慢慢写出来吧,希望对大家有所帮助,也让自己有所收获。谢谢大家观看!
最新文章
- 微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案
- PHP程序设计
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q91-Q93)
- android Camera 如何判断当前使用的摄像头是前置还是后置
- Math类和Random类(数学公式相关类)
- 细说C#多线程那些事-线程基础
- ListView异步加载网络图片完美版之双缓存技术
- java基础2
- Python金融应用编程(数据分析、定价与量化投资)
- 【SCOI2008】着色方案
- Peaceful Commission
- 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能
- day19其他模块
- mysql case when then else end 的用法
- AWS EC2实例Ubuntu系统设置root用户密码并使用root/ubuntu用户登录
- Effective Java - Item 1: Consider static factory methods instead of constructors
- Python3.5 执行发邮件脚本失败【惑】==>;【搞定】
- javascript时间日期操作
- Maven自动FTP远程部署
- imx6 Android6.0.1 init.rc解析