论坛首页 Java企业应用论坛

高手问答:如何使用Spring Boot进行Java EE开发——公布获奖名单

浏览 13634 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2016-04-11  
wiselyman 写道
string2020 写道
spring boot 版本 1.3.3.RELEASE

该版本默认自带的tomcat的版本为:8.0.32
请问,如何在spring boot 项目里面配置这个tomct的最大线程数、最大连接数


本书的试读样章有您提问的相关问题的解决方案。
Spring Boot关于tomcat的属性列表如下
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI


若都不能满足要求,可以定制:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new
           TomcatEmbeddedServletContainerFactory();
        factory.setPort(8888); //1
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));//2
        factory.setSessionTimeout(10, TimeUnit.MINUTES); //3
        return factory;
}



谢谢。
看了一下,如果只靠参数配置,就只能设置最大线程数了,最大连接数设置不了。
看来只能定制了。
0 请登录后投票
   发表时间:2016-04-11  
string2020 写道
wiselyman 写道
string2020 写道
spring boot 版本 1.3.3.RELEASE

该版本默认自带的tomcat的版本为:8.0.32
请问,如何在spring boot 项目里面配置这个tomct的最大线程数、最大连接数


本书的试读样章有您提问的相关问题的解决方案。
Spring Boot关于tomcat的属性列表如下
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI


若都不能满足要求,可以定制:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new
           TomcatEmbeddedServletContainerFactory();
        factory.setPort(8888); //1
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));//2
        factory.setSessionTimeout(10, TimeUnit.MINUTES); //3
        return factory;
}



谢谢。
看了一下,如果只靠参数配置,就只能设置最大线程数了,最大连接数设置不了。
看来只能定制了。

Spring Boot没有阻碍任何定制和扩展的可能性。
0 请登录后投票
   发表时间:2016-04-11  
不好意思,我又来了,最近在用spring boot整合mybatis的时候,出了不少问题。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface

spring boot 版本:1.3.3.RELEASE

其中,代码如下:
项目结构如下

package com.pp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

package com.pp.email;
public interface SendService
{

}


package com.pp.email;

import org.springframework.stereotype.Component;

@Component
public class SendServiceImpl implements SendService
{

}

package com.pp.user;

public interface UserManager
{

}


package com.pp.user;

import org.springframework.stereotype.Component;

@Component("userManager")
public class UserManagerImpl implements UserManager
{

}

<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>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<groupId>com.pp</groupId>
	<artifactId>springboot3</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.0.1</version>
		</dependency>
	</dependencies>
</project>

项目没有其他任何的配置文件,所有的配置都是默认的。
然而,启动报错了
报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendService' defined in file [E:\ide\code\src9\springboot3\target\classes\com\pp\email\SendService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at com.pp.App.main(App.java:11) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:68) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	... 17 common frames omitted


我如果把com.pp.email包以及包下面的东西都干掉,就没有问题了。
问题是,com.pp.email这个包里面就一个接口,接口的实现类

附件的springboot3.zip是整个工程的源代码,麻烦帮忙看看,谢谢

请问,哪里有问题?
谢谢
  • 大小: 22.7 KB
0 请登录后投票
   发表时间:2016-04-11  
spring boot 应该可以把程序变成war文件,部署到tomcat或jetty上吧?
不使用embed container
0 请登录后投票
   发表时间:2016-04-11  
wiselyman 写道
string2020 写道
你好,最近用springboot碰到1个问题,想请教一下,谢谢。
spring-boot版本:1.3.3.RELEASE

我写了一个listener,想监听spring容器启动的事件
public final class ApplicationStartup implements ApplicationListener<ContextStartedEvent>
{	
	public void onApplicationEvent(ContextStartedEvent event)
	{
		System.out.println("system is started");
	}
}

@SpringBootApplication
public class Application
{
	public static void main(String[] args)
	{
		SpringApplication application = new SpringApplication(Application.class);
		application.addListeners(new ApplicationStartup());
		application.run(args);
	}
}


我想在spring容器启动之后,最后执行onApplicationEvent里面的逻辑,
但是,实际上发现没有执行。请问,哪里有问题


ContextStartedEvent 只有在显示调用ConfigurableApplicationContext .start()才会publish,你这种情况应该使用ContextRefreshedEvent事件


