spring-小技巧-2
##
使用1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// @ConditionalOnBean 在当前上下文中存在某个对象时,才会实例化一个Bean
// @ConditionalOnClass // 某个class位于类路径上,才会实例化一个Bean
// @ConditionalOnCloudPlatform
// @ConditionalOnExpression
// @ConditionalOnJava
// @ConditionalOnJndi
// @ConditionalOnMissingBean 在当前上下文中不存在某个对象时,才会实例化一个Bean
// @ConditionalOnMissingClass 某个class类路径上不存在的时候,才会实例化一个Bea
// @ConditionalOnNotWebApplication
// @ConditionalOnProperty
// @ConditionalOnResource
// @ConditionalOnSingleCandidate
// @ConditionalOnWebApplication
// 设置默认false,这样默认不会实例化这个bean,当表达式为true的时候,才会实例化一个Bean
"${aidijing.filter.injection-attack-filter.enabled:false}" ) (
public InjectionAttackFilter injectionAttackFilter () {
return new InjectionAttackFilter();
}
spring boot Remote debug tunnel
http://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/
20.5.3 Remote debug tunnel
Java remote debugging is useful when diagnosing issues on a remote application. Unfortunately, it’s not always possible to enable remote debugging when your application is deployed outside of your data center. Remote debugging can also be tricky to setup if you are using a container based technology such as Docker.
To help work around these limitations, devtools supports tunneling of remote debug traffic over HTTP. The remote client provides a local server on port 8000 that you can attach a remote debugger to. Once a connection is established, debug traffic is sent over HTTP to the remote application. You can use the spring.devtools.remote.debug.local-port property if you want to use a different port.
You’ll need to ensure that your remote application is started with remote debugging enabled. Often this can be achieved by configuring JAVA_OPTS. For example, with Cloud Foundry you can add the following to your manifest.yml:
env:
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"
[Tip]
Notice that you don’t need to pass an address=NNNN option to -Xrunjdwp. If omitted Java will simply pick a random free port.
[Note]
Debugging a remote service over the Internet can be slow and you might need to increase timeouts in your IDE. For example, in Eclipse you can select Java → Debug from Preferences… and change the Debugger timeout (ms) to a more suitable value (60000 works well in most situations).
[Warning]
When using the remote debug tunnel with IntelliJ IDEA, all breakpoints must be configured to suspend the thread rather than the VM. By default, breakpoints in IntelliJ IDEA suspend the entire VM rather than only suspending the thread that hit the breakpoint. This has the unwanted side-effect of suspending the thread that manages the remote debug tunnel, causing your debugging session to freeze. When using the remote debug tunnel with IntelliJ IDEA, all breakpoints should be configured to suspend the thread rather than the VM. Please see IDEA-165769 for further details.
运行时得到端口
73.4 Discover the HTTP port at runtime
You can access the port the server is running on from log output or from the EmbeddedWebApplicationContext via its EmbeddedServletContainer. The best way to get that and be sure that it has initialized is to add a @Bean of type ApplicationListener
Tests that use @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) can also inject the actual port into a field using the @LocalServerPort annotation. For example:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyWebIntegrationTests {
@Autowired
EmbeddedWebApplicationContext server;
@LocalServerPort
int port;
// ...
}