非web环境使用
pom文件为:
<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>com.hurricane.learn</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
java文件:
package com.hurricane.learn.springboot.standard;
import java.util.Arrays;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import com.hurricane.learn.springboot.standard.config.annotation.EnableLog;
import com.hurricane.learn.springboot.standard.dao.UserDao;
import com.hurricane.learn.springboot.standard.entity.Product;
import com.hurricane.learn.springboot.standard.entity.User;
/**
* springBoot的简单使用
* @author Administrator
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
// 运行方式一
// SpringApplication application = new SpringApplication(App.class);
// ConfigurableApplicationContext context = application.run(args);
// 运行方式二,其实两种方式运行一样
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
String[] beanNamesForType = context.getBeanNamesForType(User.class);
System.out.println(Arrays.asList(beanNamesForType));
Map<String, UserDao> beansOfType = context.getBeansOfType(UserDao.class);
System.out.println(beansOfType);
context.close();
}
@Bean
public Runnable createRunnable() {
return ()->{};
}
@Bean
public User createUser() {
return new User();
}
}
@SpringBootApplication注解是多个注解的集合,包括(@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan等),因此上面的App类实际上可以作为一个配置类来使用(在类的内部方法上可以通过@Bean注解来讲bean放到spring的容器中进行管理),App类通常位于包的比较顶层,因为会springboot会自动扫描App当前包及其子包下的Spring注解如(@Component,@Service等)
web环境使用
pom文件为:
<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>com.hurricane.learn</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
java文件为:
package com.hurricane.learn.springboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
application.run(args);
}
}
默认使用tomcat启动在8080端口,可以使用application.propertis文件进行配置。关于application.properties文件中的具体配置项,可以参考官方文档:
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix
为了将servlet、filter、listener等web基本组件引入到springboot的环境之中,可以使用@ServletComponentScan注解。如:
package com.hurricane.learn.springboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
application.run(args);
}
}
参考网址:
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
代码的github地址:
https://github.com/hurricane123/multi/tree/master/springboot