汪老师您好,我是一个Android工程师,我现在用Bmob做自己的后台,很想在以后用Java做后台,请问读这本书是不是很合适呀?
0 请登录后投票
   发表时间:2016-04-11  
empireghost 写道
spring boot 应该可以把程序变成war文件,部署到tomcat或jetty上吧?
不使用embed container

以war的形式建立spring boot项目,和常规的web项目一样,可以放置普通的servlet容器中。
0 请登录后投票
   发表时间:2016-04-11  
东风玖哥 写道
wiselyman 写道
string2020 写道
你好,最近用springboot碰到1个问题,想请教一下,谢谢。
spring-boot版本:1.3.3.RELEASE

我写了一个listener,想监听spring容器启动的事件
public final class ApplicationStartup implements ApplicationListener<ContextStartedEvent>
{	
	public void onApplicationEvent(ContextStartedEvent event)
	{
		System.out.println("system is started");
	}
}

@SpringBootApplication
public class Application
{
	public static void main(String[] args)
	{
		SpringApplication application = new SpringApplication(Application.class);
		application.addListeners(new ApplicationStartup());
		application.run(args);
	}
}


我想在spring容器启动之后,最后执行onApplicationEvent里面的逻辑,
但是,实际上发现没有执行。请问,哪里有问题


ContextStartedEvent 只有在显示调用ConfigurableApplicationContext .start()才会publish,你这种情况应该使用ContextRefreshedEvent事件


汪老师您好,我是一个Android工程师,我现在用Bmob做自己的后台,很想在以后用Java做后台,请问读这本书是不是很合适呀?


这本书虽然没有专门说做Android的后台,但是spring boot是适合做后台服务的,主要是提供REST服务,我公司现在的手机端的后台就是使用spring boot架构的,你可以看看spring mvc和spring data rest那几节了解一下。
0 请登录后投票
   发表时间:2016-04-11   最后修改:2016-04-12
string2020 写道
不好意思,我又来了,最近在用spring boot整合mybatis的时候,出了不少问题。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface

spring boot 版本:1.3.3.RELEASE

其中,代码如下:
项目结构如下

package com.pp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

package com.pp.email;
public interface SendService
{

}


package com.pp.email;

import org.springframework.stereotype.Component;

@Component
public class SendServiceImpl implements SendService
{

}

package com.pp.user;

public interface UserManager
{

}


package com.pp.user;

import org.springframework.stereotype.Component;

@Component("userManager")
public class UserManagerImpl implements UserManager
{

}

<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>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<groupId>com.pp</groupId>
	<artifactId>springboot3</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.0.1</version>
		</dependency>
	</dependencies>
</project>

项目没有其他任何的配置文件,所有的配置都是默认的。
然而,启动报错了
报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendService' defined in file [E:\ide\code\src9\springboot3\target\classes\com\pp\email\SendService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at com.pp.App.main(App.java:11) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pp.email.SendService]: Specified class is an interface
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:68) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	... 17 common frames omitted


我如果把com.pp.email包以及包下面的东西都干掉,就没有问题了。
问题是,com.pp.email这个包里面就一个接口,接口的实现类

附件的springboot3.zip是整个工程的源代码,麻烦帮忙看看,谢谢

请问,哪里有问题?
谢谢


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendService' defined in file
提示您将接口注册成bean,我现在电脑不在身边,不方便帮你调试代码,这样的技术细节问题可以和我私聊,就不要在这里讨论了,影响其他的朋友。
另外关注你的整合mybatis,细节的配置方式请仔细阅读http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/,以免导致你现在的错误。
0 请登录后投票
   发表时间:2016-04-12  
Spring Cloud有哪些公司在用呢?开发测试时要注意些什么呢?Spring Cloud对运维是否友好?
0 请登录后投票
   发表时间:2016-04-12  
cnhawkwing 写道
Spring Cloud有哪些公司在用呢?开发测试时要注意些什么呢?Spring Cloud对运维是否友好?

有哪些公司在用我还不是很清楚,spring cloud主要是将单块式的应用分而治之,通过服务发现让这些分开的系统之间交互,每分割出来的部分称之为“微服务”,使用spring cloud开发的应用是常规的java应用,在运维上没有特别之处。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics