您的位置:首页 > 编程语言 > Java开发

一、springboot项目的简单使用

2017-07-05 09:26 741 查看
接着上篇搭建好项目之后,开始使用项目

一、配置application.peoperties文件

目录结构:



application.properties代码:
这里贴上我项目的配置文件,配置springboot,配置数据库,配置redist,配置kafka
# ======================= spring boot configuration ===============
server.port=8589
spring.aop.auto=true
spring.aop.proxy-target-class=true
server.session.timeout=18000
spring.velocity.check-template-location=false
spring.velocity.enabled=false
spring.resources.add-mappings=false
spring.mvc.favicon.enabled=false
spring.jmx.enabled=false
multipart.enabled=false
multipart.max-file-size=10MB

# ======================= database configuration ==================
spring.datasource.url=jdbc:mysql://59.110.42.200:3306/yccall?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=wdupec
spring.datasource.max-active=5
spring.datasource.maximum-pool-size=10
spring.datasource.initial-size=1
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

# ======================= redis configuration =====================
spring.redis.host=192.168.10.8
spring.redis.password=
spring.redis.port=6379
spring.redis.pool.max-idle=200
spring.redis.pool.min-idle=10
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-total=5000
spring.redis.timeout= 2000
spring.redis.topic=scan.data.call.center.redis
spring.redis.consumer.num=20

## ============================ kefu kafka (not the one used for connector)==============
youzai.kafka.producer.enabled=true
youzai.kafka.producer.properties.bootstrap.servers=192.168.10.4:9092
youzai.kafka.consumer.enabled=true
youzai.kafka.consumer.properties.zookeeper.connect=192.168.10.4:2181
youzai.kafka.consumer.properties.group.id=youzai.scan.data
youzai.kafka.consumer.properties.client.id=${youzai.kafka.consumer.properties.group.id}-client-${random.int}
youzai.kafka.consumer.properties.bootstrap.servers=${youzai.kafka.producer.properties.bootstrap.servers}
youzai.kafka.bridge=true
youzai.kafka.topic=scan.data.call.center
youzai.kafka.consumer.properties.zookeeper.session.timeout.ms =60000
## ============================ scan data data ==============
youzai.kafka.redis.topic=scan.data.call.center.redis
## ============================ kefu kafka (not the one used for connector)==============
youzai.scan.data.limit=100


二、写一个项目的启动类



注意@enablescheduling 是定时任务,没有定时任务可以把此注解去掉
@EnableAutoConfiguration注释,此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

@EnableTransactionManagement注释,它能够声明事务管理

@SpringBootApplication注解,它包括三个注解:

@Configuration:表示将该类作用springboot配置文件类。

@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置。

@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类。
@EnableJpaRepositories注解用于Srping JPA的代码配置   本项目操作数据访的方式是利用JPA的方式操作

代码:package org.uz.dxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableScheduling
@EnableAutoConfiguration
@EnableTransactionManagement
@SpringBootApplication
@EnableJpaRepositories
public class ScanDataApp
{
public static void main( String[] args )
{
SpringApplication.run(ScanDataApp.class, args);//这里如果只是想看能否正常启动,run方法里可以为空,此处因为程序引用定时任务类。
}
}

此时运行程序就可以看到项目正常运行。如果是web项目需导入 springboot的web依赖的jar包,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: