基础概念
1. 依赖注入(DI)
ApplicationContext Spring IOC 容器负责创建Bean和维护Bean之间的依赖关系
2. 控制反转(IOC)
通过容器来实现对象组件的装配和管理,是通过依赖注入实现的
3. 注解配置
3.1 声明
- @Component
- @Service
- @Repository
- @Controller
3.2 注入
- @Autowired
- @Inject
- @Resource
4. Java 配置
4.1 概念
Spring 4.x 推荐使用Java 配置
- @Configuration,相当与Spring配置的xml文件
- @Bean,当前方法返回值为一个Bean
4.2 原则
- 全局配置使用Java 配置
- 业务Bean使用注解配置
4.3. AOP
- @Aspect
- @After, @Before, @Around
Spring 常用配置
1. @Scope 新建Bean 的实例
2. @Value Spring EL,注入资源
3. 事件
- 自定义事件,ApplicationEvent
- 事件监听器, ApplicationListener
- 容器发布事件
4. @Profile,实例化不同的Bean
Spring 高级
1. 多线程
使用ThreadPoolTaskExecutor 实现一个基于线程池的TaskExecutor,然后@EnableAsync开启异步支持并通过Bean中的@Async注解来声明一个异步任务
2. 计划任务
- @EnableScheduling 开启对计划任务的支持
- @Scheduled, 声明计划任务
3. 条件注解,@Conditional,根据满足某一个特定条件创建一个特定的Bean
4. 组合注解与元注解
- 元注解,最原来的注解
- 组合注解,多个元注解注解后的注解,应用到Bean
Spring Boot 基础概念
- 独立运行Spring 项目,只需要java -jar xxx.jar
- 内嵌Servlet 容器, 无需使用war包部署
- 使用starter 简化maven 配置
- 提供基于http, ssh, telnet 对项目进行监控
- 无代码生成和xml配置
Hello Spring Boot !
1. pom.xml
<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>peerslee</groupId>
<artifactId>yierling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>yierling</name>
<url>http://maven.apache.org</url>
<!-- 父类
提供相关的默认的maven 配置,常用的包依赖可以省去version 标签
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Web 应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<!-- spring boot 编译插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. App.Java
package peerslee.yierling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@RestController
@SpringBootApplication //Spring Boot 项目和核心注解,开启自动配置
public class App
{
@RequestMapping("/")
String index() {
return "Hello Spring Boot!";
}
public static void main( String[] args ) throws Exception
{
SpringApplication.run(App.class, args);
}
}
3. Run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2017-06-04 15:28:13.006 INFO 7312 --- [ main] peerslee.yierling.App : Starting App on WIN10-703302300 with PID 7312 (D:\code\eclipse\yierling\target\classes started by Administrator in D:\code\eclipse\yierling)
2017-06-04 15:28:13.008 INFO 7312 --- [ main] peerslee.yierling.App : No active profile set, falling back to default profiles: default
2017-06-04 15:28:13.052 INFO 7312 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@df27fae: startup date [Sun Jun 04 15:28:13 CST 2017]; root of context hierarchy
2017-06-04 15:28:13.575 INFO 7312 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2017-06-04 15:28:14.039 INFO 7312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-04 15:28:14.091 INFO 7312 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-06-04 15:28:14.092 INFO 7312 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.32
2017-06-04 15:28:14.191 INFO 7312 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-04 15:28:14.191 INFO 7312 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1142 ms
2017-06-04 15:28:14.445 INFO 7312 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-04 15:28:14.451 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-04 15:28:14.672 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@df27fae: startup date [Sun Jun 04 15:28:13 CST 2017]; root of context hierarchy
2017-06-04 15:28:14.722 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String peerslee.yierling.App.index()
2017-06-04 15:28:14.725 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-04 15:28:14.725 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-04 15:28:14.746 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-04 15:28:14.746 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-04 15:28:14.778 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-04 15:28:14.861 INFO 7312 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-06-04 15:28:14.929 INFO 7312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-04 15:28:14.934 INFO 7312 --- [ main] peerslee.yierling.App : Started App in 2.134 seconds (JVM running for 2.432)
2017-06-04 15:28:36.706 INFO 7312 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-06-04 15:28:36.707 INFO 7312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-06-04 15:28:36.721 INFO 7312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms
4. 测试
http://localhost:8080/
问题
1. 使用maven 更新项目
Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix. yierling line 1 Maven Configuration Problem
2. localhost:8080拒绝访问解决方案(win10系统)
- 打开“我的电脑”,选择左上角的“计算机”中的“卸载或更改程序”。
- 点击“启用或关闭Windows功能”。
- 选中“Internet Information Service”