Skip to content

Commit

Permalink
Merge pull request #8 from Sofimedina/factory-beans
Browse files Browse the repository at this point in the history
Factory beans
  • Loading branch information
Sofimedina authored Aug 23, 2023
2 parents 323d467 + f0c0d2c commit 9c73684
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Sofia Medina
# Dependency Injection Examples

This repository is for an example application built in my [Spring Framework 5 - Beginner to Guru](https://www.udemy.com/testing-spring-boot-beginner-to-guru/?couponCode=GITHUB_REPO) online course
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/guru/springframework/pets/CatPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
/**
* Created by jt on 12/28/19.
*/
@Service
@Profile("cat")
//@Service
//@Profile("cat")
public class CatPetService implements PetService {
@Override
public String getPetType() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/guru/springframework/pets/DogPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
/**
* Created by jt on 12/28/19.
*/
@Profile({"dog", "default"})
@Service
//@Profile({"dog", "default"})
//@Service
public class DogPetService implements PetService {
public String getPetType(){
return "Dogs are the best!";
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/guru/springframework/pets/PetServiceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guru.springframework.pets;

public class PetServiceFactory {
public PetService getPetService(String petType){
switch (petType){
case "dog":
return new DogPetService();
case "cat":
return new CatPetService();
default:return new DogPetService();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package guru.springframework.sfgdi.config;


import guru.springframework.pets.CatPetService;
import guru.springframework.pets.DogPetService;
import guru.springframework.pets.PetService;
import guru.springframework.pets.PetServiceFactory;
import guru.springframework.sfgdi.repositories.EnglishGreetingRepository;
import guru.springframework.sfgdi.repositories.EnglishGreetingRepositoryImpl;
import guru.springframework.sfgdi.services.*;
Expand All @@ -12,33 +17,38 @@
public class GreetingServiceConfiguration {

@Bean
ConstructorGreetingService constructorGreetingService(){
ConstructorGreetingService constructorGreetingService() {
return new ConstructorGreetingService();
}

@Bean
PropertyInjectedGreetingService propertyInjectedGreetingService(){
PropertyInjectedGreetingService propertyInjectedGreetingService() {
return new PropertyInjectedGreetingService();
}

@Bean
SetterInjectedGreetingService setterInjectedGreetingService(){
SetterInjectedGreetingService setterInjectedGreetingService() {
return new SetterInjectedGreetingService();
}

@Primary
@Bean
PrimaryGreetingService primaryGreetingService(){
PrimaryGreetingService primaryGreetingService() {
return new PrimaryGreetingService();
}


@Profile({"ES", "default"})
@Bean(name = "i18nService")
I18NSpanishService i18NSpanishService(){
I18NSpanishService i18NSpanishService() {
return new I18NSpanishService();
}


@Profile("EN")
@Bean
I18nEnglishGreetingService i18nService() {
return new I18nEnglishGreetingService();
@Bean
EnglishGreetingRepository englishGreetingRepository(){
return new EnglishGreetingRepositoryImpl();
Expand All @@ -48,4 +58,21 @@ EnglishGreetingRepository englishGreetingRepository(){
I18nEnglishGreetingService i18nService(){
return new I18nEnglishGreetingService(englishGreetingRepository());
}

@Bean
PetServiceFactory petServiceFactory() {
return new PetServiceFactory();
}

@Profile({"dog", "default"})
@Bean
PetService dogPetService() {
return new PetServiceFactory().getPetService("dog");
}

@Profile("cat")
@Bean
PetService catPetService() {
return new PetServiceFactory().getPetService("cat");
}
}

0 comments on commit 9c73684

Please sign in to comment.