path | title |
---|---|
/learnings/java_spring |
Learnings: Java: Spring |
- Spring Boot
- Actuators >
- Reactive Spring Stack / Spring Web Flux
- using Pivotal Reactor
- Profiles
- Mutliple qualifiers on beans
- >
- Spring Components
- Spring Core Container notes
- Spring Cloud Stream
- HTTP Requests With Spring
- Book Recommendations
- Spring-boot-cli (port availiable, installs spring program name)
- IntrlliJ: Spring Intializer in new project window
If you want to prototype something out with Groovy , you can use the spring CLI , Groovy and grab to even faster boot up a Spring app (package with spring jar command and sub command )
/mapping gives you the URL route, which bean, which method is called for the route/ method. Thanks Spring Actualizer
Can use CRaSH / spring-boot-starter-remote-shell the get CLI
Launch app and use SSH to connect to port number it tells you
Can run bean inspection and anything else you would be able to do through the endpoints (either mappings, health, etc rtc)
Local host:8180 in a web client
https://github.com/codecentric/spring-boot-admin
Can secure spring actuator routes with Spring Security
true <---- does that Bash thing where it prefixes the jar file with some Bash script (useful if you're not wrapping with Baliista or Docker or something....) <--- so can put this in /etc/services (??)
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
Enable:
- include
spring-boot-starter-actuator
to maven - OR
spring-boot-actuator
<-- (but maybe need some setup here??????)
See also:
- Learning_Ops_Java_Spring
Set Spring Actuator to a different port with property value like so:
management.port: 9001
management.context_path=/spring
<-- by default is just /
(can use spring security to lock these down!!)
Describes every bean in context and every bean those beans are wired up with
Environment properties availiable to the app:
- env variables
- JVM properties
- command line params
- application.properties properties
any property name that ends with password
or secret
or key
is auto starred out
Gives list of following built in metrics:
- GC information
- Memory
- Heap
- Class Loader
- System (processor.uptime, instance.uptime, systemload.average)
- Thread Pool
- Tomcat session info
- HTTP
- etc etc
Can fetch only one metric by adding it to the URL: /metrics/gc.free <-- or whatever
^^^ health response information can be customized: beans that implement HealthIndicator
interface
many built in HealthIndicators: JmsHealthIndicator, #{datastore}HealthIndicators
, MailHealthIndicator etc etc
(auto configured / conditional beans on the availability / use of the Spring Boot data store in question)
If management.port
is set, the following is true:
/health
is exposed on management.port- it contains sensitive information (ie disk space)
If only server.port
is set (read: no management.port
), the following is true:
/health
is exposed on server.port <-- if management.security.enabled=false this will NOT contain sensitive information
There's a ReactiveHealthIndicator
, and ReactiveHealthRegistry
See also:
<<Java_Spring_Boot_JMX>>
All actuator endpoints storied in org.springframework.boot
domain.
See also:
- Learinng_Ops_Java_JMX
Property Name | Description / Notes |
---|---|
management.security.enabled | NOTE: do NOT turn this off without a management port: your actuators etc are then EXPOSED TO PUBLIC INTERNET!!! |
management.port | explicitly specify where management.port is (usually server.port + 1) |
Spring WebFlux over Spring MVC ^^^ both clients and server. So now have reactive web client.
This means it wouldn't be hard AT ALL to turn your reactive spring controller into a stream based API, instead of one-and-done.
Controllers should return reactive container types:
Mono<>
object <-- serialize this one thing before sending upSingle<>
object <-- same asMono<>
, but RxJava compatible typeFlux<>
object <-- good for streaming requestsObservable<>
object <-- same asFlux<>
, but RxJava compatible types
See also:
- Learning_Java_Rx
SpringMVC needs to know how long the stream should go on. Use the content type header for this: application/json
for example is finite, a streaming video is "infinite". Set this via `@produces(
Uses http://projectreactor.io/ (Also powers a Clojure library and has wide support from major players)
#terms
Deosn't publish until subscriber connected When connected, sub gets all events from beginning
Published instant flux created New subs just get most recent data
(Spring 3.1 feature)
To select which implementation of a bean you want in which environment
@Configuration
@Profile("dev")
public class DevConfig() {
@Bean
public String EnvName() {
return "dev";
}
}
@Configuration
@Profile("production")
public class ProdConfig() {
@Bean
public String EnvName() {
return( "production - build {}".... )
}
}
Control active profile:
- context parameters on web service
- JNDI entries
- environmental variables
- set
spring.profiles.active
property - set
spring.profiles.default
property - set
@ActiveProfile
annotation on integration test case
Can not do this - must create annotation for additional qualifiers
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface MyOrderQualifier { }
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface MyUrlQualifier { }
See:
IF TESTING NON SPRING-BOOT SPRING:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
<version>4.2.7.RELEASE</version>
</dependency>
IF TESTING SPRING BOOT:
`spring-boot-starter-test`
-
MockMvc <-- create HTTP "requests" that are processed by test controller
-
MockMvcBuilders <--- builds you MockMvc instances
-
MockMvcRequestBuilders <-- builds you HTTP "requests" that are sent to the tested controller
- MockHttpServletRequestBuilder <--- simple requests
- MockMultipartHttpServletRequestBuilder <--- multipart requests
-
MockMvcResultHandlers <--- provides asserts etc for checking HTTP response from controller
If we want to use the web application context based configuration, we have to run our unit tests by using the SpringJUnit4ClassRunner class.
Lesson Link: https://www.testwithspring.com/lesson/configuring-the-system-under-test/
@Category(UnitTest.class)
public class HelloControllerTest {
private MockMvc mockMvc;
@Before
public void configureSystemUnderTest() {
mockMvc = MockMvcBuilders.standaloneSetup(new HelloController())
.build();
}
}
standaloneSetup will register one or more @Controller instances and configure the Spring MVC infrastructure programmatically
See StandaloneMockMvcBuilder
to specify intercepters, placeholderValues, locale resolvers etc etc
but probably should be minimal with these
I configure the used ExceptionResolver if my application has error views that are rendered when a controller method throws an exception. I specify the used LocaleResolver if a Locale object is injected into a method of the tested controller as a method parameter. I specify the used ViewResolver if I don’t want that my unit tests use the InternalViewResolver that is used by the Spring MVC Test framework if no view resolver is configured.
See ~/Writing/whitepapers/SpringJustDependencyInjection.ooutline
- AoC
- Spring Security
- Data Access
- Convention over Configuration
- IoC
-
Spring Test:
-
Sub Components:
-
Important Classes / Annotations:
- @ContextConfiguration < --- does NOT set up logging or properties file
- @MockEnvironment
- @MockPropertySource
- @ContextHierarchy
- @TestPropertySource
- ApplicationContextAware superclass "an alternative is to inject the application context into your test class via @Autowired..."
- ReflectionTestUtils
-
With Spring Boot: "https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html"
- @MockBean
- @SpringBootTest "technically in springframework.boot.test.context < -- sets up logging, config etc etc"
-
Takes class name and lower cases first letter: TeamPlayer becomes teamPlayer
@ComponentScan(base packages={"com.what", "com.where"})
Abstracts over streaming / logging solutions
DSL for stream topologies Scdf shell CLI tool
Sec/test/resources/contracts GROOVY scripts / DSL that specify request and response in groovy format
Built on netty. reactor