Spring简介
Spring框架是个轻量级的Java EE框架。所谓轻量级,是指不依赖于容器就能运行的。Struts、Hibernate也是轻量级的。
轻量级框架是相对于重量级框架而言的,重量级框架必须依赖特定的容器,例如EJB框架就必须运行在Glassfish、JBoss等支持EJB的容器中,而不能运行在Tomcat中。——《Java Web整合开发 王者归来》
Spring以IoC、AOP为主要思想,其中IoC,Inversion of Control 指控制反转或反向控制。在Spring框架中我们通过配置创建类对象,由Spring在运行阶段实例化、组装对象。AOP,Aspect Oriented Programming,面向切面编程,其思想是在执行某些代码前执行另外的代码,使程序更灵活、扩展性更好,可以随便地添加、删除某些功能。Servlet中的Filter便是一种AOP思想的实现。
Spring同时也是一个“一站式”框架,即Spring在JavaEE的三层架构[表现层(Web层)、业务逻辑层(Service层)、数据访问层(DAO层)]中,每一层均提供了不同的解决技术。如下:
- 表现层(Web层):Spring MVC
- 业务逻辑层(Service层):Spring的IoC
- 数据访问层(DAO层):Spring的jdbcTemplate
导入Spring框架(maven)
导入jar包:
注:
Module | Description |
---|---|
core | This is the main module that you will need for every Spring application. In this JAR file, you will find all the classes that are shared among all other Spring modules (for example, classes for accessing configuration files). Also, in this JAR, you will find selections of extremely useful utility classes that are used throughout the Spring codebase and that you can use in your own application. |
beans | This module contains all the classes for supporting Spring’s manipulation of Spring beans. Most of the classes here support Spring’s bean factory implementation. For example, the classes required for processing the Spring XML configuration file and Java annotations are packed into this module. |
context | This module contains classes that provide many extensions to Spring Core.You will find that all classes need to use Spring’s ApplicationContext feature, along with classes for Enterprise JavaBeans (EJB), Java Naming and Directory Interface (JNDI), and Java Management Extensions (JMX) integration. Also contained in this module are the Spring remoting classes, classes for integration with dynamic scripting languages (for example, JRuby, Groovy, and BeanShell), JSR-303 (Bean Validation), scheduling and task execution, and so on. |
expression | This module contains all support classes for Spring Expression Language (SpEL). |
maven:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
</dependencies>
Spring HelloWorld
新建maven工程,并引入Spring框架依赖。
HelloWorld.java
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("Hello," +
name);
}
public HelloWorld() {
}
}
Main.java
public class Main {
public static void main(String[] args) {
//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setName("wusuiwei");
//调用hello方法
helloWorld.hello();
// 1.创建Spring的IOC容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Bean实例
HelloWorld bean = context.getBean(HelloWorld.class);
//3.调用hello方法
bean.hello();
}
}
Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置bean-->
<bean name="helloWorld" class="com.wusuiwei.spring.beans.HelloWorld">
<property name="name" value="jack"></property>
</bean>
</beans>
运行结果: