BAEL-393: renamed guice-intro directory to guice (#1385)
* 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 * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * Update pom.xml * Update pom.xml * Update pom.xml * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md * BAEL-157: README.md updated * Changed project name * Update RunGuice.java Removed references to message logging and output * Update Communication.java Removed message logging-related code * BAEL-566: Updated README.md * New project name * BAEL-393: removing guice-intro directory * BAEL-393: renamed module guice-intro to guice in root pom.xml
This commit is contained in:
32
guice/src/main/java/com/baeldung/examples/RunGuice.java
Normal file
32
guice/src/main/java/com/baeldung/examples/RunGuice.java
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
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);
|
||||
while (true) {
|
||||
String input = scanner.nextLine();
|
||||
if (input.equalsIgnoreCase("q")) {
|
||||
System.exit(0);
|
||||
} else {
|
||||
comms.sendMessage(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
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;
|
||||
|
||||
@Inject
|
||||
private DefaultCommunicator communicator;
|
||||
|
||||
public Communication(Boolean keepRecords) {
|
||||
if (keepRecords) {
|
||||
System.out.println("keeping records");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sendMessage(String message) {
|
||||
|
||||
return communicator.sendMessage(message);
|
||||
}
|
||||
|
||||
public DefaultCommunicator getCommunicator() {
|
||||
return this.communicator;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
package com.baeldung.examples.guice;
|
||||
|
||||
import com.baeldung.examples.guice.marker.Communicator;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.name.Named;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author baeldung
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
package com.baeldung.examples.guice;
|
||||
|
||||
import com.baeldung.examples.guice.aop.MessageSentLoggable;
|
||||
import com.baeldung.examples.guice.constant.CommunicationModel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author baeldung
|
||||
*/
|
||||
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.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
package com.baeldung.examples.guice.aop;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
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 {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.examples.guice.marker;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Tayo
|
||||
*/
|
||||
public interface Communicator {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user