Spring Boot usage reference

1. Spring Boot entry example

1. Create a Maven project

    Group Id is set to com.tuozixuan, ArtifactId is set to springboot, Package is set to com.tuozixuan.springboot

 

2. Add the following configuration to the pom.xml file of the Maven project

  

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  

 3. Create a Java class under the com.tuozixuan.springboot package

 

@SpringBootApplication
@RestController
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@RequestMapping("/")
	public String index() {
		return "index";
	}
}

 4. Run the Application class and visit http://localhost:8080

 

5. Summary

 

spring-boot-starter: This is the core Spring Boot starter, including auto-configuration, logging, and YAML

spring-boot-starter-web: supports web development, including Tomcat and Spring Web MVC

@SpringBootApplication: It integrates the three annotation functions of @Configuration, @EnableAutoConfiguration, and @ComponentScan to use annotations for everyone

@RestController: equivalent to the combined effect of the two annotations @Controller and @ResponseBody

 

 

Second, the commonly used starters of Spring Boot and their functions

 

spring-boot-starter: This is the core Spring Boot starter, including auto-configuration, logging, and YAML.

spring-boot-starter-actuator : Helps monitor and manage applications.

spring-boot-starter-amqp : spring-rabbit to support the AMQP protocol (Advanced Message Queuing Protocol).

spring-boot-starter-aop: Supports aspect-oriented programming or AOP, including spring-aop and AspectJ.

spring-boot-starter-artemis: API (Java Message Service API) that supports JMS through Apache Artemis.

spring-boot-starter-batch: Support for Spring Batch, including HSQLDB databases.

spring-boot-starter-cache: Supports Spring's Cache abstraction.

spring-boot-starter-cloud-connectors: Support for Spring Cloud Connectors, which simplifies connecting services on cloud platforms like Cloud Foundry or Heroku.

spring-boot-starter-data-elasticsearch: Supports ElasticSearch search and analysis engines, including spring-data-elasticsearch.

spring-boot-starter-data-gemfire: Supports GemFire ​​distributed data storage, including spring-data-gemfire.

spring-boot-starter-data-jpa JPA Java Persistence API , spring-data-jpa spring-orm 、 hibernate

spring-boot-starter-data-MongoDB: Supports MongoDB data, including spring-data-mongodb.

spring-boot-starter-data-rest: Through spring-data-rest-webmvc, supports exposing Spring Data data warehouse through REST.

spring-boot-starter-data-solr: Supports the Apache Solr search platform, including spring-data-solr.

spring-boot-starter-freemarker: supports FreeMarker template engine.

spring-boot-starter-groovy-templates: Supports the Groovy template engine.

spring-boot-starter-hateoas: Supports HATEOAS-based RESTful web services through spring-hateoas.

spring-boot-starter-hornetq: Support for JMS via HornetQ.

spring-boot-starter-integration: supports the generic spring-integration module.

spring-boot-starter-jdbc: Supports JDBC databases.

spring-boot-starter-jersey: Supports Jersey RESTful web services framework.

spring-boot-starter-jta-atomikos: Supports JTA distributed transaction processing via Atomikos.

spring-boot-starter-jta-bitronix: Supports JTA distributed transaction processing via Bitronix.

spring-boot-starter-mail: supports the javax.mail module.

spring-boot-starter-mobile:支持spring-mobile。

spring-boot-starter-mustache: Supports the Mustache template engine.

spring-boot-starter-Redis: Supports Redis key-value store databases, including spring-redis.

spring-boot-starter-security:支持spring-security。

spring-boot-starter-social-facebook:支持spring-social-facebook

spring-boot-starter-social-linkedin:支持spring-social-linkedin

spring-boot-starter-social-twitter:支持pring-social-twitter

spring-boot-starter-test: supports regular test dependencies, including JUnit, Hamcrest, Mockito, and spring-test modules.

spring-boot-starter-thymeleaf: Support for the Thymeleaf template engine, including integration with Spring.

spring-boot-starter-velocity: supports Velocity template engine.

