Skip to content

Commit

Permalink
add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusreusch committed Jan 12, 2019
1 parent 770f2a2 commit 2695787
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"ch.mare.springfundamentals.beanscopes._01"})
@ComponentScan(basePackages = {"ch.mare.springfundamentals.beanscopes"})
public class AppConfig {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.mare.springfundamentals.beanscopes;

import ch.mare.springfundamentals.dependencyinjection.di_03.CustomerController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

Expand All @@ -9,8 +8,18 @@ public class Application {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

CustomerController customerController = ctx.getBean(CustomerController.class);
SingletonComponent singletonComponent1 = ctx.getBean(SingletonComponent.class);
SingletonComponent singletonComponent2 = ctx.getBean(SingletonComponent.class);

System.out.println(singletonComponent1.toString());
System.out.println(singletonComponent2.toString());

PrototypeComponent prototypeComponent1 = ctx.getBean(PrototypeComponent.class);
PrototypeComponent prototypeComponent2 = ctx.getBean(PrototypeComponent.class);

System.out.println(prototypeComponent1.toString());
System.out.println(prototypeComponent2.toString());


customerController.removeCustomerAccount("123");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.mare.springfundamentals.beanscopes;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class PrototypeComponent {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.mare.springfundamentals.beanscopes;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("singleton")
public class SingletonComponent {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ch.mare.springfundamentals.dependencyinjection.di_01;

import ch.mare.springfundamentals.dependencyinjection.di_01.controller.CustomerController;

public class Application {

public static void main(String[] args) {

CustomerController customerController = new CustomerController();
customerController.deactivateCustomerAccount("1");
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package ch.mare.springfundamentals.dependencyinjection.di_01.controller;

public class CustomerController {
import ch.mare.springfundamentals.dependencyinjection.di_01.service.CustomerService;

public class CustomerController {

public void deactivateCustomerAccount(String customerId) {
CustomerService customerService = new CustomerService();
customerService.deactivateCustomer(customerId);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package ch.mare.springfundamentals.dependencyinjection.di_01.dataaccess;

import ch.mare.springfundamentals.dependencyinjection.common.domain.Customer;
import ch.mare.springfundamentals.dependencyinjection.common.domain.MailingAddress;
import java.util.HashMap;
import java.util.Map;

public class CustomerLoader {


private static final Map<String, Customer> DATABASE = new HashMap<>() {{
put("1", new Customer("1", "Hans Wurst", new MailingAddress("Hauptstrasse 2", "Zürich"), "[email protected]"));
put("2", new Customer("2", "Peter Peterson", new MailingAddress("Bahnhofsweg 100", "Bern"), "[email protected]"));
}};

public Customer findById(String customerId) {
return DATABASE.get(customerId);
return InMemoryDatabase.DATABASE.get(customerId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.mare.springfundamentals.dependencyinjection.di_01.dataaccess;

import ch.mare.springfundamentals.dependencyinjection.common.domain.Customer;
import ch.mare.springfundamentals.dependencyinjection.common.domain.MailingAddress;
import java.util.HashMap;
import java.util.Map;

public class InMemoryDatabase {

public static final Map<String, Customer> DATABASE = new HashMap<>() {{
put("1", new Customer("1", "Hans Wurst", new MailingAddress("Hauptstrasse 2", "Zürich"), "[email protected]"));
put("2", new Customer("2", "Peter Peterson", new MailingAddress("Bahnhofsweg 100", "Bern"), "[email protected]"));
}};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class CustomerService {

public void removeCustomer(String customerId) {
public void deactivateCustomer(String customerId) {

CustomerLoader customerLoader = new CustomerLoader();
Customer customer = customerLoader.findById(customerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;

@Service
@Profile({"mock"})
public class CustomerLoaderMock implements CustomerLoader {
private static final Map<String, Customer> DATABASE = new HashMap<>() {{
put("1", new Customer("1", "Hans Tester", new MailingAddress("Teststrasse 2", "Zürich"), "[email protected]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;

@Service
@Profile("real")
public class CustomerLoaderReal implements CustomerLoader {

private static final Map<String, Customer> DATABASE = new HashMap<>() {{
Expand Down

0 comments on commit 2695787

Please sign in to comment.