Skip to content

Commit

Permalink
00 started working on chapter 3 and polished the project Gradle confi…
Browse files Browse the repository at this point in the history
…guration. I pushed this commit to GitHub just to give you a taste of how the project will look when the book will be released. ;)
  • Loading branch information
iuliana committed Jan 29, 2017
0 parents commit d414191
Show file tree
Hide file tree
Showing 1,340 changed files with 103,945 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.idea
.gradle
*.iml
out
build
gradle
gradlew*
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

gradlew
gradlew.bat
Binary file added 9781430261513.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2017 Iuliana Cosmina, Clarence Ho, Rob Harrop, and Chris Schaefer

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


33 changes: 33 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
= Apress Source Code

TODO - change book ISBN
This repository accompanies [*Pro Spring*](http://www.apress.com/9781430261513) by Iuliana Cosmina, Clarence Ho, Rob Harrop, and Chris Schaefer (Apress, 2017).

TODO change cover image
![Cover image](9781430261513.jpg)

Download the files as a zip using the green button, or clone the repository to your machine using Git.

== Releases
Release v1.0 corresponds to the code in the published book, without corrections or updates.

== Contributions
See the file Contributing.adoc for more information on how you can contribute to this repository.

== UNDER CONSTRUCTION

Version 5 of this book is being written so sources here will be changed to use Spring 5.x and Spring Boot 2.x


== Building and Deploying
This is a Gradle multi-module project. The *-practice projects, have tests and pieces left unimplemented so building the full project using:
----
gradle build
----
will fail.

In order to build the full project and skipping tests, build from the root (`book-code`) directory:
----
gradle build -x test
----

38 changes: 38 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ext {
springVersion = '5.0.0.M4'
slf4jVersion = '1.7.22'
logbackVersion = '1.1.9'
junitVersion = '4.12'

spring = [
context: "org.springframework:spring-context:$springVersion"
]

testing = [
junit: "junit:junit:$junitVersion"
]

misc = [
slf4jJcl: "org.slf4j:jcl-over-slf4j:$slf4jVersion",
logback : "ch.qos.logback:logback-classic:$logbackVersion"
]
}

subprojects {
version '5.0-SNAPSHOT'

repositories {
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

task wrapper(type: Wrapper) {
gradleVersion = '3.3'
}
23 changes: 23 additions & 0 deletions chapter02/chapter02.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
subprojects {
group 'com.apress.prospring5.ch02'
apply plugin: 'java'

//Unfortunately, the runtime discovery algorithm in commons-logging, while convenient for the end-user, is problematic
// So the Spring team recommends excluding it and replacing it with SL4j
// https://docs.spring.io/spring/docs/5.0.0.M4/spring-framework-reference/htmlsingle/#overview-not-using-commons-logging
configurations {
all*.exclude group: "commons-logging", module: "commons-logging"
}

/*Task that copies all the dependencies under build/libs */
task copyDependencies(type: Copy) {
from configurations.compile
into 'build/libs'
}

dependencies {
compile spring.context, misc.slf4jJcl, misc.logback
testCompile testing.junit
}
}

7 changes: 7 additions & 0 deletions chapter02/hello-world/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jar {
manifest {
attributes(
'Main-Class': 'com.apress.prospring5.ch2.HelloWorldSpringDI',
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.apress.prospring5.ch2;

public class HelloWorld {

public static void main(String... args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.apress.prospring5.ch2;

import com.apress.prospring5.ch2.decoupled.MessageRenderer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldSpringDI {
public static void main(String... args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext
("spring/app-context.xml");

MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
mr.render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.apress.prospring5.ch2;

public class HelloWorldWithCommandLine {

public static void main(String... args) {
if (args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.apress.prospring5.ch2.annotated;

import com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider;
import com.apress.prospring5.ch2.decoupled.MessageProvider;
import com.apress.prospring5.ch2.decoupled.MessageRenderer;
import com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by iuliana.cosmina on 1/28/17.
*/
@Configuration
public class HelloWorldConfiguration {

@Bean
public MessageProvider provider() {
return new HelloWorldMessageProvider();
}

@Bean
public MessageRenderer renderer(){
MessageRenderer renderer = new StandardOutMessageRenderer();
renderer.setMessageProvider(provider());
return renderer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.apress.prospring5.ch2.annotated;

import com.apress.prospring5.ch2.decoupled.MessageRenderer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Created by iuliana.cosmina on 1/28/17.
*/
public class HelloWorldSpringAnnotated {

public static void main(String... args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext
(HelloWorldConfiguration.class);
MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
mr.render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.apress.prospring5.ch2.decoupled;

public class HelloWorldDecoupled {
public static void main(String... args) {
MessageRenderer mr = new StandardOutMessageRenderer();
MessageProvider mp = new HelloWorldMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.apress.prospring5.ch2.decoupled;

public class HelloWorldDecoupledWithFactory {

public static void main(String... args) {
MessageRenderer mr = MessageSupportFactory.getInstance().getMessageRenderer();
MessageProvider mp = MessageSupportFactory.getInstance().getMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.apress.prospring5.ch2.decoupled;

public class HelloWorldMessageProvider implements MessageProvider {
@Override
public String getMessage() {
return "Hello World!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apress.prospring5.ch2.decoupled;

public interface MessageProvider {
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.apress.prospring5.ch2.decoupled;

public interface MessageRenderer {
void render();
void setMessageProvider(MessageProvider provider);
MessageProvider getMessageProvider();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.apress.prospring5.ch2.decoupled;

import java.util.Properties;

public class MessageSupportFactory {

private static MessageSupportFactory instance;
private Properties props;
private MessageRenderer renderer;
private MessageProvider provider;

private MessageSupportFactory() {
props = new Properties();
try {
props.load(this.getClass().getResourceAsStream("/msf.properties"));
String rendererClass = props.getProperty("renderer.class");
String providerClass = props.getProperty("provider.class");
renderer = (MessageRenderer) Class.forName(rendererClass).newInstance();
provider = (MessageProvider) Class.forName(providerClass).newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}

static {
instance = new MessageSupportFactory();
}

public static MessageSupportFactory getInstance() {
return instance;
}

public MessageRenderer getMessageRenderer() {
return renderer;
}

public MessageProvider getMessageProvider() {
return provider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.apress.prospring5.ch2.decoupled;

public class StandardOutMessageRenderer implements MessageRenderer {

private MessageProvider messageProvider;

@Override
public void render() {
if (messageProvider == null) {
throw new RuntimeException(
"You must set the property messageProvider of class:"
+ StandardOutMessageRenderer.class.getName());
}
System.out.println(messageProvider.getMessage());
}

@Override
public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
}

@Override
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
2 changes: 2 additions & 0 deletions chapter02/hello-world/src/main/resources/msf.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
renderer.class=com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer
provider.class=com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider
12 changes: 12 additions & 0 deletions chapter02/hello-world/src/main/resources/spring/app-context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="provider" class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>

<bean id="renderer" class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer"
p:messageProvider-ref="provider"/>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.apress.prospring5.ch2;

import com.apress.prospring5.ch2.annotated.HelloWorldConfiguration;
import com.apress.prospring5.ch2.decoupled.MessageRenderer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertNotNull;

/**
* Created by iuliana.cosmina on 1/28/17.
*/
public class TestHelloWorldSpringAnnotated {

@Test
public void testHello() {
ApplicationContext ctx = new AnnotationConfigApplicationContext
(HelloWorldConfiguration.class);
MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
assertNotNull(mr);
}
}
Loading

0 comments on commit d414191

Please sign in to comment.