简单认识Spring框架

简单认识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-testJava 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();
	}
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值