Skip to content

Commit 98a7d93

Browse files
committed
initial commit
0 parents  commit 98a7d93

19 files changed

+363
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target/
2+
.classpath
3+
.settings/
4+
.project

pom.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>kurs.spring</groupId>
4+
<artifactId>core</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
7+
<dependencies>
8+
<dependency>
9+
<groupId>org.springframework</groupId>
10+
<artifactId>spring-context</artifactId>
11+
<version>5.1.1.RELEASE</version>
12+
</dependency>
13+
<dependency>
14+
<groupId>org.springframework</groupId>
15+
<artifactId>spring-test</artifactId>
16+
<version>5.1.1.RELEASE</version>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>org.aspectj</groupId>
21+
<artifactId>aspectjrt</artifactId>
22+
<version>1.6.11</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.aspectj</groupId>
26+
<artifactId>aspectjweaver</artifactId>
27+
<version>1.6.11</version>
28+
</dependency>
29+
</dependencies>
30+
</project>

src/main/java/aop/AopApplication.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package aop;
2+
3+
import org.springframework.beans.factory.config.BeanDefinition;
4+
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
8+
9+
@ComponentScan
10+
@EnableAspectJAutoProxy
11+
public class AopApplication {
12+
13+
public static void main(String[] args) throws Exception {
14+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AopApplication.class);
15+
Greeter greeter = ctx.getBean(Greeter.class);
16+
System.out.println(greeter.getClass());
17+
greeter.sayHello();
18+
19+
for (String beanName : ctx.getBeanDefinitionNames()) {
20+
System.out.println(beanName);
21+
BeanDefinition bd = ctx.getBeanDefinition(beanName);
22+
}
23+
}
24+
}

src/main/java/aop/Greeter.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package aop;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Greeter {
7+
8+
public void sayHello() throws InterruptedException {
9+
System.out.print("Wait for it...");
10+
Thread.sleep(2000);
11+
System.out.println("Hello!");
12+
}
13+
}

src/main/java/aop/Logger.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package aop;
2+
3+
import org.aopalliance.intercept.Joinpoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Aspect
11+
public class Logger {
12+
13+
@Around("execution(* *.*(..))")
14+
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
15+
16+
String methodName = joinPoint.getSignature().getName();
17+
System.out.println("Intercepting call to " + methodName);
18+
19+
long start = System.currentTimeMillis();
20+
21+
Object result = joinPoint.proceed();
22+
23+
long duration = System.currentTimeMillis() - start;
24+
System.out.println("Execution time ms: " + duration);
25+
26+
return result;
27+
}
28+
}

