springboot的druid监控

springBoot

创建一个maven项目jar

加入依赖


  org.springframework.boot
  spring-boot-starter-parent
  1.5.9.RELEASE

  	
  	
    
        org.springframework.boot
        spring-boot-starter-web
    


注解解释:

@RestController:相当于@Controller+@Responsebody

@EnableAutoConfiguration:自动配置

 

加入数据库:

    org.springframework.boot

    spring-boot-starter-data-jpa

实体类注解:

@id :主键
@GeneratedValue(strategy=GenerationType.AUTO)
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO. 
TABLE:使用一个特定的数据库表格来保存主键。 
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。 
IDENTITY:主键由数据库自动生成(主要是自动增长型) 
AUTO:主键由程序控制。 
通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id, 
其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法, 
JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出. 


 

想要访问jsp加入依赖:

 

org.apache.tomcat.embed

 

tomcat-embed-jasper

 

应用freemarker的依赖:



    org.springframework.boot
    spring-boot-starter-freemarker


//是否为freemarker开启缓存

spring.freemarker.cache=false


 

配置druid监控:

加入依赖:



    com.alibaba
    druid
    1.1.5


 

application.properties中配置,自动装配四要素

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource


 

创建ConfigBean类,相当于web.xml中配置之前的servlet配置与main放在同级:
@Configuration
public class ConfigBean {
/**
 * 
 * @return
 */
@Bean
public ServletRegistrationBean druidStatView(){
ServletRegistrationBean srb=new ServletRegistrationBean();
srb.setName("DruidStatView");
StatViewServlet svs=new StatViewServlet();
srb.setServlet(svs);
String url="/druid/*";
List urls=new ArrayList<>();
urls.add(url);
srb.setUrlMappings(urls);
LinkedHashMap linkedHashMap=new LinkedHashMap<>();
linkedHashMap.put("loginUsername", "admin");
linkedHashMap.put("loginPassword", "admin");
srb.setInitParameters(linkedHashMap);
return srb;
}
}


产品化特征(Actuator):


        org.springframework.boot
        spring-boot-starter-actuator
    


Application.properties配置

spring.datasource.url=jdbc:mysql://localhost/food
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat,config
spring.jpa.show-sql=true
server.port=80
server.context-path=/sb
spring.freemarker.cache=false
spring.devtools.restart.enabled=true 
debug=true


你可能感兴趣的:(springboot)