查看“SpringBoot:快速入门”的源代码
←
SpringBoot:快速入门
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[category:SpringBoot]] == Spring的发展 == # Spring1.x: #: 通过'''xml文件'''配置bean:随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。 # Spring2.x: #:(JDK 1.5 注解支持)使用'''注解'''对Bean进行申明和注入:大大的减少了xml配置文件,同时也大大简化了项目的开发。 ## 应用的基本配置用xml(如:数据源、资源文件等); ## 业务开发用注解(如:Service中注入bean等); # Spring3.x(Spring4.x): #: 推荐使用'''Java配置'''方式:可以完全替代xml配置。 === Spring的Java配置方式 === (Java配置是Spring4.x推荐的配置方式)<br/> Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的: # '''@Configuration''':标记类,相当于一个xml配置文件; # '''@Bean''':标记方法,相当于xml配置中的“<bean>”; 其他配置类注解: # 读取外部的资源配置文件: ## '''@PropertySource''':指定读取的配置文件; ## '''@Value''':获取配置文件的配置项; 示例:配置数据库连接池: ::<syntaxhighlight lang="java"> // 通过该注解来表明该类是一个Spring的配置,相当于一个xml文件 @Configuration // 配置扫描包,相当于xml中的"<context:component-scan ...>" @ComponentScan(basePackages = "cn.itcast.springboot.javaconfig") //加载配置文件 @PropertySource(value = { "classpath:jdbc.properties", "xxx.properties" }, ignoreResourceNotFound = true) public class SpringConfig { @Bean // 通过该注解来表明是一个Bean对象,相当于xml中的<bean> //1、相当于xml中的<bean>标签 //2、相当于类中的@Service类的注解 //所以"UserDAO"可以用于"UserService"属性注入 public UserDAO getUserDAO() { return new UserDAO(); // 直接new对象做演示 } //返回值,相当于<bean>中的"class"属性 //方法名称,相当于<bean>中的"id"属性? // 获取配置文件的配置项 @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.driverClassName}") private String jdbcDriverClassName; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; @Bean(destroyMethod = "close") public DataSource dataSource() { BoneCPDataSource boneCPDataSource = new BoneCPDataSource(); // 数据库驱动 boneCPDataSource.setDriverClass(jdbcDriverClassName); // 相应驱动的jdbcUrl boneCPDataSource.setJdbcUrl(jdbcUrl); // 数据库的用户名 boneCPDataSource.setUsername(jdbcUsername); // 数据库的密码 boneCPDataSource.setPassword(jdbcUsername); // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 boneCPDataSource.setIdleMaxAgeInMinutes(30); // 每个分区最大的连接数 boneCPDataSource.setMaxConnectionsPerPartition(100); // 每个分区最小的连接数 boneCPDataSource.setMinConnectionsPerPartition(5); return boneCPDataSource; } } </syntaxhighlight> === 实例 === # 创建工程以及导入依赖: #: <syntaxhighlight lang="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>cn.itcast.springboot</groupId> <artifactId>itcast-springboot</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.8.0.RELEASE</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project> </syntaxhighlight> # 编写User对象: #: <syntaxhighlight lang="java"> public class User { private String username; private String password; private Integer age; // getter & setter & toString } </syntaxhighlight> # 编写UserDao接口: #: <syntaxhighlight lang="java"> public interface UserDaoUserDao { //根据id查询用户信息 public User findUserById(int id) throws Exception; } </syntaxhighlight> # 编写UserDaoImpl接口:(mybatis) #: <syntaxhighlight lang="java"> public class UserDaoImpl implements UserDao { // 需要向dao实现类中注入SqlSessionFactory // 这里通过构造方法注入 private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public User findUserById(int id) throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("test.findUserById", id); // 释放资源 sqlSession.close(); return user; } } </syntaxhighlight> # 编写UserService实现User数据操作业务逻辑: #: <syntaxhighlight lang="java"> @Service public class UserService { @Autowired // 注入Spring容器中的bean对象 private UserDaoImpl UserDaoImpl; public User findUserById(int id) throws Exception { // 调用userDAO中的方法进行查询 return this.UserDaoImpl.findUserById(id); } } </syntaxhighlight> # 编写SpringConfig用于'''实例化Spring容器''': #: <syntaxhighlight lang="java" highlight="1-2,5"> @Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件 @ComponentScan(basePackages = "cn.itcast.springboot.javaconfig") //配置扫描包 public class SpringConfig { @Bean // 通过该注解来表明是一个Bean对象,相当于xml中的<bean> public UserDao getUserDao(){ return new UserDao(); // 直接new对象做演示 } } </syntaxhighlight> # 编写测试方法,'''启动Spring容器''': #: <syntaxhighlight lang="java"> public class Main { public static void main(String[] args) { // 通过Java配置来实例化Spring容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); // 对比使用:使用xml方式的“加载spring配置文件” ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); // 对比使用:使用xml方式的“根据配置创建对象” UserService userService = (UserService) context.getBean("userService"); // 在Spring容器中获取Bean对象 UserService userService = context.getBean(UserService.class); // 调用对象中的方法 List<User> list = userService.queryUserList(); for (User user : list) { System.out.println(user.getUsername() + ", " + user.getPassword() + ", " + user.getPassword()); } // 销毁该容器 context.destroy(); } } </syntaxhighlight> === 实例化Spring容器:xml与Java配置 === 对比,“xml、注解”于“Java配置类”的实例化Spring容器: <syntaxhighlight lang="java"> // 通过Java配置来实例化Spring容器 // 1、Java配置类 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); // 2、xml、注解 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 在Spring容器中“获取Bean对象” // 1、Java配置类 UserService userService = context.getBean(UserService.class); // 2、xml、注解 UserService userService = (UserService) context.getBean("userService"); </syntaxhighlight> == Spring Boot == “习惯优于配置”:<br/> SpringBoot是一种全新的框架,目的是为了简化Spring应用的初始搭建以及开发过程。该框架使用特定的方式(集成starter,约定优于配置)来进行配置,从而使开发人员不需要再定义样板化的配置。 * SpringBoot基于Sring4进行设计,继承了原有Spring框架的优秀基因。 * SpringBoot并不是一个框架,而是一些类库的集合。 * maven或者gradle项目导入相应依赖即可使用SpringBoot,而无需自行管理这些类库的版本。 优点: # 快速创建基于Spring的应用程序。 # 无需手动管理依赖包的版本。 # 自动配置,无需XML。 # 嵌入式的Tomcat,无需部署war文件。 === 快速入门 === ==== 设置spring boot的parent ==== <syntaxhighlight lang="xml"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> </syntaxhighlight> Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。 ==== 导入spring boot的web支持 ==== <syntaxhighlight lang="xml"> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </syntaxhighlight> ==== 添加Spring boot的插件 ==== <syntaxhighlight lang="xml"> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <pluginManagement> ... </pluginManagement> </build> </syntaxhighlight> ==== 编写第一个Spring Boot的应用 ==== <syntaxhighlight lang="java"> @Controller @SpringBootApplication @Configuration public class HelloApplication { @RequestMapping("hello") @ResponseBody public String hello(){ return "hello world!"; } public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } } </syntaxhighlight> # '''@SpringBootApplication''':Spring Boot项目的核心注解,主要目的是开启自动配置; # '''@Configuration''':这是一个配置Spring的配置类; # '''@Controller''':标明这是一个SpringMVC的Controller控制器; # main方法:在main方法中启动一个应用,即:这个应用的入口; ==== 启动应用 ==== 在Spring Boot项目中,启动的方式有两种,一种是另外一种是。 # 直接run Java Application #: [[File:SpringBoot启动应用:RunAS ”Java Aplication“.png|600px]] # 通过Spring Boot的Maven插件运行 #: [[File:SpringBoot启动应用:Maven插件运行.png|600px]] == SpringBoot核心 == === 入口类和@SpringBootApplication === Spring Boot的项目一般都会有“xxxApplication”的入口类,类中有main方法,这是一个标准的Java应用程序的入口方法。<br/> <syntaxhighlight lang="java"> @Controller @SpringBootApplication(exclude = { RedisAutoConfiguration.class }) @Configuration public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } } </syntaxhighlight> 其中:“'''@SpringBootApplication'''”:是Spring Boot的核心注解,它其实是一个组合注解: <syntaxhighlight lang="java"> @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 { </syntaxhighlight> # “'''@SpringBootConfiguration'''”:Spring Boot项目的'''配置'''注解(组合注解): #: <syntaxhighlight lang="java"> @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { } </syntaxhighlight> #* 在Spring Boot中推荐使用“@SpringBootConfiguration”替代“@Configuration”; # “'''@EnableAutoConfiguration'''”:'''启用自动配置''',该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项: # “'''@ComponentScan'''”:默认扫描“@SpringBootApplication”所在类的同级目录,以及它的子目录; === 关闭自动配置 === 启用自动配置时,Spring Boot会根据项目中的jar包依赖,自动做出配置;而Spring Boot支持的自动配置如下(非常多): :[[File:SpringBoot启动应用:自动配置.png|400px]] 如果不需要Spring Boot自动配置,想关闭某一项的自动配置(以Redis为例): <syntaxhighlight lang="java"> @Controller @SpringBootApplication(exclude = { RedisAutoConfiguration.class }) @Configuration public class HelloApplication { </syntaxhighlight> === 自定义Banner === 启动Spring Boot项目后会看到默认的Banner: :[[File:SpringBoot启动应用:Banner.png|600px]] * 关闭Banner: *: <syntaxhighlight lang="java"> public static void main(String[] args) { // SpringApplication.run(HelloApplication.class, args); SpringApplication application = new SpringApplication(HelloApplication.class); application.setBannerMode(Mode.OFF); application.run(args); } </syntaxhighlight> * 自定义Banner: *# 在[http://patorjk.com/software/taag/#p=display&h=3&v=3&f=4Max&t=itcast%20Spring%20Boot 工具站点]中生成Banner,并保存为“banner.txt”; *# 将banner.txt拷贝到项目的resources目录; *#: [[File:SpringBoot启动应用:Banner自定义.png|200px]] === 全局配置文件 === Spring Boot项目使用一个全局的配置文件“application.properties”(或“application.yml”),在“resources”目录下(或:类路径下的“/config”下)。<br/> 可以进行项目全局配置,如: : <syntaxhighlight lang="properties"> server.port=8088 server.servlet-path=/ spring.resources.static-locations=classpath:/public/ logging.level.org.springframework=DEBUG </syntaxhighlight> * 更多配置:'''[[SpringBoot全局配置]]''' === Starter pom === Starter POMs是可以包含到应用中的一个方便的“依赖关系描述符集合”。 * 可以通过starters获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符; * 所有的starters遵循一个相似的命名模式:“'''spring-boot-starter-*'''”; 例如:在 SpringBoot 项目中集成 Redis,那么我只需要加入“spring-data-redis-starter”的依赖: <syntaxhighlight lang="xml"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </syntaxhighlight> 而不需考虑与其他内容的依赖、版本等; 官方支持的 starter pom:(2.3.5.RELEASE) {| class="wikitable" ! 名称 !! 描述 |- ! colspan="2"| application starters |- | spring-boot-starter || Core starter, including auto-configuration support, logging and YAML |- | spring-boot-starter-activemq || Starter for JMS messaging using Apache ActiveMQ |- | spring-boot-starter-amqp || Starter for using Spring AMQP and Rabbit MQ |- | spring-boot-starter-aop || Starter for aspect-oriented programming with Spring AOP and AspectJ |- | spring-boot-starter-artemis || Starter for JMS messaging using Apache Artemis |- | spring-boot-starter-batch || Starter for using Spring Batch |- | spring-boot-starter-cache || Starter for using Spring Framework’s caching support |- | spring-boot-starter-data-cassandra || Starter for using Cassandra distributed database and Spring Data Cassandra |- | spring-boot-starter-data-cassandra-reactive || Starter for using Cassandra distributed database and Spring Data Cassandra Reactive |- | spring-boot-starter-data-couchbase || Starter for using Couchbase document-oriented database and Spring Data Couchbase |- | spring-boot-starter-data-couchbase-reactive || Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive |- | spring-boot-starter-data-elasticsearch || Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch |- | spring-boot-starter-data-jdbc || Starter for using Spring Data JDBC |- | spring-boot-starter-data-jpa || Starter for using Spring Data JPA with Hibernate |- | spring-boot-starter-data-ldap || Starter for using Spring Data LDAP |- | spring-boot-starter-data-mongodb || Starter for using MongoDB document-oriented database and Spring Data MongoDB |- | spring-boot-starter-data-mongodb-reactive || Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive |- | spring-boot-starter-data-neo4j || Starter for using Neo4j graph database and Spring Data Neo4j |- | spring-boot-starter-data-r2dbc || Starter for using Spring Data R2DBC |- | spring-boot-starter-data-redis || Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client |- | spring-boot-starter-data-redis-reactive || Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client |- | spring-boot-starter-data-rest || Starter for exposing Spring Data repositories over REST using Spring Data REST |- | spring-boot-starter-data-solr || Starter for using the Apache Solr search platform with Spring Data Solr |- | spring-boot-starter-freemarker || Starter for building MVC web applications using FreeMarker views |- | spring-boot-starter-groovy-templates || Starter for building MVC web applications using Groovy Templates views |- | spring-boot-starter-hateoas || Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS |- | spring-boot-starter-integration || Starter for using Spring Integration |- | spring-boot-starter-jdbc || Starter for using JDBC with the HikariCP connection pool |- | spring-boot-starter-jersey || Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web |- | spring-boot-starter-jooq || Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc |- | spring-boot-starter-json || Starter for reading and writing json |- | spring-boot-starter-jta-atomikos || Starter for JTA transactions using Atomikos |- | spring-boot-starter-jta-bitronix || Starter for JTA transactions using Bitronix. Deprecated since 2.3.0 |- | spring-boot-starter-mail || Starter for using Java Mail and Spring Framework’s email sending support |- | spring-boot-starter-mustache || Starter for building web applications using Mustache views |- | spring-boot-starter-oauth2-client || Starter for using Spring Security’s OAuth2/OpenID Connect client features |- | spring-boot-starter-oauth2-resource-server || Starter for using Spring Security’s OAuth2 resource server features |- | spring-boot-starter-quartz || Starter for using the Quartz scheduler |- | spring-boot-starter-rsocket || Starter for building RSocket clients and servers |- | spring-boot-starter-security || Starter for using Spring Security |- | spring-boot-starter-test || Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito |- | spring-boot-starter-thymeleaf || Starter for building MVC web applications using Thymeleaf views |- | spring-boot-starter-validation || Starter for using Java Bean Validation with Hibernate Validator |- | spring-boot-starter-web || Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container |- | spring-boot-starter-web-services || Starter for using Spring Web Services |- | spring-boot-starter-webflux || Starter for building WebFlux applications using Spring Framework’s Reactive Web support |- | spring-boot-starter-websocket || Starter for building WebSocket applications using Spring Framework’s WebSocket support |- ! colspan="2"| production starters |- | spring-boot-starter-actuator || Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application |- ! colspan="2"| technical starters |- | spring-boot-starter-jetty || Starter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat |- | spring-boot-starter-log4j2 || Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging |- | spring-boot-starter-logging || Starter for logging using Logback. Default logging starter |- | spring-boot-starter-reactor-netty || Starter for using Reactor Netty as the embedded reactive HTTP server. |- | spring-boot-starter-tomcat || Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web |- | spring-boot-starter-undertow || Starter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat |} === Xml 配置文件 === # “'''@PropertySource'''”:加载指定的配置文件; #* 只能加载“*.properties”文件,不能加载“.yaml”文件; #: <syntaxhighlight lang="java"> @PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true) </syntaxhighlight> # “'''@Import'''”: #* 4.2之前,只支持导入配置类;(相当于“<import resource=""/>”标签) #* 4.2之后,支持导入普通的java类,并将其声明成一个bean; #: <syntaxhighlight lang="java"> @Import({ CustomerConfig.class, SchedulerConfig.class }) </syntaxhighlight> # “'''@ImportResource'''”:导入Spring的配置文件,让配置文件里面的内容生效; #* SpringBoot中编写的Spring配置文件是不能自动识别的;【???】 #: <syntaxhighlight lang="java"> @ImportResource(locations = {"classpath:applicationContext.xml"}) </syntaxhighlight> === 日志 === Spring Boot对各种日志框架都做了支持,可以通过全局配置来修改默认的日志的配置: <syntaxhighlight lang="properties"> #设置日志级别 logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG` </syntaxhighlight> == 自动配置的原理 ==
返回至“
SpringBoot:快速入门
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
笔记
服务器
数据库
后端
前端
工具
《To do list》
日常
阅读
电影
摄影
其他
Software
Windows
WIKIOE
所有分类
所有页面
侧边栏
站点日志
工具
链入页面
相关更改
特殊页面
页面信息