Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory beans #8

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}