关于java:java项目之路-一Spring-boot项目搭建

13次阅读

共计 6830 个字符,预计需要花费 18 分钟才能阅读完成。

1:可能产生的 erorr –pom.xml 文件中,引入 springboot 父依赖时,文件报错:

-- 解决方案:清空.m2/repository 下的所有依赖文件,从新下载即可 

2:springboot 集成 mybatis,倡议应用 xml 文件治理 sql 语句。

dependencies 如下:mysql:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
mybatis:
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

编写 applicastion.yml 文件:

    spring:
        datasource:
            type: com.alibaba.druid.pool.DruidDataSource
            url: jdbc:mysql://127.0.0.1:3306/person?useUnicode=true&characterEncoding=utf-8
            username: root
            password:
            driver-class-name: com.mysql.jdbc.Driver
    mybatis:
        mapper-locations: classpath:mapper/*.xml
        type-aliases-package: com.example.demo.model
        configuration:
            cache-enabled: true
            lazy-loading-enabled: false
            aggressive-lazy-loading: true
            multiple-result-sets-enabled: true
            map-underscore-to-camel-case: true
            auto-mapping-behavior: full
            use-column-label: true
            use-generated-keys: false
            default-executor-type: simple
            default-statement-timeout: 25000
    server:
        port: 8080

3:mapper 接口可应用注解 @Mapper,也能够采纳主动扫描机制在入口类应用 @MapperScan(“com.example.demo.dao”) 来注解 (举荐应用)
主动扫描 mapper 接口能够不增加注解 (idea 中,mapper 接口不应用注解的话,应用 @Autowired 注解是飘红,并不影响应用,
有强迫症的童鞋能够在 mapper 接口应用 @Repository 或者 @Resouse 注解 )

4:springboot 热部署:两种形式 (能够防止如论代码改变多少都须要频繁重启服务)

1): 增加 dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
2): 应用视图渲染 (例如:freemarker,Thymeleaf)
    freemarker 设置形式
    
spring:
  freemarker:
    cache: false
    
    Thymeleaf 设置形式:
spring: 
  thymeleaf:
    cache: false 

5:springboot 整合 freemarker:

 增加 dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
 编写 application.yml 文件:
spring:
  freemarker:
    cache: false  

6:springboot 整合 Thymeleaf:

 增加 dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
 编写 application.yml 配置文件
    
spring:
  thymeleaf:
    cache: false
    mode: HTML5

7:springboot 整合 redis:

 增加 dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
</dependency>
 配置文件 application.yml 编写
    
spring:
 redis:
    host: 192.168.44.129
    password: admin
  session:
    store-type: none
 可能报错:JedisDataException
起因:未设置 redis 明码
相干命令:config get requirepass: 这是查问 redis 是否配置明码, 如果返回为空,则表明未配置明码。config set requirepass“admin”这是将 redis 的明码设置为“admin”

8:SpringBoot 集成 Redis 音讯订阅公布

 
1) 创立一个 Redis 音讯接收器
    
package cn.tyrone.springboot.redis.message;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class Receiver {private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {this.latch = latch;}

    public void receiveMessage(String message) {LOGGER.info("Received <" + message + ">");
        latch.countDown();}
}
2) 注册一个监听器并发送音讯
    
package cn.tyrone.springboot.redis.message;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

//https://spring.io/guides/gs/messaging-redis/

@SpringBootApplication
public class Application {public static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

    /*
     * Redis 音讯监听器容器
     * 这个容器加载了 RedisConnectionFactory 和音讯监听器
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, 
            MessageListenerAdapter listenerAdapter){RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage"));
        return container;
    }

    /*
     * 将 Receiver 注册为一个音讯监听器,并指定音讯接管的办法(receiveMessage)* 如果不指定音讯接管的办法,音讯监听器会默认的寻找 Receiver 中的 handleMessage 这个办法作为音讯接管的办法
     */
    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver){return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    /*
     * Receiver 实例
     */
    @Bean
    Receiver receiver(CountDownLatch latch){return new Receiver(latch);
    }

    @Bean
    CountDownLatch latch(){return new CountDownLatch(1);
    }

    /*
     * Redis Template 用来发送音讯
     */
    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory){return new StringRedisTemplate(connectionFactory);
    }

    /*
     * 测试用例
     */
    public static void main(String[] args) throws Exception {ApplicationContext ctx = SpringApplication.run(Application.class, args);

        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
//      CountDownLatch latch = ctx.getBean(CountDownLatch.class);

        LOGGER.info("Sending message......");
        template.convertAndSend("sprinboot-redis-messaage", "Hello, SpringBoot redis message!!!!");
//      latch.wait();

        System.exit(0);

    }

}
    对于本例并不非常分明 CountDownLatch latch 这个的目标,在测试的过程中,加上这句代码,会抛一个异样,然而发送和接管音讯都是胜利的。如果将此代码正文掉,该异样也将隐没。同时,也并不影响音讯的公布与接管。CountDownLatch 只是一个同步的辅助类,测试过程中,并没有发现这个类对测试后果的有什么帮忙。

9:SpringBoot 整合 Apache ActiveMQ

 装置 ActiveMQ:间接去官网(http://activemq.apache.org/)下载最新版本即可,因为这是免装置的,只须要解压就行了。装置完之后进入 bin 目录,双击 activemq.bat 文件(linux 下在 bin 目录下执行 activemq start)在浏览器输出:http://ip:8161/admin/,呈现如下界面示意启动胜利 
    61616 为对外服务端口号 
    8161 为控制器端口号 
    当端口号抵触时,能够批改这两个端口号。cd conf , 批改 activemq.xml 批改外面的 61616 端口。批改 jetty.xml,批改外面的 8161 端口。1) 增加 dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2) 编写 application:
spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
        
删除不流动队列:个别状况下,ActiveMQ 的 queue 或者 topic 在不应用之后,能够通过 web 控制台来删除掉。当然,也能够通过配置,使得 broker 能够主动探测到无用的队列(肯定工夫内为空的队列)并删除掉,回收响应资源。activemq.xml

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data" destroyApplicationContextOnStop="true" schedulePeriodForDestinationPurge="10000">
    <destinationPolicy>
        <policyMap>
          <policyEntries>
            <policyEntry topic=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb">
              <pendingSubscriberPolicy>
                <vmCursor />
              </pendingSubscriberPolicy>
            </policyEntry>
            <policyEntry queue=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb">
            </policyEntry>
          </policyEntries>
        </policyMap>
    </destinationPolicy>
</broker>

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0