1、创建一个普通的java spring项目
(1)创建一个Maven的Java项目
(2)添加依赖(jar包)
-
spring-core
-
spring-beans
-
spring-context
-
spring-context-support
-
spring-expression
写在pom.xml中放在根标签中,如果根标签中有<dependencies>就是放在这个标签里,把这里的<dependencies>这个标签删除
<dependencies>
<!--spring-core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-beans-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-context-support-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
(3)对象准备
可以根据需要自己创建一个类
public class Employee {
public Employee() {
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
(4)配置文件
创建一个xml文件
(5)Spring依赖注入(有xml和注解两种方式注入,这里采用注解注入)
最后整个目录结构如下: