Font size
WorksheetsSpring Final Exam Worksheet Extraction
Total questions: 60
Worksheet time: 30mins
In a Spring XML configuration file, how would you define a bean definition for the class EmployeeServiceImpl where it has multiple aliases (i.e., names)?
<bean class=”EmployeeServiceImpl”/>
<bean id=”myID” name=”another, andAnother” class=”EmployeeServiceImpl”/>
<bean id=”myID” class=”EmployeeServiceImpl”/>
<bean id=”myID another, andAnother” class=”EmployeeServiceImpl”/>
You have an EmployeeService bean that has a three-argument constructor: public EmployeeService(String s, Integer i, Boolean b). Using XML, how would you configure this bean?
All the above
<bean id="emplService class="EmployeeServiceImpl" >
<constructor-arg index="2" value="true"/>
<constructor-arg index="0" value="hello"/>
<constructor-arg value=“3" />
</bean>
<bean id="emplService class="EmployeeServiceImpl" >
<constructor-arg name="b' value="true"/>
<constructor-arg value="hello"/>
<constructor-arg type="int" value=“3" />
</bean>
<bean id="emplService class="EmployeeServiceImpl" >
<constructor-arg name="b' value="true"/>
<constructor-arg index="0" value="hello"/>
<constructor-arg value=“3" />
</bean>
What are key advantages of using the Spring framework?
All of the above
Promotes decoupling and reusability
Code is coupled to Spring
Object dependencies are clearly defined in the application code itself
What are we defining here? A principle where the control of a program, i.e., delegations from one object to another, are inverted: instead of the programmer controlling the flow in code, external sources (framework, services, configuration resources) take control of it.
Inversion of control
Inheritance
Code to the interface
Dependency injection
What are we defining here? A design pattern where class X is dependent on Y. So rather than creating an object of Y within class X, we can inject the dependencies via a constructor or setter injection.
Dependency injection
Inheritance
Inversion of control
Code to the interface
What types of dependency injection are there in XML configurations?
Constructor
Method injection
Setter
Field
ApplicationContext implementations have what type of built-in class type to read metadata?
JSON Parser
Resource
InputStreamReader
FileReader
In an XML configuration file, what bean tag attribute do you use to ensure that the bean definition creates different managed bean instances per request?
it’s the default there is no need to add an attribute
scope=request
scope=prototype
scope=singleton
Constructor argument resolution in an XML file can be achieved by; pick all that apply.
Type
Index
Constructor name
Order within type
XML configuration file tags and grammar is controlled by; pick all that apply.
Java jars
Schemas
Namespaces
Java code
You are to define a bean definition getServiceBean() in a Java configuration class for EmployeeServiceImpl with a name of "emplService" and a public initializer method of "init". Which is a correct approach to doing this?
@Bean(name="emplService") and execute the init() method inside the Bean definition method itself before returning the service bean
All of the above
@Bean(name="emplService") and annotate method "init" in EmployeeServiceImpl with @PostConstruct
@Bean(name="emplService", init-method="init")
Using annotations, how could you inject a bean that implements the interface EmployeeDao, whose id is "emplDao", into a Spring-managed bean of "employeeService"? Assume that there are multiple beans that implement EmployeeDao in your application and you are annotating the property employeeDaoImpl (field).
@Value("emplDao")
@Autowired (name="emplDao")
@Resource(name="emplDao")
@Named("emplDao")
You are writing a JUnit test class for your application using a Java configuration class AppConfig, and it is also to have an Initializer (the class MyInitializer) where you have tailored system properties for use in the test. Which is the annotation approach that you will take?
@ExtendWith(SpringExtension.class) and @ContextConfiguration(classes=AppConfig.class)
@ExtendWith(SpringExtension.class) and @ContextConfiguration(locations=AppConfig.class, initializer=MyInitializer.class)
@ExtendWith(SpringExtension.class) and @ContextConfiguration(classes=AppConfig.class, initializer=MyInitializer.class)
@ExtendWith(SpringExtension.class) and @ContextConfiguration(classes=AppConfig.class, initializer="MyInitializer")
Which of the following is false about JSR 250 @Resource annotation?
Can be used on constructors
Can only be used on setters
Is not a Spring Annotation
Does the same as @Autowired as it can try to resolve a bean by-type
We mentioned that after setter injection in a bean lifecycle, we can define an initializer by implementing what interface?
ApplicationContextAware
DisposableBean
InitializingBean
BeanPostProcessor
What were the motivations behind Java Configurations; pick all that apply.
Externalize the configuration
Compile time checking
Get rid of XML
Type Safety
What annotation can we use for simple value injection.
@Component
@Value
@Configuration
@Scope
What tag from the context namespace do you use to register the BeanPostProcessors that scan for annotations or spring managed bean.
property
component-scan
annotation-config
bean
Which annotation do you use @Qualifier with.
@PostConstruct
@Resource
@Autowired
@Inject
What Design Pattern does the Spring DispatcherServlet implement.
FrontController
Proxy
Model
Model View Controller
What are Spring Boot Starter dependencies? Select all that apply.
Application dependencies that “drag” in the actual jars for the Spring Boot Application
They build and define the starter project structure for a Spring Boot Application
They contain no code of their own but are virtual dependencies
The core dependencies that provide the basis of a Spring Boot Application
Under which directory would you find configuration files in a Spring Boot Project using all default configurations.
src/main/resources/static
src/main/resources/js
src/main/resources/template
src/main/resources/config
What annotations does the single annotation @SpringBootApplication on a SpringBootApplication runnable class replace to automatically configure a WebApplicationContext including the DispatcherServlet and auto scanning for Controllers? Choose the three that apply.
@Autowired
@Configuration
@ComponentScan
@EnableAutoConfiguration
Why are @ConfigurationProperties annotated Java classes preferred over @Value (“${…}”) when it comes to injecting items in from the Spring Environment? Select all that apply.
Can populate complex objects
Validated
Type Safe
Just injecting into a Controller as a Java Dependency i.e. the class and not a Spring annotation dependency
What annotation must you place upon a class to be picked up by Spring Boot package-based component scan to be part of the DispatcherServlet HandlerMappings matching URL to method in such a class.
@GetMapping
@SpringBootApplication
@RestController
@Configuration
What annotation must you place upon a method in a RestController class to be tied to an HTTP GET.
@Configuration
@SpringBootApplication
@GetMapping
@RestController
What annotations does the @SpringBootApplication annotation replace.
@ComponentScan
@EnableAutoConfiguration
@SpringBootConfiguration
@RestController
What notation format can we use to define parent–child relationships in configuration files to populate the Spring Environment object.
Properties i.e. key value pairs
Java
Yaml
text
Spring MVC uses what to invoke RestController methods and convert request parameters and response payloads through provided converters.
HandlerMappings
HandlerAdapters
ViewResolvers
ExceptionHandlers
How would you inject into a Controller method an optional parameter from the request, i.e. url? id=55 where the id parameter may or may not be present.
Annotate the method argument with @RequestParam("id") int id
Annotate the method argument with @PathVariable("id") Optional<Integer> optional
Annotate the method argument with @RequestParam("id") Optional<Integer> optional
Annotate the method argument with @PathVariable("id") int id
When accessing a REST service at url /abc with an HTTP GET, using RestTemplate, what method would you use to get at the response body immediately.
String string = new RestTemplate().getForObject(abc, String.class);
ResponseEntity
ResponseEntity
String string = new RestTemplate().exchange/abc, null, String.class);
What attribute would you add to a @GetMapping("/") annotated RestController method to return XML or JSON.
@GetMapping("/")
@GetMapping(path="/", produces=MediaType.APPLICATION_XML)
@GetMapping(path="/", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@GetMapping(path="/", consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
How can you extract the header "user-agent" out of the request in a RestController method, to be available in that method for processing.
@RequestHeader("user-agent") String agent
@PathVariable("user-agent") String agent
@RequestParam("user-agent") Optional<String> optional)
@RequestHeader("user-agent") Optional<String> optional)
How can you extract the request payload and inject it into a RestController method for processing
@RequestBody
@RequestParam
@Pathvariable
@Header
Who creates the Model object
HandlerAdapter
DispatcherServlet
ViewResolver
HandlerMappings
Attributes in the Model are passed through to the request and then that object is dispatched to the selected view. If I place in the Model an attribute i.e. model.addAttribute("message", "hello"), how can I access that in a Thymeleaf web page
${message}
${model.getHello()}
${hello}
message
How would I define the port of a Spring boot application if I did not want the default 8080 but 8081
In the application.yaml file enter the key value pair of port=8081
Place the attribute "port" the @Controller annotation
In the application.properties file enter the key value pair of server.servlet.context-path=8081
In the application.properties file enter the key value pair of server.port=8081
What is the @RequestMapping annotation used for
It identifies what the payload is for a Request
It defines the Response types from a Controller
It defines the Url relationship between a Controller and an HTTP Request and can be placed at the Class level or method level.
It annotates the class as a Controller
How does Spring Boot wire up the WebApplicationContext components (select all that apply)
The WebApplicationContext components are dragged into your application via Spring boot Starters in the pom.xml
It has a series of opinionated defaults that without further enhancement will render a WebApplicationContext
You can override these defaults
You have to code up the dependencies yourself
Using Annotations, how could you annotate a JPA EntityManager of a Spring managed bean to provide Transactionally based interactions with a database?
@PersistenceContext
@PersistenceUnit
@Inject
@Value("#{entityManager}")
How would you provide an implementation of the Spring data JpaRepository to? inject into your Spring managed beans? Pick all that apply.
Create a class that extends JpaRsepository and provide implementations of its methods
Provide a class that extends EntityManager an inherit its methods
Create an interface that extends JpaRepository
Provide the maven dependency for spring-boot-starter-data-jpa in your pom
How do you Annotate a Repository to be a Restful service itself where json collections is wrapped in the element "course" and its url is "/courses"?
@RepositoryRestResource(collectionResourceRel = "course")
@RestController
@RepositoryRestResource(collectionResourceRel = "course", path = "courses")
@RepositoryRestResource
How would you define a query method in a JpaRepository
Collection
Collection
Collection
Collection
What annotation defines a class as a subset of a JPA Entity Resource when exposing a JpaRepository through REST
@Projection
@Component
@Controller
@Entity
In Aspect Oriented Programming using Spring, what is a Joinpoint?
A class that implements enterprise application concerns that cut across multiple classes
Defines an expression that represents and entry point into the application
Defines additional behavior to apply when a method is executed in the application
Defines when an Advice should be applied, such as a method invocation
When would you use an @Around advice? Select all that apply.
To change the return object from a target bean
To get at the target method arguments
To catch and log Exceptions and force a throw of a more general Exception
To block access to a target bean
Which Advice would you use to always log the return from a target bean method execution.
@AfterReturning
@After
@Before
@AfterThrowing
How would you annotate a method to Guarantee that it will run in its own transaction and rollback on a CheckedException
@Transactional
@Transactional(noRollbackFor = CheckedException.class, propagation =Propagation.REQUIRES_NEW)
@Transactional(propagation= REQUIRED)
@Transactional(noRollbackFor = CheckedException.class, propagation =Propagation.REQUIRED)
What are cross-cutting concerns in Spring AOP
Behavior which we want to have in a core business modules of an application
Behavior that is External to the application
Behavior in one class only
Behavior which is applicable throughout the application
What does the yaml configuration property in a eureka Service, eureka.instance.leaseExpirationDurationInSeconds define?
Eureka is to advertise the IP addresses of services rather than their hostnames.
Removes, a service from the Eureka Registry after the Eureka Service has not received 3 consecutive missing heartbeats of the desired number of seconds.
The host name of the Eureka Service.
The period between heart beats from registered services to the Eureka instance to inform Eureka that the service is still available.
Which annotation enables you to register a service with a running Eureka Service?
@EnableDiscoveryClient
@EnableEurekaServer
@RibbonClient
@EnableZuulProxy
Which annotation enables you to launch a running Eureka Service, ready to accept registration?
@EnableEurekaServer
@EnableDiscoveryClient
@RibbonClient
@EnableZuulProxy
Which of the following is not an independent objective of a Microservice architecture?
Deployable
Scalable
A monolithic executable component
Replaceable
Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through auto configuration for binding to the Spring Environment. What org.springframework.cloud artifact enables this to happen?
spring-cloud-starter-feign
spring-cloud-starter-hystrix
spring-cloud-starter-ribbon
spring-cloud-starter-eureka-server
Which of the following best describes Service Discovery?
Maximize the availability of a service across multiple servers.
The client is responsible for determining network locations by querying a service registry.
A service that controls the flow of scaled service instances.
Prevent an application from repeatedly trying to execute an operation that's likely to fail.
Which type of Service in Netflix OSS is used to provide client side load balancing for services to delegate to other services?
Ribbon
Feign
Zuul
Eureka
What type of filters can a Zuul service provide? (Select all that apply)
EXCEPTION
LOGGING
ROUTING
PRE
Which Netflix OSS project provides functionality for stopping cascade failures across services by implementing the circuit Breaker Pattern?
Hystrix
Zuul
Ribbon
Feign
Which Netflix OSS project provides functionality for Routing services?
Hystrix
Feign
Zuul
Ribbon
Which of these annotations are JSR330
@Override
@Inject
@Resource
@Autowired
