Guice Intro (#1177)

* Add files via upload

* Update pom.xml

* Update RunGuice.java

* Update Communication.java

* Update CommunicationMode.java

* Update DefaultCommunicator.java

* Update EmailCommunicationMode.java

* Update IMCommunicationMode.java

* Update SMSCommunicationMode.java

* Update MessageLogger.java

* Update MessageSentLoggable.java

* Update AOPModule.java

* Update BasicModule.java

* Update CommunicationModel.java

* Update Communicator.java

* Update BasicModule.java

* Update RunGuice.java

* Update MessageLogger.java

* Update Communicator.java

* Update pom.xml

* Update pom.xml

* Update pom.xml

* Update pom.xml
This commit is contained in:
k0l0ssus 2017-03-02 23:20:59 -05:00 committed by KevinGilmore
parent 23489a082b
commit 6356fb6a26
15 changed files with 429 additions and 0 deletions

34
guice-intro/pom.xml Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.examples.guice</groupId>
<artifactId>guice-intro</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<guice.version>4.1.0</guice.version>
</properties>
<name>guice-intro</name>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.examples;
import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.binding.AOPModule;
import com.baeldung.examples.guice.modules.BasicModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.Scanner;
/**
*
* @author Baeldung
*/
public class RunGuice {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule());
Communication comms = injector.getInstance(Communication.class);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your message to be sent; press Q to quit and P to print the message log");
while (true) {
String input = scanner.nextLine();
if (input.equalsIgnoreCase("q")) {
System.exit(0);
}
if (input.equalsIgnoreCase("p")) {
comms.print();
} else {
comms.sendMessage(input);
}
}
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.examples.guice;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Logger;
/**
*
* @author Baeldung
*/
public class Communication {
final Date start = new Date();
@Inject
private Logger logger;
private Queue<String> messageLog;
@Named("CommsUUID")
private String commsID;
@Inject
private DefaultCommunicator communicator;
public Communication(Boolean keepRecords) {
if (keepRecords) {
messageLog = new LinkedList();
}
}
public boolean sendMessage(String message) {
if (!message.isEmpty() && messageLog != null) {
messageLog.add(message);
}
return communicator.sendMessage(message);
}
public void print() {
if (messageLog != null) {
for (String message : messageLog) {
logger.info(message);
}
} else {
logger.info("Message logging wasn't enabled");
}
}
public DefaultCommunicator getCommunicator() {
return this.communicator;
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.constant.CommunicationModel;
public interface CommunicationMode {
public CommunicationModel getMode();
public boolean sendMessage(String message);
}

View File

@ -0,0 +1,48 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.marker.Communicator;
import com.google.inject.Inject;
import com.google.inject.name.Named;
public class DefaultCommunicator implements Communicator {
private CommunicationMode defaultCommsMode;
@Inject
@Named("SMSComms")
CommunicationMode smsCommsMode;
@Inject
@Named("EmailComms")
CommunicationMode emailCommsMode;
@Inject
@Named("IMComms")
CommunicationMode imCommsMode;
protected DefaultCommunicator(CommunicationMode defaultComms) {
this.defaultCommsMode = defaultComms;
}
public DefaultCommunicator() {
}
public boolean sendMessage(String message) {
boolean sent = false;
if (defaultCommsMode != null) {
sent = sendMessageByDefault(message);
} else {
sent = smsCommsMode.sendMessage(message);
}
return sent;
}
private boolean sendMessageByDefault(String message) {
boolean sent = false;
if (message != null && !message.trim().equals("")) {
return defaultCommsMode.sendMessage(message);
}
return sent;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
/**
*
* @author Baekdung
*/
public class EmailCommunicationMode implements CommunicationMode {
@Override
public CommunicationModel getMode() {
return CommunicationModel.EMAIL;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String Message) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;
/**
*
* @author Baeldung
*/
public class IMCommunicationMode implements CommunicationMode {
@Inject
private Logger logger;
@Override
public CommunicationModel getMode() {
return CommunicationModel.IM;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String message) {
logger.info("IM Message Sent");
return true;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;
/**
*
* @author Baeldung
*/
public class SMSCommunicationMode implements CommunicationMode {
@Inject
private Logger logger;
@Override
public CommunicationModel getMode() {
return CommunicationModel.SMS;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String message) {
logger.info("SMS message sent");
return true;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.examples.guice.aop;
import java.util.logging.Logger;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
*
* @author Baeldung
*/
public class MessageLogger implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] objectArray = invocation.getArguments();
int i = 0;
for (Object object : objectArray) {
Logger.getAnonymousLogger().info("Sending message: " + object.toString());
}
return invocation.proceed();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.examples.guice.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Baeldung
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MessageSentLoggable {
}

View File

@ -0,0 +1,22 @@
package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.aop.MessageLogger;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.google.inject.AbstractModule;
import com.google.inject.matcher.Matchers;
/**
*
* @author Baeldung
*/
public class AOPModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(),
Matchers.annotatedWith(MessageSentLoggable.class),
new MessageLogger()
);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Baeldung
*/
public class BasicModule extends AbstractModule {
@Override
protected void configure() {
try {
bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE));
} catch (NoSuchMethodException ex) {
Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex);
}
bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton();
bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.examples.guice.constant;
/**
*
* @author Baeldung
*/
public enum CommunicationModel {
EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone");
final String name;
CommunicationModel(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.examples.guice.marker;
/**
*
* @author Baeldung
*/
public interface Communicator {
public boolean sendMessage(String message);
}

View File

@ -0,0 +1,37 @@
package com.baeldung.examples.guice.modules;
import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Baeldung
*/
public class BasicModule extends AbstractModule {
@Override
protected void configure() {
try {
bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class));
bind(Boolean.class).toInstance(true);
} catch (NoSuchMethodException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
}
bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton();
bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class);
}
}