Spring/boot Flashcards
run java application from command line with maven
mvn clean compile exec:java
how to view the final created pom file that contains the pom and any parent poms and generated config
mvn help:effective-pom
can you have an empty dependencies body in the pom.xml?
yes
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
I didn’t have a class with ‘@SpringBootApplication’ annotated before it
the proper way to run from command line a pom.xml file downloaded from spring initializr
./mvnw spring-boot:run or mvn spring-boot:run
if you want to run a freshly download spring initialzr project using mvn clean compile exec:java what do you need to do?
mvn -X exec:java -Dstart-class=com.example.demo.DemoApplication
@RestController annotation
marks the class as a controller where every method returns a domain object instead of a view. It is shorthand for including both @Controller and @ResponseBody.
@SpringBootApplication is a convenience annotation that adds:
@Configuration, @EnableAutoConfiguration, @ComponentScan
@Configuration,
Tags the class as a source of bean definitions for the application context.
the 2 things to run a project with gradle
1) you have to have set it up with a build.gradle file, e.g. thru the Spring Initializer 2) use ./gradlew bootRun
@EnableAutoConfiguration,
Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
@ComponentScan
Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.
JavaBeans
classes that encapsulate many objects into a single object (the bean)
beans in Spring
The objects that form the backbone of your application and that are managed by the Spring IoC container [ https://www.tutorialspoint.com/spring/spring_bean_definition.htm]
How to build jar file using gradle
./gradlew build
run spring with gradle on another port - method 1
add following to src/main/java/resources/application.yml: server:\nport: 8081
run spring with gradle on another port - method 2
add following to src/main/java/resources/application.properties: server.port=8081
POJO
Plain Old Java Object
Baeldung’s definition of IoC
is a process in which an object defines its dependencies without creating them
In IoC, objects delegate the job of constructing dependencies to
an IoC container
JPA
Java Persistence API. Mechanism to interface with databases
how to create a new Spring project
spring init –dependencies=web,data-jpa my-project
how to create a new Spring project with gradle
spring init –dependencies=web,data-jpa –build=gradle my-project
@Data annotation
from “import lombok.Data;” @Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull, in order to ensure the field is never null.
why wasn’t my Junit test running?
I didn’t have the @Test annotation beforehand
Why weren’t my console logs working in my unit test?
I needed to add the following to the bottom of my build.gradle:
test {
testLogging.showStandardStreams = true
}