一、前導
用戶的請求會到達 Servlet,然後根據請求調用相應的 Java Bean,
並把所有的顯示結果交給 JSP 去完成,這樣的模式我們就稱爲 MVC 模式
M 代表 模型(Model)
模型就是數據,就是 dao,bean
V 代表 視圖(View)
JSP,用來展示模型中的數據
C 代表 控制器(controller)
控制器的作用就是把不同的數據(Model),
顯示在不同的視圖(View)上,Servlet 扮演的就是這樣的角色。
IOC 反轉控制
簡單說就是創建對象由以前的程序員自己new 構造方法來調用,變成了交由Spring創建對象
DI 依賴注入
簡單地說就是拿到的對象的屬性,已經被注入好相關值了,直接使用即可
BeanFactory爲IoC容器,而稱ApplicationContext爲應用上下文
1.通過xml文件將配置加載到IOC容器中
<?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">
<!--若沒寫id,則默認爲com.test.Man#0,#0爲一個計數形式-->
<bean id="man" class="com.test.Man"></bean>
</beans>
public class Test {
public static void main(String[] args) {
//加載項目中的spring配置文件到容器
//ApplicationContext context = new ClassPathXmlApplicationContext("resouces/applicationContext.xml");
//加載系統盤中的配置文件到容器
ApplicationContext context = new FileSystemXmlApplicationContext("E:/Spring/applicationContext.xml");
//從容器中獲取對象實例
Man man = context.getBean(Man.class);
man.driveCar();
}
}
2.通過java註解的方式將配置加載到IOC容器
@Configuration
public class ManConfig {
@Bean
public Man man() {
return new Man(car());
}
@Bean
public Car car() {
return new QQCar();
}
}
public class Test {
public static void main(String[] args) {
//從java註解的配置中加載配置到容器
ApplicationContext context = new AnnotationConfigApplicationContext(ManConfig.class);
//從容器中獲取對象實例
Man man = context.getBean(Man.class);
man.driveCar();
}
}
二、配置文件
1.環境
jdk1.8
maven3.x
IDEA2017
springboot1.5.9.release
在maven的settings.xml中添加
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2.寫一個功能:瀏覽器發送hello請求,服務器接收請求處理,相應helloworld字符串
idea的maven設置看how2j
創建一個maven工程(jar)
項目名:spring-boot-01-haluo
@SpringBootApplication
public class SbootApplication {
public static void main(String[] args) {
SpringApplication.run(SbootApplication.class, args);
}
}
@springbootapplication 標註一個主程序類,說明這是springboot
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello SpringBoot.";
}
}
@Controller 處理請求
@RequestMapping 接收請求
@RestController是@ResponseBody和@Controller的縮寫
3.簡化部署
雙擊package
配置文件
SpringBoot使用全局的配置文件
application.properties
application.yml
springweb開發:https://blog.csdn.net/netbar4/article/details/104137007