spring-boot-starter-web: Supports full-stack web development, including Tomcat and spring-webmvc.

spring-boot-starter-websocket: supports WebSocket development.

spring-boot-starter-ws:支持Spring Web Services。

 

spring-boot-starter-remote-shell: Added support for remote ssh shell.

 

spring-boot-starter-jetty: Introduced the Jetty HTTP engine (to replace Tomcat).

spring-boot-starter-log4j: Supports the Log4J logging framework.

spring-boot-starter-logging: Introduces Spring Boot's default logging framework, Logback.

spring-boot-starter-tomcat: Introduces Spring Boot's default HTTP engine, Tomcat.

spring-boot-starter-undertow: Introduced the Undertow HTTP engine (to replace Tomcat).

 

3. Spring Boot engineering operation database

1. Dependency configuration on JDBC and MYSQL in pom.xml file

 

    <!-- JDBC support-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
		
    <!-- MYSQL support-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

 

2. Create the application.properties configuration file in the resources directory and add the database configuration information

 

#datasource config

spring.datasource.url=jdbc:mysql://localhost:3306/coredb

spring.datasource.username=root

spring.datasource.password=password

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

3. Write a JAVA class UserDao that accesses data

 

@Component
public class UserDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void getUser() {
		String sql = "select * from ts_user where userName='tuozixuan'";
		Map<String, Object> userMap = jdbcTemplate.queryForMap(sql);
		System.out.println("userName:" + userMap.get("userName"));
	}
}

 

4. Inject UserDao into other places and use it

 

@Autowired

private UserDao userDao;

 

Fourth, log processing

 

1. Spring Boot logs use Commons Logging by default, and provides default configurations for Java Util Logging, Log4J2, and Logback.

 

2. Log level

    By default, the log output level is INFO

 

    When the log level is TRACE, the log information of TRACE, DEBUG, INFO, WARN, ERROR, FATAL will be output

    When the log level is DEBUG, the log information of DEBUG, INFO, WARN, ERROR, and FATAL will be output

    When the log level is INFO, the log information of INFO, WARN, ERROR, and FATAL will be output

    When the log level is WARN, the log information of WARN, ERROR, and FATAL will be output

    日志级别设为FATAL时,ERROR和FATAL的日志信息会输出

    日志级别设为OFF时,关闭所有级别的日志信息输出

 

    可通过在application.properties中添加logging.level.root配置来自定义日志级别,如:

 

    #可配置项:TRACE、DEBUG、INFO、WARN、ERROR、FATAL、OFF

    logging.level.root=DEBUG

 

3. 代码中日志信息的输出

 

    private Log log = LogFactory.getLog(Application.class);

 

    log.trace("----------trace----------");

    log.debug("----------debug----------");

    log.info("----------info----------");

    log.warn("----------warn----------");

    log.error("----------error----------");

    log.fatal("----------fatal----------");

 

4. 日志信息输出到文件

    默认情况下Spring Boot仅在控制台输出日志信息。

    如果要将日志信息输出到文件,可以通过在application.properties中添加logging.file或logging.path配置,如:

    

    logging.file=F:/springboot.log 或 logging.path=F:/

 

    logging.file配置可以把日志信息输出到自定义的日志文件中,日志文件会在10M大小的时候截断,产生新的日志文件

    logging.path配置可以把日志信息默认输出到spring.log文件中,日志文件会在10M大小的时候截断,产生新的日志文件

 

5. 使用log4j日志

5.1 在pom.xml文件中加入log4j依赖

    在spring-boot-starter中排除对spring-boot-starter-logging的依赖,并加入log4j的依赖

 

<!-- 排除对spring-boot-starter-logging的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 加入log4j的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
  5.2 在resources目录下创建log4j.properties文件,并添加log4j相关配置

 

log4j.rootLogger=debug,ServerDailyRollingFile,stdout

 

log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd

log4j.appender.ServerDailyRollingFile.File=F:/springboot.log

log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout

log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p [%c] - %m%n

log4j.appender.ServerDailyRollingFile.Append=true

 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n

 

 

 

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326467613&siteId=291194637