Skip to content

Commit 82c36d1

Browse files
committed
question 8,9,10 and 11
1 parent dc7d8c6 commit 82c36d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+443
-18
lines changed

Study-guide.pdf

148 KB
Binary file not shown.

pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@
77
<groupId>java.certification.spring</groupId>
88
<artifactId>springcertification</artifactId>
99
<version>1.0-SNAPSHOT</version>
10-
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
1122

1223

1324
<dependencies>

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ioc.commun;
22
import lombok.*;
33

4+
import java.time.LocalDate;
5+
46

57
@Builder
68
@Setter
@@ -11,6 +13,7 @@ public class Person {
1113

1214
private String name;
1315
private int age;
16+
private LocalDate dateOfBirth;
1417
private Profession profession;
1518

1619

src/main/java/ioc/question_004/AppContextCreation.java

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* This class create a standalone application context using the annotation config way.
1010
* @todo We can use other ways like:
1111
* {@link ClassPathXmlApplicationContext}
12-
*
1312
*/
1413

1514
public class AppContextCreation {

src/main/java/ioc/question_004/MyConfiguration.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
package ioc.question_004;
22

3-
43
import ioc.commun.Person;
54
import ioc.commun.Profession;
6-
import org.springframework.context.annotation.Bean;
7-
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.*;
6+
7+
import java.time.LocalDate;
88

99
@Configuration
1010
public class MyConfiguration {
1111

1212
@Bean
1313
public Profession profession(){
1414
return Profession.builder()
15-
.name("professeur")
16-
.description("professeur a l'universite")
15+
.name("professor")
16+
.description("professor in the university")
1717
.build();
1818
}
1919

2020
@Bean
2121
public Person person(){
2222
return Person.builder()
23-
.name("wijsen")
23+
.name("Bagna")
2424
.age(52)
2525
.profession(profession())
26+
.dateOfBirth(LocalDate.of(1950,12,13))
2627
.build();
2728
}
2829

src/main/java/ioc/question_007/AppContextCreation.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import org.springframework.context.ApplicationContext;
44
import org.springframework.context.ConfigurableApplicationContext;
55
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6-
import org.springframework.context.support.ClassPathXmlApplicationContext;
76
import org.springframework.web.context.ContextLoaderListener;
8-
import javax.servlet.*;
97

108
/**
119
* What is the preferred way to close an application context?
@@ -19,12 +17,9 @@
1917
public class AppContextCreation {
2018

2119
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-
}
20+
// ApplicationContext apContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
21+
// ((ConfigurableApplicationContext)apContext).registerShutdownHook();
22+
// final Object pesrson = apContext.getBean("person");
23+
// System.out.println(pesrson);
24+
}
3025
}

src/main/java/ioc/question_007/MyConfiguration.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88

9+
import java.time.LocalDate;
10+
911
@Configuration
1012
public class MyConfiguration {
1113

@@ -22,6 +24,7 @@ public Person person(){
2224
return Person.builder()
2325
.name("wijsen")
2426
.age(52)
27+
.dateOfBirth(LocalDate.of(1960,01,30))
2528
.profession(profession())
2629
.build();
2730
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ioc.question_008;
2+
3+
4+
/**
5+
* Scopes of beans, What is the default scope?
6+
* Stand-alone application scopes:
7+
* 1-Singleton scope: default scope, unique in the application context.
8+
* 2-Prototype: new instance whenever the bean is requested in the application context.
9+
*
10+
* WebApplication scopes:
11+
* 3-Request scope : new instance for every new Request. {@link org.springframework.web.context.request.RequestScope}
12+
* 4-Session scope : new instance for every new Session.{@link org.springframework.web.context.request.SessionScope}
13+
* 5-Application scope : new instance for every new ServletContext.{@link org.springframework.web.context.annotation.ApplicationScope}
14+
* 6-WebSocket scope : new instance for every new WebSocket.
15+
*/
16+
public class BeansScope {
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ioc.question_008;
2+
3+
import ioc.commun.Person;
4+
import ioc.commun.Profession;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Scope;
8+
9+
import java.time.LocalDate;
10+
11+
@Configuration
12+
public class ConfigurationForBeansScope {
13+
14+
15+
/**
16+
*
17+
* @return a new instance of the profession class whenever it is called
18+
*/
19+
@Bean
20+
@Scope(scopeName = "prototype")
21+
public Profession profession(){
22+
return Profession.builder()
23+
.name("professor")
24+
.description("professor in the university")
25+
.build();
26+
}
27+
28+
@Bean
29+
public Person person(){
30+
return Person.builder()
31+
.name("Bagna")
32+
.age(52)
33+
.profession(profession())
34+
.dateOfBirth(LocalDate.of(1950,12,13))
35+
.build();
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ioc.question_008;
2+
3+
import ioc.commun.Person;
4+
import ioc.question_004.MyConfiguration;
5+
import ioc.question_008.dao.MyDaoRepository;
6+
import ioc.question_008.dao.dao;
7+
import ioc.question_008.service.MyService;
8+
import ioc.question_008.service.service;
9+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.context.annotation.Import;
13+
14+
/**
15+
* Can you describe:
16+
* 1-Dependency injection using Java configuration?
17+
* 2-Dependency injection using annotations (@Autowired)?
18+
* 3-Component scanning, Stereotypes?
19+
* 4-Scopes for Spring beans? What is the default scope?
20+
*
21+
* 1-Java configuration:
22+
* @Bean the annotated method will produces a bean that will be managed by the container.
23+
* @Configuration indicates that the annotated class contains one or more methods annotated by @Bean.
24+
* 2,3- (@Component, @Autowired)
25+
* @Component indicate that the annotated class can be automaticaly detected and registred as a @Bean in the
26+
* application context.
27+
* @Autowired indicated that the container should look for a bean and inject it in the annoted place.
28+
* @ComponentScan define a root package that should be scanned to create @Bean.
29+
*
30+
* Some stereotype :
31+
* @Component: Root annotation, uses as a componenet scan condidate.
32+
* @Service: Business logic.
33+
* @Repository: Data access logic.
34+
* @Controller: Web Controller class.
35+
* @Configuration: Class that declare beans to be loaded and managed by the container.
36+
*
37+
*/
38+
39+
@Configuration
40+
@Import(MyConfiguration.class)
41+
@ComponentScan(basePackageClasses = {MyDaoRepository.class, MyService.class} )
42+
public class DependencyInjection {
43+
44+
public static void main( String[] args ) {
45+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DependencyInjection.class);
46+
dao myDaoRepository = (MyDaoRepository) context.getBean("myDaoRepository");
47+
service myService = (service) context.getBean("myService");
48+
System.out.println(myDaoRepository);
49+
Person p = context.getBean(Person.class);
50+
myService.save(p);
51+
System.out.println(myDaoRepository);
52+
myService.delete(p);
53+
System.out.println(myDaoRepository);
54+
55+
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ioc.question_008.dao;
2+
3+
import ioc.commun.Person;
4+
import lombok.*;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
@Repository
12+
//@Builder
13+
@Setter
14+
@Getter
15+
@EqualsAndHashCode
16+
@ToString
17+
@NoArgsConstructor
18+
public class MyDaoRepository implements dao {
19+
List<Person> personList = new ArrayList<>();
20+
21+
22+
23+
@Override
24+
public boolean save( Person person ){
25+
return this.personList.add(person);
26+
}
27+
28+
@Override
29+
public boolean delete( Person person ){
30+
return this.personList.remove(person);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ioc.question_008.dao;
2+
3+
import ioc.commun.Person;
4+
5+
public interface dao {
6+
boolean save( Person person );
7+
8+
boolean delete( Person person );
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ioc.question_008.service;
2+
3+
import ioc.commun.Person;
4+
import ioc.question_008.dao.MyDaoRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class MyService implements service {
10+
11+
@Autowired
12+
MyDaoRepository myDaoRepository;
13+
14+
@Override
15+
public boolean save( Person person ){
16+
return this.myDaoRepository.save(person);
17+
}
18+
19+
@Override
20+
public boolean delete(Person person){
21+
return this.myDaoRepository.delete(person);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ioc.question_008.service;
2+
3+
import ioc.commun.Person;
4+
5+
public interface service {
6+
boolean save( Person person );
7+
8+
boolean delete( Person person );
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ioc.question_009;
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+
import org.springframework.context.annotation.Lazy;
9+
10+
import java.time.LocalDate;
11+
12+
@Configuration
13+
@Lazy//mark every bean to be initialized lazily
14+
public class LazyBeanConfiguration {
15+
16+
@Bean
17+
public Profession profession(){
18+
return Profession.builder()
19+
.name("professor009")
20+
.description("professor in the university")
21+
.build();
22+
}
23+
24+
@Bean
25+
public Person person(){
26+
System.out.println("person");
27+
return Person.builder()
28+
.name("Bagna_009")
29+
.age(52)
30+
.profession(profession())
31+
.dateOfBirth(LocalDate.of(1950,12,13))
32+
.build();
33+
}
34+
35+
@Bean
36+
@Lazy(false)//override the Class annotation
37+
public Person person2(){
38+
System.out.println("person2");
39+
return Person.builder()
40+
.name("Bagna2_009")
41+
.age(52)
42+
.profession(profession())
43+
.dateOfBirth(LocalDate.of(1950,12,13))
44+
.build();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)