spring是什么?在各框架中的作用是什么?其实我认为spring是一家不透明的加工厂,为你管理bean。如果说springMVC负责web层。hibernate 负责持久化层,执行对数据库的crud操作。那么spring 负责业务层,即为web层提供接口,又集成了持久化层,使各框架在完成自己工作同时更实现了1+1+1>3的作用。
官方介绍:
轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
依赖注入(DI --- dependency injection、IOC)
面向切面编程(AOP --- aspect oriented programming)
容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)
如果大家还是不清楚,没关系。先会用,再去懂它也不迟。
按照国际惯例,我们先来一个Hello World。
第一步:创建maven web项目,不清楚的可以访问:http://blog.csdn.net/qq_19558705/article/details/49887717
第二步:导包
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring</groupId> <artifactId>helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> </dependencies> </project>第三步:HelloWorld文件
package com.spring.hello; public class HelloWorld { private String hello; public void setHello(String hello) { this.hello = hello; } public void helloWorld(){ System.out.println("Spring say :"+hello); } }第四步:配置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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置一个 bean --> <bean id="helloWorld" class="com.spring.hello.HelloWorld"> <!-- 为属性赋值 --> <property name="hello" value="Hello World"></property> </bean> </beans>
package com.spring.hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloTest { public static void main(String[] args) { //1. 创建Spring 的IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); //2. 从容器中获取 Bean 其实就是new 的过程 HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); // 也可以是 HelloWorld helloWorld = ctx.getBean(HelloWorld.class); //3. 执行函数 helloWorld.helloWorld(); } }
运行结果如果打印以下内容,则说明完成
十一月 23, 2015 12:01:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@edbe39: startup date [Mon Nov 23 12:01:46 CST 2015]; root of context hierarchy 十一月 23, 2015 12:01:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [beans.xml] Spring say :Hello World
网址链接:http://write.blog.csdn.net/postedit/49992021
*注:博客内容重在使用层面,常用知识点,并不会写的很详细,其目的是让读者快速入门,如果想钻研更深,请参考官网和其他博客。
每天都在进步,每天都在成长,如果有什么问题和建议可以留言,我会及时处理。