HandlerInterceptors vs. Filters in Spring MVC. Also there is no such thing as DispatcherRequestServlet in Spring.
Spring Expression Documentation
@Transactional
annotation at the class level?spring.security.transactions.xml
config file that uses Spring’s transaction implementation and validation code.TransactionBytecodeValidator
class. Default Transaction behavior rolls back on validation exception but commits on proper validationTransactionValidator
class using Spring’s transaction validation code. Default Transaction behavior rolls back on validation exception.@SpringBootApplication
public class App {
public static void main(String args[]) {
SpringApplication.run(App.class, args);
System.out.println("startup");
}
}
public class Print implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("init");
}
}
Explanation: SpringApplication.run method returns the created Context, so main method will continue running and print “startup”. Class Print is not a Spring Bean, because it is not annotated with @Component, so it will not be initialized.
@Component
public class Test implements InitializingBean {
@Autowired
ApplicationContext context;
@Autowired
static SimpleDateFormat formatter;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(context.containsBean("formatter") + " ");
System.out.println(context.getBean("formatter").getClass());
System.out.println(formatter.getClass());
System.out.println(context.getClass());
}
}
@Configuration
class TestConfig {
@Bean
public SimpleDateFormat formatter() {
return new SimpleDateFormat();
}
}
@RestController
public class SampleController {
@RequestMapping("/map")
public String map(@RequestParam("bar") String foo, @RequestParam("foo") String bar) {
return bar + foo;
}
}
Overview and Need for DelegatingFilterProxy in Spring
@ContextConfiguration Example in Spring Test
____
.my.property=Test
@Prop("${my.property}")
private String val;
@GetVal("my.property")
private String val;
@GetProperty("${my.property}")
private String val;
@Value("${my.property}")
private String val;
@Component
public class Test implements InitializingBean {
@Autowired
ApplicationContext context;
private TestService service;
public void setService(TestService service) {
this.service = service;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.print(context.containsBean("testService") + " ");
System.out.println(service.getClass());
}
}
@Service
class TestService {}
Explanation: missing @Autowired
on private TestService service
or on the setter
_
in Spring @Configuration.Explanation: sha-1 is not considered secure anymore: https://en.wikipedia.org/wiki/SHA-1#Attacks . With bcrypt you can select more complex hashes https://en.wikipedia.org/wiki/Bcrypt
@target(com.linkedin.annotation.Loggable)
Difference between @target and @within (Spring AOP)
@Component
public class Test implements InitializingBean {
@Autowired
ApplicationContext context;
@Autowired
SimpleDateFormat formatter;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(context.containsBean("formatter"));
System.out.println(formatter.getClass());
}
}
@Configuration
class TestConfig2 {
@Bean
public final SimpleDateFormat formatter() {
return new SimpleDateFormat();
}
}
Explanation: @Bean
-method in @Configuration
must be overridable. Remove the final
keyword to fix.
POST localhost:8080/map
{"b" : "b", "d" : "d"}
@RestController
public class SampleController {
@RequestMapping("/map")
public String map(@RequestBody SampleObject sampleObject) {
return sampleObject.getB() + sampleObject.getC();
}
}
public class SampleObject {
String b;
String c;
public String getB() { return b; }
public void setB() { this.b = b; }
public String getC() { return c; }
public void setC() { this.c = c; }
}
@SpringBootApplication
public class Question14 {
@Autowired
private static Service service;
public static void main(String[] args) {
SpringApplication.run(Question14.class, args);
}
}
@Component
class Service {}
ThreadLocal
in an Authentication
object.@Controller
. Then, using a specific naming convention for the methods, the RequestMappingHandlerAdapter
will automatically configure your endpoints with the proper HTTP verb and URI.RequestMappingHandlerAdapter
will automatically configure your endpoints based on values from the YAML config file.@RequestMapping
, or a HTTP verb-specific annotation with a String URI pattern parameter (and other params as needed), which is supported through a RequestMappingHandlerMapping/Adapter
.Spring RequestMapping. Spring does not use naming conventions for web requests (unlike e.g. for the Data Repositories)
execution(* setter*(..))
within(com.linkedin.service..*)
/shutdown
Reason: By default, all the endpoints are enabled in Spring Boot Application except /shutdown; this is, naturally, part of the Actuator endpoints.
@GetMapping("api/v1/domain/resource/{id}")
public Pojo getPojo(@PathVariable("id") String id) {
return testService.getPojo(id);
}
execution(* com.linkedin.TestService.*(..))
[Explanation] There are many key differences between constructor injection and setter injection.
Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only. Overriding: Setter injection overrides the constructor injection. If we use both constructor and setter injection, IOC container will use the setter injection. Changes: We can easily change the value by setter injection. It doesn’t create a new bean instance always like constructor. So setter injection is flexible than constructor injection.
@Component
public class Test implements InitializingBean {
@Autowired
ApplicationContext context;
@Autowired
static SimpleDateFormat formatter;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(context.containsBean("formatter") + " ");
System.out.println(context.getBean("formatter").getClass());
System.out.println(formatter.getClass());
System.out.println(context.getClass());
}
}
@Configuration
class TestConfig {
@Bean
public SimpleDateFormat formatter() {
return new SimpleDateFormat();
}
}
Explanation: Here only one line can throw NPE. Calling getClass() from context.getBean(“formatter”) can potentially throw NPE if context.getBean(“formatter”) will return null.
- A. Creating SQL queries
- B. Logging
- C. Filtering, sorting and transforming data
- D. Transaction management
- E. Audit logging
- F. Business logic
@SpringBootApplication
public class App {
@Autowired
Service service;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@Primary
@Component
class Service2Impl implements Service {
Service2Impl() {
System.out.println("Service2");
}
}
@Component("Service")
class Service1Impl implements Service {
Service1Impl() {
System.out.println("Service1");
}
}
interface Service{}
execution(* com.linkedin.service..*.*(..))