src/main/java/ehandel/AppConfig.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ehandel;
2+
3+
import org.springframework.context.ApplicationContext;
4+
5+
public class AppConfig {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("--- Running AppConfig.main() ---");
10+
11+
//TODO: Create and initialize an ApplicationContext based on annotations
12+
ApplicationContext ctx = null;
13+
14+
OrderProcessingDemo.runDemo(ctx);
15+
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ehandel;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class FakeInventoryManager implements InventoryManager {
7+
8+
Map<String,Integer> inventory;
9+
10+
11+
public FakeInventoryManager() {
12+
this.inventory = new HashMap<String, Integer>();
13+
}
14+
15+
16+
public synchronized int getInventoryLevel(String product) {
17+
return inventory.getOrDefault(product, 0);
18+
}
19+
20+
public synchronized void adjustInventoryLevel(String product, int deltaLevel) {
21+
inventory.putIfAbsent(product, 0);
22+
int newLevel = inventory.get(product) + deltaLevel;
23+
inventory.put(product, newLevel);
24+
System.out.println("Product: " + product + ", level: " + newLevel);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ehandel;
2+
3+
public class FakeOrderRepository implements OrderRepository {
4+
5+
public void saveOrder(Order order) {
6+
7+
System.out.println("------- Saving order -------");
8+
for(OrderItem item: order.getItems()) {
9+
System.out.println(item);
10+
}
11+
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ehandel;
2+
3+
public interface InventoryManager {
4+
5+
int getInventoryLevel(String productId);
6+
7+
void adjustInventoryLevel(String product, int deltaLevel);
8+
}

src/main/java/ehandel/JavaConfig.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ehandel;
2+
3+
import org.springframework.context.ApplicationContext;
4+
5+
public class JavaConfig {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("--- Running JavaConfig.main() ---");
10+
11+
//TODO: Create and initialize a context using Java Configuration
12+
ApplicationContext ctx = null;
13+
14+
OrderProcessingDemo.runDemo(ctx);
15+
}
16+
17+
}

src/main/java/ehandel/Order.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ehandel;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Order {
7+
private List<OrderItem> items = new ArrayList<>();
8+
9+
public List<OrderItem> getItems() {
10+
return items;
11+
}
12+
13+
public void add(String product, int quantity) {
14+
items.add(new OrderItem(product, quantity));
15+
}
16+
}

src/main/java/ehandel/OrderItem.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ehandel;
2+
3+
public class OrderItem {
4+
5+
private String product;
6+
private int quantity;
7+
8+
public OrderItem(String product, int quantity) {
9+
this.product = product;
10+
this.quantity = quantity;
11+
}
12+
13+
public String getProduct() {
14+
return product;
15+
}
16+
17+
public int getQuantity() {
18+
return quantity;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "OrderItem [product=" + product + ", quantity=" + quantity + "]";
24+
}
25+
26+
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ehandel;
2+
3+
import org.springframework.context.ApplicationContext;
4+
5+
public class OrderProcessingDemo {
6+
7+
public static void runDemo(ApplicationContext ctx) {
8+
InventoryManager inventoryManager = ctx.getBean(InventoryManager.class);
9+
10+
//Add some items to inventory
11+
inventoryManager.adjustInventoryLevel("balls", 30);
12+
inventoryManager.adjustInventoryLevel("racket", 10);
13+
14+
ShoppingCart cart = new ShoppingCart();
15+
cart.getItems().add(new OrderItem("balls", 4));
16+
cart.getItems().add(new OrderItem("racket", 1));
17+
18+
OrderProcessor processor = ctx.getBean(OrderProcessor.class);
19+
processor.placeOrder(cart);
20+
}
21+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ehandel;
2+
3+
public class OrderProcessor {
4+
5+
//TODO: initialize fields using Spring DI
6+
private InventoryManager inventoryManager;
7+
private OrderRepository orderRepository;
8+
9+
public synchronized void placeOrder(ShoppingCart cart) {
10+
Order order = new Order();
11+
for(OrderItem item: cart.getItems()) {
12+
order.add(item.getProduct(), item.getQuantity());
13+
inventoryManager.adjustInventoryLevel(
14+
item.getProduct(),
15+
-item.getQuantity());
16+
}
17+
18+
orderRepository.saveOrder(order);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ehandel;
2+
3+
public interface OrderRepository {
4+
void saveOrder(Order order);
5+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ehandel;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ShoppingCart {
7+
8+
private List<OrderItem> items = new ArrayList<>();
9+
10+
public void add(String product, int amount) {
11+
getItems().add(new OrderItem(product, amount));
12+
}
13+
14+
public List<OrderItem> getItems() {
15+
return items;
16+
}
17+
}

src/main/java/ehandel/XmlConfig.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ehandel;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class XmlConfig {
8+
9+
public static void main(String[] args) {
10+
11+
System.out.println("--- Running JavaConfig.main() ---");
12+
13+
14+
//TODO: Create and initialize a context using xml configuration
15+
ApplicationContext ctx = null;
16+
}
17+
}

src/main/java/ehandel/spring.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans
3+
xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<!-- TODO Declare beans for e-commerce example -->
9+
10+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package core;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.annotation.DirtiesContext;
9+
import org.springframework.test.annotation.DirtiesContext.ClassMode;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import ehandel.FakeInventoryManager;
14+
import ehandel.FakeOrderRepository;
15+
import ehandel.OrderProcessor;
16+
import ehandel.ShoppingCart;
17+
18+
public class OrderProcessorTests {
19+
20+
@Test
21+
public void orderProcessorAdjustsInventory() {
22+
23+
//Arrange
24+
FakeInventoryManager inventoryManager = new FakeInventoryManager();
25+
FakeOrderRepository or = new FakeOrderRepository();
26+
27+
//TODO: create an instance based on the DI approach you chose in exercise 1
28+
OrderProcessor orderProcessor = null;
29+
30+
inventoryManager.adjustInventoryLevel("balls", 30);
31+
inventoryManager.adjustInventoryLevel("racket", 10);
32+
33+
ShoppingCart cart = new ShoppingCart();
34+
cart.add("balls", 4);
35+
cart.add("racket", 1);
36+
37+
//Act
38+
orderProcessor.placeOrder(cart);
39+
40+
//Assert
41+
assertEquals(26, inventoryManager.getInventoryLevel("balls"));
42+
assertEquals(9, inventoryManager.getInventoryLevel("racket"));
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)