一 、spring例子学习
1. 环境的设置
给maven 的settings.xml配置文件的profiles标签添加:
<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. springBoot的第一个例子:
- 创建maven文件
- 导入其相关的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
其中打包的插件为:
<build>
<!--打包插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 创建控制类程序:
@Controller
public class TestController {
//@ResponseBody是将内容返回到页面,并非页面跳转
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "helloword";
}
}
- 创建主运行程序,并将其运行起来。由于springboot内置tomcat服务器,无需在将其部署到服务器上。
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
- 打开浏览器进行访问,可以输入项目名,也可以不输入项目名。
运行结果如下:
3. springBoot的配置注解学习
举例说明:
//一个接口Car
public interface Car {
void say();
}
------------------------------------------
//一个BWM类
@Component
public class BWM implements Car{
@Override
public void say() {
System.out.println("我是宝马");
}
}
------------------------------------------
//一个丰田类
@Component
public class fengtian implements Car {
@Override
public void say() {
System.out.println("我是丰田");
}
}
由于springBoot是目的是省配置文件,所以无需再创建xml来配置,将其注入到配置文件中。现在是将其配置文件抽象成一个java实体类,但是必须有其注解@Configuration中,加上@Configuration该注解之后,该类代表是spring IOC容器,其中的每个方法是每一个Bean。如:
@Configuration
public class JavaConfig1 {
@Bean("BWM")
public Car getBWM(){
return new BWM();
}
}
------------------------------------------
@Configuration
public class JavaConfig2 {
@Bean("fengtian")
public Car getFengTian(){
return new fengtian();
}
}
-----多个配置文件合成一个配置文件-------------
@Configuration
@Import({JavaConfig1.class,JavaConfig2.class})
public class JavaConfigAll {
}
-----运行类--------------------------------
public class LoderTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(JavaConfigAll.class);
BWM bwm = (BWM) context.getBean("BWM");
bwm.say();
}
}
运行其主类即可。
=========================================================================
二、Spring boot注解学习
1.@SpringBootApplication:
这个注解标注在哪个类上,哪个类就是SpringBoot的主配置类,SpringBoot就可以运行这个类的main方法来启动SpringBoot应用。
点进去该注解有以下相应的注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
2.@SpringBootConfiguration:
标注在某个类上,表示这是一个Spring Boot的配置类;
3.@Configuration:配置类上来标注这个注解;
配置类 ----- 配置文件;配置类也是容器中的一个组件;@Component
4.@EnableAutoConfiguration:开启自动配置功能;
之前我们需要配置的东西,Spring Boot会帮助我们自动配置;点开该注解会有以下配置
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
点开*@AutoConfigurationPackage*可以看到
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
其中**@Import({Registrar.class})** 改注解可以自动的为容器导入包,其中包的路径为主配置类(@SpringBootApplication标注的类)的所在包及以下所有子包里面的所有组件扫描到spring容器中。
5.@Import(EnableAutoConfigurationImportSelector.class)
给容器中导入相应的组件。
**EnableAutoConfigurationImportSelector:**导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值.
三、springBoot 热部署
只需要引入spring-boot-devtools的模块依赖,修改java代码之后需要按Crtl+F9使其重新编译部署,不需要重启服务器,从而实现热部署。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
四、springBoot对静态资源的访问
如果是static,可以直接对其进行访问。
如果是templates是,必须通过controller进行跳转,他相当于eclipse中的web-inf。