一、什么是Spring?
Spring是一个开源框架,管理对象的生命周期。使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能。
Spring是一个IOC和AOP容器框架。
二 、创建一个简单的Spring项目
注:本文章使用的是Maven创建的Spring项目(Maven使用的是阿里云镜像)
- 先把spring需要的最基本的jar给导入进来,把以下代码复制到pom.xml文件中的dependencies节点下。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- AOP -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
jar包就自动导进来了
2.创建一个applicationContext.xml配置文件,给配置文件加入约束。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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
- 如果想要使配置文件中输入代码的时候有提示,可以从
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/spring-context.xsd、
http://www.springframework.org/schema/aop/spring-aop.xsd
下载约束文件,分别保存的名字为spring-beans.xsd、spring-context.xsd和spring-aop.xsd。把这三个文件保存在自己指定的文件夹里。然后点击Window----->Preferences
添加成功就有提示了
- 验证Spring是否搭建好,我们来创建一个简单的spring项目。
- 创建一个Person类
package com.springdemo;
public class Person { //创建一个Person类
private int id;
private String name;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 在applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
id:给Person类指定一个person的对象
class:Perosn类的全路径名
-->
<bean id="person" class="com.springdemo.Person">
<!--
name:需要赋值的属性名
value:需要给属性赋的值
-->
<property name="id" value="001"></property>
<property name="name" value="张三"></property>
</bean>
</beans>
- 创建一个测试类
package com.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springdemo.Person;
public class MyTest {
@Test
public void test(){
//获取spring上下文对象
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person p=(Person) context.getBean("person");
System.out.println("id="+p.getId()+",name="+p.getName());
}
}
输出的值为如下,即Spring项目创建成功。