Skip to content

Commit

Permalink
some more samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusreusch committed Jan 8, 2019
1 parent 6b94e26 commit 185e026
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ch.mare.springfundamentals.beanscopes;

import org.springframework.context.annotation.ComponentScan;

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

}
16 changes: 16 additions & 0 deletions src/main/java/ch/mare/springfundamentals/beanscopes/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.mare.springfundamentals.beanscopes;

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

public class Main {

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

CustomerController customerController = ctx.getBean(CustomerController.class);

customerController.removeCustomerAccount("123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

public class Customer {

private String id;
private String name;
private MailingAddress mailingAddress;
private String emailAddress;

public Customer(String id, String name, MailingAddress mailingAddress, String emailAddress) {
this.id = id;
this.name = name;
this.mailingAddress = mailingAddress;
this.emailAddress = emailAddress;
}

public boolean hasEmailAddress() {
return true;
return emailAddress != null && !emailAddress.isEmpty();
}

public String getEmailAddress() {
return "[email protected]";
return emailAddress;
}

public MailingAddress getMailingAddress() {
return new MailingAddress();
return mailingAddress;
}

public void deactivate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

public class MailingAddress {

private String street;
private String city;

public MailingAddress(String street, String city) {
this.street = street;
this.city = city;
}

@Override
public String toString() {
return "MailingAddress{" +
"street='" + street + '\'' +
", city='" + city + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
public class EmailService {

public void send(String emailAddress, String subject, String message) {
System.out.println("Email has been sent to " + emailAddress);
System.out.println("Email with subject " + subject + " has been sent to " + emailAddress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.mare.springfundamentals.dependencyinjection.di_01;

public class Application {

public static void main(String[] args) {

}

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

public class CustomerController {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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);
}

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

import ch.mare.springfundamentals.dependencyinjection.common.domain.Customer;
import ch.mare.springfundamentals.dependencyinjection.common.service.EmailService;
import ch.mare.springfundamentals.dependencyinjection.common.service.PostalService;
import ch.mare.springfundamentals.dependencyinjection.di_01.dataaccess.CustomerLoader;

public class CustomerService {

public void removeCustomer(Customer customer) {
public void removeCustomer(String customerId) {

CustomerLoader customerLoader = new CustomerLoader();
Customer customer = customerLoader.findById(customerId);

deactivate(customer);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ch.mare.springfundamentals.dependencyinjection.di_01.service;

public class EmailService {

public void send(String emailAddress, String subject, String message) {
System.out.println("Email with subject " + subject + " has been sent to " + emailAddress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch.mare.springfundamentals.dependencyinjection.di_01.service;

import ch.mare.springfundamentals.dependencyinjection.common.domain.MailingAddress;
import org.springframework.stereotype.Component;

//TODO: better naming?
public class PostalService {

public void sendLetter(MailingAddress mailingAddress) {
System.out.println("A letter will be sent to " + mailingAddress);
}
}

0 comments on commit 185e026

Please sign in to comment.