springboot-笔记
- 格式:docx
- 大小:3.87 MB
- 文档页数:123
springboot注解@SpringBootApplication分析@SpringBootApplication注解⽤在Spring Boot的⼊⼝类上⾯,是Spring Boot提供的应⽤启动相关的注解。
直接上注解的源码:@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) })public @interface SpringBootApplication {@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")Class<?>[] exclude() default {};@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")String[] excludeName() default {};@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")String[] scanBasePackages() default {};@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {};}@SpringBootApplication是⼀个复合注解查看源码,可以发现@SpringBootApplication是⼀个复合注解,包含了@SpringBootConfiguration,@EnableAutoConfiguration和@ComponentScan三个注解。
Springboot总结:核⼼功能和优缺点SpringBoot核⼼功能1、独⽴运⾏Spring项⽬Spring boot 可以以jar包形式独⽴运⾏,运⾏⼀个Spring Boot项⽬只需要通过java -jar xx.jar来运⾏。
2、内嵌servlet容器Spring Boot可以选择内嵌Tomcat、jetty或者Undertow,这样我们⽆须以war包形式部署项⽬。
3、提供starter简化Maven配置spring提供了⼀系列的start pom来简化Maven的依赖加载,例如,当你使⽤了spring-boot-starter-web,会⾃动加⼊如图5-1所⽰的依赖包。
4、⾃动装配SpringSpringBoot会根据在类路径中的jar包,类、为jar包⾥⾯的类⾃动配置Bean,这样会极⼤地减少我们要使⽤的配置。
当然,SpringBoot只考虑⼤多数的开发场景,并不是所有的场景,若在实际开发中我们需要配置Bean,⽽SpringBoot灭有提供⽀持,则可以⾃定义⾃动配置。
5、准⽣产的应⽤监控SpringBoot提供基于http ssh telnet对运⾏时的项⽬进⾏监控。
6、⽆代码⽣产和xml配置 SpringBoot不是借助与代码⽣成来实现的,⽽是通过条件注解来实现的,这是Spring4.x提供的新特性。
SpringBoot优缺点优点:快速构建项⽬。
对主流开发框架的⽆配置集成。
项⽬可独⽴运⾏,⽆须外部依赖Servlet容器。
-提供运⾏时的应⽤监控。
极⼤的提⾼了开发、部署效率。
与云计算的天然集成。
缺点:如果你不认同spring框架,也许这就是缺点。
SpringBoot特性创建独⽴的Spring项⽬内置Tomcat和Jetty容器提供⼀个starter POMs来简化Maven配置提供了⼀系列⼤型项⽬中常见的⾮功能性特性,如安全、指标,健康检测、外部配置等完全没有代码⽣成和xml配置⽂件SpringBoot CLISpringBoot CLI 是SpringBoot提供的控制台命令⼯具。
简单明了的带你理解springboot原理和三大核心注解一、springboot的定义Spring Boot是伴随着Spring4.0共同诞生的,它的目的就是简化spring的配置及开发,并协助开发人员可以整体管理应用程序的配置而不再像以前那样需要做大量的配置工作,它提供了很多开发组件,并且内嵌了web应用容器,如tomcat和Jetty等。
其目的便是使我们的开发变得简化并且能大幅度提高开发人员的开发效率,为了简化Spring功能的配置我们可以引入或启动我们需要的Spring功能。
这样做的好处就是避免开发人员过多的对框架的关注,而把更多的精力与时间放在系统的业务逻辑代码中。
二、SpringBoot的作用Spring Boot框架,其功能非常简单,便是帮助我们实现自动配置。
我们都知道Spring Boot框架的核心是自动配置。
只要有相应的jar包,Spring就会帮助我们实现自动配置,而无需像以前我们使用spring框架一样要做很多配置。
当默认配置不能满足我们要求的时候,我们能够用自己的配置来替换这些自动的配置类。
此外,上面我们也提到Spring Boot内嵌了web应用容器,除此之外还集成了系统监控等功能,这些都可以帮助我们快速搭建企业级的应用程序并使用。
三、Spring Boot的核心功能1.可以不依赖tomcat等外部容器来独立运行的web项目,springboot的优点是能够以jar包的形式运行。
2.嵌入式的Servlet容器:我们不需要像以前那边先打个war包,然后再运行,在springboot看来这些都是多余的,我们可以选择他内嵌的tomcat、Jetty或者Undertow等容器来直接运行。
3.使pom文件配置更简化:我们只需要在pom 文件中添加starter-web 依赖即可,无需像以前一样引入很多依赖而造成容易漏掉。
4.能够生产环境中直接使用性能指标、健康检查和应用信息等。
5.springboot不需要任何xml文件配置而能实现所有的spring配置。
1.1SpringBoot环境配置和常⽤注解Spring Boot常⽤注解:@Service: 注解在类上,表⽰这是⼀个业务层bean@Controller:注解在类上,表⽰这是⼀个控制层bean@Repository: 注解在类上,表⽰这是⼀个数据访问层bean@Component:注解在类上,表⽰通⽤bean ,value不写默认就是类名⾸字母⼩写@Autowired:按类型注⼊.默认属性required= true;当不能确定Spring 容器中⼀定拥有某个类的Bean 时,可以在需要⾃动注⼊该类Bean 的地⽅可以使⽤@Autowired(required = false),这等于告诉Spring:在找不到匹配Bean时也不抛出BeanCreationException 异常。
@Autowired 和 @Qualifier 结合使⽤时,⾃动注⼊的策略就从byType 转变byName 了。
@Autowired可以对成员变量、⽅法以及构造函数进⾏注释,⽽@Qualifier 的标注对象是成员变量、⽅法⼊参、构造函数⼊参。
正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统⼀成⼀个注释类。
@Resource:按名称装配区别:@Resource默认按照名称⽅式进⾏bean匹配,@Autowired默认按照类型⽅式进⾏bean匹配@Resource(importjavax.annotation.Resource;)是J2EE的注解,@Autowired(importorg.springframework.beans.factory.annotation.Autowired;)是Spring的注解@Configuration:注解在类上,表⽰这是⼀个IOC容器,相当于spring的配置⽂件,java配置的⽅式。
IOC容器的配置类⼀般与@Bean 注解配合使⽤,⽤@Configuration 注解类等价与 XML 中配置 beans,⽤@Bean 注解⽅法等价于 XML 中配置 bean。
Springboot注解@ServletComponentScan和@ComponentS。
⼀、SpringBoot中使⽤Servlet在SpringBootApplication上使⽤@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解⾃动注册,⽆需其他代码。
1.在⼊⼝Application类上加⼊注解@ServletComponentScanpackage com.hui;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;@SpringBootApplication@ServletComponentScanpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}2.新建Servlet类,继承HttpServlet并且加⼊注解@WebServlet(name=“TestServlet”,urlPatterns="/test")package com.hui.qiang;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(name="TestServlet",urlPatterns="/test")public class TestServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("doGet");}}⼆、Spring, Spring Boot中的@ComponentScan注解⽤法介绍@ComponentScan如果你理解了ComponentScan,你就理解了Spring.Spring是⼀个依赖注⼊(dependency injection)框架。
Spring Boot1、什么是 Spring Boot?Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。
2、Spring Boot有哪些优点?减少开发,测试时间和努力。
使用JavaConfig有助于避免使用XML。
避免大量的Maven导入和各种版本冲突。
提供意见发展方法。
通过提供默认值快速开始开发。
没有单独的Web服务器需要。
这意味着你不再需要启动Tomcat,Glassfish或其他任何东西。
需要更少的配置因为没有web.xml文件。
只需添加用@ Configuration注释的类,然后添加用@Bean注释的方法,Spring将自动加载对象并像以前一样对其进行管理。
您甚至可以将@Autowired添加到bean方法中,以使Spring自动装入需要的依赖关系中。
基于环境的配置使用这些属性,您可以将您正在使用的环境传递到应用程序:-Dspring.profiles.active = {enviornment}。
在加载主应用程序属性文件后,Spring将在(application{environment} .properties)中加载后续的应用程序属性文件。
3、Spring Boot 的核心配置文件有哪几个?它们的区别是什么?Spring Boot 的核心配置文件是 application 和 bootstrap 配置文件。
application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。
bootstrap 配置文件有以下几个应用场景。
使用 Spring Cloud Config 配置中心时,这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;一些固定的不能被覆盖的属性;一些加密/解密的场景4、Spring Boot 的配置文件有哪几种格式?它们有什么区别?v.properties 和 .yml,它们的区别主要是书写格式不同。
记⼀次SpringBootAspect不⽣效解决过程转⾃我的个⼈博客:问题描述项⽬中两个aspect,⼀个环绕controller,⽤于记录⽇志,能够正常在point处进⼊aspect处理;另⼀个aspect 的point设于service,死活不能进⼊。
解决思路1、⾸先排查pointcut配置是否正确,检查后发现没有问题;2、我们都知道spring的aop运⽤的是动态代理技术,由spring托管的bean⼤多为代理bean,controller层打印service对象,发现service对象竟然直接是service实现类的“本尊”。
如下图所⽰:再看springboot启动⽇志,发现roleServiceImpl实例化的时候有如下提⽰:Bean 'roleServiceImpl' of type [com.gaoxiaobo.wms.service.impl.RoleServiceImpl] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)那么问题就明了了,我们在controller层调⽤的roleServiceImpl对象⾮代理对象⽽是其本⾝,那么对其的aspect是不会⽣效的,不仅aspsect不会⽣效,事务注解@Transactional也不会⽣效,问题还是相当严重的。
那么,why 没有⽣成其代理对象呢?⼀定是哪⾥先调⽤了roleServiceImpl导致spring优先实例化了该bean;通过排查,找到了罪魁祸⾸:shiro 的LifecycleBeanPostProcessor优先实例化⾃定义Realm,⾃定义的Realm依赖于roleService,导致roleService被初始化。
解决⽅法⾃定义realm中roleService设置成懒加载。
深入浅出SpringBootSpringBoot是一个快速创建基于Spring框架的Java应用程序的工具。
它支持各种编码和编译工具,可以容易的构建Spring应用程序。
在本文中,我们将深入浅出地学习SpringBoot框架,以帮助您更好地理解并使用它。
一、为什么选择SpringBoot?SpringBoot最大的好处是:快速启动、便于配置,同时又提供了丰富的功能,可以快速部署。
二、SpringBoot的主要优点有哪些?1、基于Spring,使用Spring的所有特性。
2、零配置,开箱即用,少了很多繁琐的配置3、对开发人员友好,简单高效,灵活配置,可以集成多种开发框架。
4、自动配置,快速创建各种类库、Web应用和单元测试。
三、SpringBoot项目搭建1、创建项目:在IDEA中创建Maven项目,勾选“Create from archetype”并选择“maven-archetype-quickstart”。
2、在pom.xml文件中添加SpringBoot的相关依赖:<!-- SpringBoot Web模块依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot JDBC模块依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>3、编写代码:在src/main/java下新建SpringBoot的主类,添加注解@EnableAutoConfiguration来启用自动配置,添加注解@RestController来创建一个RESTful控制器。
______________________________________________________________________________________________________________ 精品资料 1. 今日大纲 1、 了解Spring的发展 2、 掌握Spring的java配置方式 3、 学习Spring Boot 4、 使用Spring Boot来改造购物车系统
访问 www.mukedaba.com 了解更多
2. Spring的发展
2.1. Spring1.x 时代 在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。
2.2. Spring2.x时代 随着JDK 1.5带来的注解支持,Spring2.x可以使用注解对Bean进行申明和注入,大大的减少了xml配置文件,同时也大大简化了项目的开发。
那么,问题来了,究竟是应该使用xml还是注解呢? 最佳实践: 1、 应用的基本配置用xml,比如:数据源、资源文件等; 2、 业务开发用注解,比如:Service中注入bean等;
2.3. Spring3.x到Spring4.x 从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x和Spring boot都推荐使用java配置的方式。
3. Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置。 ______________________________________________________________________________________________________________ 精品资料 3.1. @Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的: 1、@Configuration 作用于类上,相当于一个xml配置文件; 2、@Bean 作用于方法上,相当于xml配置中的;
3.2. 示例 该示例演示了通过Java配置的方式进行配置Spring,并且实现了Spring IOC功能。 3.2.1. 创建工程以及导入依赖 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">
4.0.0 cn.itcast.springboot itcast-springboot 1.0.0-SNAPSHOT war
org.springframework spring-webmvc 4.3.7.RELEASE com.jolbox bonecp-spring 0.8.0.RELEASE ${project.artifactId} org.apache.maven.plugins maven-resources-plugin ______________________________________________________________________________________________________________ 精品资料 UTF-8 org.apache.maven.plugins maven-compiler-plugin 1.7 1.7 UTF-8 org.apache.tomcat.maven tomcat7-maven-plugin 2.2
3.2.2. 编写User对象 public class User { private String username; private String password; private Integer age; public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; ______________________________________________________________________________________________________________ 精品资料 } public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
3.2.3. 编写UserDAO 用于模拟与数据库的交互 public class UserDAO {
public List queryUserList(){ List result = new ArrayList(); // 模拟数据库的查询 for (int i = 0; i < 10; i++) { User user = new User(); user.setUsername("username_" + i); user.setPassword("password_" + i); user.setAge(i + 1); result.add(user); } return result; }
} ______________________________________________________________________________________________________________
精品资料 3.2.4. 编写UserService 用于实现User数据操作业务逻辑
@Service public class UserService {
@Autowired // 注入Spring容器中的bean对象 private UserDAO userDAO;
public List queryUserList() { // 调用userDAO中的方法进行查询 return this.userDAO.queryUserList(); }
} 3.2.5. 编写SpringConfig 用于实例化Spring容器 @Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件 @ComponentScan(basePackages = "cn.itcast.springboot.javaconfig") //配置扫描包 public class SpringConfig {
@Bean // 通过该注解来表明是一个Bean对象,相当于xml中的 public UserDAO getUserDAO(){ return new UserDAO(); // 直接new对象做演示 }
}
3.2.6. 编写测试方法 用于启动Spring容器 public class Main {
public static void main(String[] args) { // 通过Java配置来实例化Spring容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
// 在Spring容器中获取Bean对象