Home
Browse
Create
Search
Log in
Sign up
Upgrade to remove ads
Only $2.99/month
Container, Dependency, and IOC
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (52)
What is dependency injection and what are the advantages?
Your object is handed what it needs to work. Objects don't need to resolve dependencies, improves reusability, conceals implementation details of dependencies, improves testing, allows central control over object lifestyle
What is a pattern?
A pattern is a solution to a general problem that software developers faced during software development.
What is an anti-pattern?
An anti pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive
Is dependency injection a pattern?
Yes it is a design pattern
What is an interface and what are the advantages of making use of them in Java?
A way of implementing multiple inheritance (polymorphism), interfaces only contain abstract methods and cannot be instantiated. Advantages include providing different implementations at runtime, the ability to inject dependencies, and polymorphism
Why are interfaces recommended for Spring beans?
They promote ease of dependency injection
What is meant by "application-context?
The context that loads the config (usually an xml). It is a container used for IoC(inversion of control) over beans. Interface, a more advanced container that is a sub-interface of BeansFactory and adds enterprise-specific functionality to it.
What is the concept of a "container" and what is its lifecycle?
The core of spring it manages components that make up an application. Spring container is an "environment" for the beans to live. It creates objects, wires them together, configures them and manages their complete life cycle from creation until destruction.
How are you going to create a new instance of an ApplicationContext?
ApplicationContext context =
SpringApplication.run( ApplicationConfig.class );
Can you describe the lifecycle of a Spring Bean in an ApplicationContext?
Initialization - Spring beans definitions are processed and stored prior to the applicationContext being created. After creation of the context the beans are created and dependencies are injected. The post construct methods are also invoked here.
Use - spring beans are used by the application
Destruction- Application context is closed the pre destroy methods are invoked garbage collecting the beans
How are you going to create an ApplicationContext in an integration test test?
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SystemTestConfig.class)
point to the system test config file
What is the preferred way to close an application context? Does Spring Boot do this for
you?
context.registerShutdownHook(); yes it does it in springApplication.run()
Describe Dependency injection using Java configuration?
One bean method should call another bean method.
@Bean
public Foo foo() {
return new Foo(bar());
}
@Bean
public Bar bar() {
return new Bar();
}
Describe Dependency injection using annotations (@Component, @Autowired)?
@Component marks the class as a Java Bean and Spring picks that up and pulls it into the Application context so that it can be injected into @Autowired instances.
Component scanning
Components are scanned at startup based off the specified classpath or marked with @Component
Stereotypes
When component scanning checks the annotations simply marked with @Component
Meta-annotations
An annotation that can be used to annotate other annotations. Marking interface MyTransactionalService with @Service. You can then implement it by annotating a class with @MyTransactionalService. ComponentScan will pick this up.
Scopes for Spring beans? What is the default scope?
Scopes define how the bean will be used. For example singleton scope will use the same instance of the requested object each time. Common scopes are singleton, prototype, session, and request. The default scope is singleton.
Are beans lazily or eagerly instantiated by default? How do you alter this behavior?
Beans are eagerly instantiated by default. You can ovverride this by marking the bean @Lazy
Eager vs Lazy instantiation
Eager instantiation and by default loads the bean immediately while lazy loads it on demand (when the getter is called for example)
What is a property source? How would you use @PropertySource?
Abstract base class that represents a source of name value property pairs. The annotation lets you define which propertysources are included within a specific environment (@Profile)
What is a BeanFactoryPostProcessor and what is it used for? When is it invoked?
BeanFactoryPostProcessor is an interface that allows for defining customizing BeanDefinitions for future beans before those beans are created. Using BeanFactoryPostProcessor is useful when for example you want to populate class fields from a properties file. It is called before beans are initialized
Why would you define a static @Bean method?
To ensure that the bean is created before any others
What is a ProperySourcesPlaceholderConfigurer used for?
This bean is used for defining the location of the properties file to be used in assigning values to bean properties. But it also allows for searching the required value in the system and/or environment variables.
What is a BeanPostProcessor and how is it different to a BeanFactoryPostProcessor?
What do they do? When are they called?
BeanPostProcessor interface is used for defining a bean that will adjust all beans before the init method and after it. BeanPostProcessor differs from BeanFactoryPostProcessor in the moment it is called. BeanFactoryPostProcessor is called before beans are initialized (works on BeanDefinitions) whilst BeanPostProcessor is called on existing beans.
What is an initialization method and how is it declared on a Spring bean?
Initialization method is a method that does some initialization work after all the properties of the bean were set by the container. It can be declared using the initMethod of @Bean, using @PostConstruct, or implementing InitializingBean and overriding afterPropertiesSet(discouraged). The order the methods are called is @PostConstruct, then afterPropertiesSet, and then init-method.
What is a destroy method, how is it declared and when is it called?
Destroy method is the method that is called when the container containing it is destroyed. We can use the @Bean destroyMethod attribute, the @PreDestroy annotation, or implementing DisposableBean then overriding destroy()(discouraged). @PreDestroy is called when the ConfigurableApplicationContext is closed.
Consider how you enable JSR-250 annotations like @PostConstruct and @PreDestroy? When/how will they get called?
For these annotation to be available you must have the CommonAnnotationBeanPostProcessor registered in context. They are declared with the init or destroy methods and also are called upon instantiation or just before beans are removed from the container.
What is the common usage of Annotation (@PostConstruct/@PreDestroy) vs. @Bean methods (initMethod/destroyMethod) for bean startup and shutdown?
Use Annotation methods for classes that you wrote and have control over. Use @Bean methods for classes that you didn't write and cannot annotate
What do you have to do, if you would like to inject something into a private field? How does
this impact testing?
From Spring point of view it doesn't matter whether you use a private or a public field so you can just use @autowired
What is the behavior of the annotation @Autowired with regards to field injection,
constructor injection and method injection?
@Autowired tries to find a matching bean by type and inject it at the place on annotation - that may be a constructor, a method (not only setter, but usually setter) and field.
How does the @Qualifier annotation complement the use of @Autowired?
@Qualifier allows for labeling beans. Meaning multiple beans can have the same qualifier and in case you are trying to autowire some collection of beans that match that qualifier all of those beans will be passed to the collection.
What is a proxy object and what are the two different types of proxies Spring can create?
Proxy objects are objects that replace beans that should be injected in a singleton bean but are themselves lazily instantiable. The two types are interface proxy and class proxy
What are the limitations of these proxies (per type)?
With Dynamic proxying, the proxied class must implement interfaces. With CGLib proxying the class must provide a default constructor, you cannot use it with final methods, and the resulting proxy is final (meaning you cannot proxy it again)
What is the power of a proxy object?
Proxies are a great means of adding functionality around beans methods. You can define some things to happen at different times around methods but you can also add new methods to beans by declaring new "parents" for the bean using @DeclareParents annotation
Disadvantages of proxies?
. Proxies can only work from the outside, proxied objects must be instantiated by the Spring container(not w new keyword), and proxies are not serializable
What are the advantages of Java Config? What are the limitations?
Advantages are type safety assured by IDEs as well as using someone else's classes. The major limitation is that you cannot dynamically change the config you must rebuild the project
What does the @Bean annotation do?
@Bean annotation is used on methods that will return beans of the specified return type. @Bean is usually used in configuration classes that don't use components scan; but may be used as part of a component
What is the default bean id if you only use @Bean? How can you override this?
In a configuration class the name of the bean in case you use @Bean on some method will instantiate a bean with the name matching the method name. override with @Bean(name="somename")
Why are you not allowed to annotate a final class with @Configuration
Spring requires subclassing classes to be annotated with @Configuration and that uses CGLib proxying. Using CGlib proxying we cannot proxy a final class.
How do @Configuration annotated classes support singleton beans?
It only creates one instance of that bean
Why can't @Bean methods be final either?
Spring requires subclassing classes to be annotated with @Configuration and that uses CGLib proxying. Using CGlib proxying we cannot proxy a final class.
How do you configure profiles?, What are possible use cases where they might be useful?
You use the @Profile annotation and then you activate it by using setActiveProfiles. Profiles are very useful in integrationTests
Can you use @Bean together with @Profile?
Yes, profile can be used on either configuration level or bean level.
Can you use @Component together with @Profile?
Yes, profile can be used on either configuration level or bean level.
How many profiles can you have?
Multiple may be active at once
How do you inject scalar/literal values into Spring beans?
use @Value annotation at constructor arguments, over properties and at setter methods
What is @Value used for?
This annotation is used for assigning some value to a bean field
What is Spring Expression Language (SpEL for short)?
SpEL is an expression language that allows for querying and manipulating an object graph at runtime. @Value("#{otherBean.someField}")
What is the Environment abstraction in Spring?
The environment interface represents spring abstraction for the environment. It deals with profiles and properties from different sources
What can you reference using SpEL?
Beans, system properties, and system variables
What is the difference between $ and # in @Value expressions?
For property placeholders we use $ whilst for SpEL we use #
THIS SET IS OFTEN IN FOLDERS WITH...
Data Management: JDBC, Transactions, JPA, Spring D…
39 terms
Spring Boot
15 terms
Spring MVC and the Web Layer
20 terms
Security
16 terms
YOU MIGHT ALSO LIKE...
Computer Science Chapter 8
50 terms
Chapter9
117 terms
Java Chapter 11
71 terms
C# Interview Questions
50 terms
OTHER SETS BY THIS CREATOR
Testing
8 terms
REST
22 terms
OTHER QUIZLET SETS
U.S. History
19 terms
CLOTH SELF CHECKS
94 terms
Reveiw of classification
15 terms
Geology Exam #2- Geologic Structures (chap 12)
38 terms