一、前导
用户的请求会到达 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