目录
1. Spring框架的概述
IOC:控制反转,将创建对象的过程交给spring进行管理
AOP:面向切面,在不修改源代码的情况之下进行代码功能的增强
ssm框架是由mybatis,Spring,SpringMVC组成
2.搭建Spring框架
创建maven工程,导入坐标依赖
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
编写demo,编写具体的实现方法
Demo.java
package com.qcby;
import org.springframework.context.ApplicationContext;
public class Demo {
public void run(){
System.out.println("hello Spring");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--IOC管理bean-->
<!--id:类的唯一标识符 class:类的全路径名-->
<bean id="demo" class="com.qcby.service.Demo" />
</beans>
编写测试方法。
DemoTest.java
import com.qcby.Demo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoTest {
@Test
public void testDemo(){
Demo userService = new Demo();
userService.run();
//创建spring工厂
}
@Test
public void run1(){
//创建spring工厂,加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean对象
Demo us = (Demo) ac.getBean("demo");
//调用方法
us.run();
}
}
输出