简单认识Spring框架
Spring框架是类似Mybatis的框架,都是采用XML映射文件,后面会将Spring和Mybatis结合使用,会更高效。
这一节课不需要连接数据库使用
1、新建Maven
项目:eclipse
——文件——新建——其他——Maven Project
2、新建src/main/resources
资源文件夹
3、修改pom.xml
文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<!--Spring版本号-->
<spring.version>5.0.8.RELEASE</spring.version>
</properties>
<dependencies>
<!-- log4j日志依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
<!--Spring的三个依赖context、core、test-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--添加spring-test依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
4、在src/main/java
文件夹里新建bean包,包里新建JavaBean类
- 成员变量的
Getter()
和Setter()
方法 - 打印该类对象相关信息的方法,如
toString()
方法、show()
方法
5、在src/main/test
文件夹里新建包用于存放测试类
6、在src/main/resources
文件夹里新建applicationContext.xml
文件(新建——其他——xml——xml File)
applicationContext.xml
映射关系大概有:
- 类里有无参构造函数:
- 类里有有参数构造函数:
- 类里有其他类对象:
- 类里有List、Map、Array数组:
7、测试(spring-test
和Java main
):
Java main
测试方法:(注意导包)
public class TestMain {
public static void main(String[] args) {
//读取Context容器xml映射文件地址
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext("applicationContext.xml");
//新建Student对象
Student student= //student是xml文件里bean id=student
applicationContext.getBean("student",Student.class);
//调用Student对象的show方法打印
student.show();
Teacher teacher=
applicationContext.getBean("teacher",Teacher.class);
teacher.show();
Classroom classroom=
applicationContext.getBean("classroom",Classroom.class);
classroom.show();
Mix mix=
applicationContext.getBean("mix",Mix.class);
mix.show();
}
}
运行结果:
spring-test
测试方法:(注意导包)
@RunWith(SpringJUnit4ClassRunner.class)//运行环境
@ContextConfiguration(locations= {"/applicationContext.xml"})//文件配置
public class TestJunit4 {
@Autowired
private Student student; //自动装配Student对象
@Autowired
private Teacher teacher;
@Autowired
private Classroom classroom;
@Autowired
private Mix mix;
@Test
public void testStudent() {
System.out.println("开始测试testStudent");
//调用student对象的show方法打印
student.show();
}
@Test
public void testTeacher() {
System.out.println("开始测试testTeacher");
teacher.show();
}
@Test
public void testClassroom() {
System.out.println("开始测试testClassroom");
classroom.show();
}
@Test
public void testMix() {
System.out.println("开始测试testMix");
mix.show();
}
}
运行结果: