Skip to content

Commit dc7d8c6

Browse files
committed
IOC Questions: question 1 to question 7
0 parents  commit dc7d8c6

21 files changed

+279
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
Spring5Certification.iml

lib/javax.annotation.jar

7.53 KB
Binary file not shown.

lib/javax.ejb.jar

46.5 KB
Binary file not shown.

lib/javax.jms.jar

25.3 KB
Binary file not shown.

lib/javax.persistence.jar

127 KB
Binary file not shown.

lib/javax.resource.jar

43.5 KB
Binary file not shown.

lib/javax.servlet.jar

68.3 KB
Binary file not shown.

lib/javax.servlet.jsp.jar

77 KB
Binary file not shown.

lib/javax.servlet.jsp.jstl.jar

27.5 KB
Binary file not shown.

lib/javax.transaction.jar

9.49 KB
Binary file not shown.

pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>java.certification.spring</groupId>
8+
<artifactId>springcertification</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
13+
<dependencies>
14+
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
<version>LATEST</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context</artifactId>
24+
<version>LATEST</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.projectlombok</groupId>
28+
<artifactId>lombok</artifactId>
29+
<version>1.18.4</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-test</artifactId>
34+
<version>LATEST</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-web</artifactId>
39+
<version>5.1.7.RELEASE</version>
40+
</dependency>
41+
42+
</dependencies>
43+
44+
</project>

src/main/java/ioc/commun/Person.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ioc.commun;
2+
import lombok.*;
3+
4+
5+
@Builder
6+
@Setter
7+
@Getter
8+
@EqualsAndHashCode
9+
@ToString
10+
public class Person {
11+
12+
private String name;
13+
private int age;
14+
private Profession profession;
15+
16+
17+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ioc.commun;
2+
3+
4+
import lombok.*;
5+
6+
@Builder
7+
@Setter
8+
@Getter
9+
@EqualsAndHashCode
10+
@ToString
11+
public class Profession {
12+
private String name;
13+
private String description;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ioc.question_001;
2+
3+
4+
/**
5+
* What is dependency injection and what are the advantages?
6+
*
7+
* Object declare their dependencies, other object(Spring container) create and inject those dependencies.
8+
* Several ways:
9+
* 1- constructor injection(mandatory)
10+
* 2- setter method injection
11+
* 3- field injection(the worst way)
12+
*
13+
* Advantages:
14+
* 1-Loose coupling(between the object and its dependency)
15+
* 2-coherent code
16+
* 3-clean code(object creation is externalized)
17+
* 4-easy testing(using mocked object)
18+
*
19+
* Disadvantage
20+
* more classes and interfaces
21+
*/
22+
public class DependencyInjection {
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ioc.question_003;
2+
3+
4+
import org.springframework.beans.factory.DisposableBean;
5+
import org.springframework.beans.factory.InitializingBean;
6+
import org.springframework.beans.factory.config.*;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
10+
/**
11+
* What is meant by application-context?
12+
*
13+
*
14+
* An application context is an advanced ioc container:
15+
* -Bean factory.
16+
* -Resource loader.
17+
* -Event publishing to registered listener.
18+
* -Internationalisation.
19+
*
20+
* The life cycle of the application context:
21+
* 1-Spring IOC is instantiated.
22+
* 2-Configuration bean metadata is loaded to the container.
23+
* 3-Bean definition are created from the loaded metadata.
24+
* 4-Bean definition are adjusted by the {@link BeanFactoryPostProcessor} (example {@link PropertySourcesPlaceholderConfigurer}).
25+
* 5-Bean are created, property set.
26+
* 6-{@link BeanPostProcessor#postProcessBeforeInitialization(Object, String)} are invoked.
27+
* 7-methods of Aware interface are called.
28+
* 8-{@link javax.annotation.PostConstruct},{@link InitializingBean#afterPropertiesSet()}, {@link Bean#initMethod()} are called.
29+
* 9-{@link BeanPostProcessor#postProcessAfterInitialization(Object, String)} are invoked.
30+
* 10-Applicatiuon is running.
31+
* 11-Applicatiuon stop.
32+
* 12-Spring Container is closed.
33+
* 13-{@link javax.annotation.PreDestroy},{@link DisposableBean#destroy()}, {@link Bean#destroyMethod()} are called.
34+
*
35+
*/
36+
public class SpringContainerLifeCycle {
37+
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ioc.question_004;
2+
3+
import ioc.commun.Person;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.context.support.ClassPathXmlApplicationContext;
7+
8+
/**
9+
* This class create a standalone application context using the annotation config way.
10+
* @todo We can use other ways like:
11+
* {@link ClassPathXmlApplicationContext}
12+
*
13+
*/
14+
15+
public class AppContextCreation {
16+
17+
18+
public static void main( String[] args ) {
19+
ApplicationContext apContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
20+
final Object pesrson = apContext.getBean("person");
21+
System.out.println(pesrson);
22+
23+
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ioc.question_004;
2+
3+
4+
import ioc.commun.Person;
5+
import ioc.commun.Profession;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class MyConfiguration {
11+
12+
@Bean
13+
public Profession profession(){
14+
return Profession.builder()
15+
.name("professeur")
16+
.description("professeur a l'universite")
17+
.build();
18+
}
19+
20+
@Bean
21+
public Person person(){
22+
return Person.builder()
23+
.name("wijsen")
24+
.age(52)
25+
.profession(profession())
26+
.build();
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ioc.question_005;
2+
import ioc.question_003.*;
3+
4+
5+
/**
6+
* Can you describe the lifecycle of a Spring Bean in an ApplicationContext?
7+
*
8+
* 1-Bean instance is created.
9+
* 2-Bean Property are set.
10+
* 3-please refer to the question_003 {@link SpringContainerLifeCycle} for the steps that follows.
11+
*/
12+
public class BeanLifeCycle {
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ioc.question_006;
2+
import org.springframework.test.context.ContextConfiguration;
3+
import org.springframework.test.context.web.WebAppConfiguration;
4+
5+
6+
/**
7+
* How are you going to create an ApplicationContext in an integration test?
8+
*
9+
*
10+
* StandAlone Application: Application Context in an integration test is loaded by {@link ContextConfiguration} annotation
11+
* WebApplication : {@link ContextConfiguration } annotation along with {@link WebAppConfiguration} .
12+
*/
13+
public class MyApplicationContext {
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ioc.question_007;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.ConfigurableApplicationContext;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.context.support.ClassPathXmlApplicationContext;
7+
import org.springframework.web.context.ContextLoaderListener;
8+
import javax.servlet.*;
9+
10+
/**
11+
* What is the preferred way to close an application context?
12+
* StandAlone application: registering a shutdownhook using {@link ConfigurableApplicationContext}.
13+
* WebApplication: automaticaly by the {@link ContextLoaderListener#contextDestroyed(javax.servlet.ServletContextEvent)}.
14+
*
15+
* Spring Boot closes stand-alone and web applications in the same way, but manual registering of the shutdown hook isn't
16+
* required for stand-alone applications
17+
*/
18+
19+
public class AppContextCreation {
20+
21+
public static void main( String[] args ) {
22+
ApplicationContext apContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
23+
((ConfigurableApplicationContext)apContext).registerShutdownHook();
24+
final Object pesrson = apContext.getBean("person");
25+
System.out.println(pesrson);
26+
27+
28+
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ioc.question_007;
2+
3+
4+
import ioc.commun.Person;
5+
import ioc.commun.Profession;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class MyConfiguration {
11+
12+
@Bean
13+
public Profession profession(){
14+
return Profession.builder()
15+
.name("professeur")
16+
.description("professeur a l'universite")
17+
.build();
18+
}
19+
20+
@Bean
21+
public Person person(){
22+
return Person.builder()
23+
.name("wijsen")
24+
.age(52)
25+
.profession(profession())
26+
.build();
27+
}
28+
29+
}

0 commit comments

Comments
 (0)