@@ -21,6 +21,13 @@ | |||
<version>5.7.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<!-- https://mvnrepository.com/artifact/org.springframework/spring --> | |||
<dependency> | |||
<groupId>org.springframework</groupId> | |||
<artifactId>spring-context</artifactId> | |||
<version>5.2.13.RELEASE</version> | |||
</dependency> | |||
<dependency> | |||
<!-- https://github.com/ricksbrown/cowsay --> | |||
<groupId>com.github.ricksbrown</groupId> |
@@ -1,21 +1,18 @@ | |||
package be.syntra; | |||
import be.syntra.cowsay.*; | |||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |||
import org.springframework.context.annotation.ComponentScan; | |||
import java.util.Scanner; | |||
@ComponentScan | |||
public class App { | |||
public static void main(String[] args) { | |||
/** | |||
* Dependency Injection is a technique of creating software in which objects do not create their dependencies on itself. | |||
* instead objects declare dependencies that they need and it an external object or framework to provide | |||
* concrete dependencies to objects | |||
*/ | |||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class); | |||
CowSay cowSay = new DragonCowSay(); | |||
QuoteDao quoteDao = new QuoteDaoMysql(); | |||
cowSay.setQuoteDao(quoteDao); | |||
CowSay cowSay = context.getBean("cowSay", CowSay.class); | |||
cowSay.showQuote(); | |||
Scanner in = new Scanner(System.in); |
@@ -0,0 +1,51 @@ | |||
package be.syntra.common; | |||
import java.util.logging.ConsoleHandler; | |||
import java.util.logging.Level; | |||
import java.util.logging.Logger; | |||
import java.util.logging.SimpleFormatter; | |||
import org.springframework.beans.factory.annotation.Value; | |||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |||
import org.springframework.core.annotation.Order; | |||
import org.springframework.stereotype.Component; | |||
import javax.annotation.PostConstruct; | |||
@Component | |||
@Order(1) | |||
public class LoggerConfig { | |||
@Value("WARNING") | |||
private String rootLoggerLevel; | |||
@Value("WARNING") | |||
private String printedLoggerLevel; | |||
@PostConstruct | |||
public void initLogger() { | |||
// parse levels | |||
Level rootLevel = Level.parse(rootLoggerLevel); | |||
Level printedLevel = Level.parse(printedLoggerLevel); | |||
// get logger for app context | |||
Logger applicationContextLogger = Logger.getLogger(AnnotationConfigApplicationContext.class.getName()); | |||
// get parent logger | |||
Logger loggerParent = applicationContextLogger.getParent(); | |||
// set root logging level | |||
loggerParent.setLevel(rootLevel); | |||
// set up console handler | |||
ConsoleHandler consoleHandler = new ConsoleHandler(); | |||
consoleHandler.setLevel(printedLevel); | |||
consoleHandler.setFormatter(new SimpleFormatter()); | |||
// add handler to the logger | |||
loggerParent.addHandler(consoleHandler); | |||
} | |||
} |
@@ -4,6 +4,5 @@ public interface CowSay { | |||
void showQuote(); | |||
void voteUp(); | |||
void voteDown(); | |||
void setQuoteDao(QuoteDao quoteDao); | |||
} | |||
@@ -1,11 +1,16 @@ | |||
package be.syntra.cowsay; | |||
import com.github.ricksbrown.cowsay.Cowsay; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.beans.factory.annotation.Qualifier; | |||
import org.springframework.stereotype.Component; | |||
@Component | |||
public class DragonCowSay implements CowSay { | |||
QuoteDao quoteDao; | |||
Quote quote; | |||
public void setQuoteDao(QuoteDao quoteDao) { | |||
@Autowired | |||
public DragonCowSay (@Qualifier("activeQuoteDao") QuoteDao quoteDao) { | |||
this.quoteDao = quoteDao; | |||
} | |||
@@ -1,11 +1,16 @@ | |||
package be.syntra.cowsay; | |||
import com.github.ricksbrown.cowsay.Cowsay; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.beans.factory.annotation.Qualifier; | |||
import org.springframework.stereotype.Component; | |||
@Component("cowSay") | |||
public class OriginalCowSay implements CowSay { | |||
QuoteDao quoteDao; | |||
Quote quote; | |||
public void setQuoteDao(QuoteDao quoteDao) { | |||
@Autowired | |||
public OriginalCowSay (@Qualifier("activeQuoteDao") QuoteDao quoteDao) { | |||
this.quoteDao = quoteDao; | |||
} | |||
@@ -1,5 +1,8 @@ | |||
package be.syntra.cowsay; | |||
import org.springframework.stereotype.Component; | |||
@Component | |||
public class QuoteDaoDummy implements QuoteDao { | |||
@Override | |||
public Quote getRandomQuote() { |
@@ -1,26 +1,22 @@ | |||
package be.syntra.cowsay; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import org.springframework.beans.factory.annotation.Value; | |||
import org.springframework.context.annotation.PropertySource; | |||
import org.springframework.stereotype.Component; | |||
import java.sql.*; | |||
import java.util.Properties; | |||
public class QuoteDaoMysql implements QuoteDao { | |||
private static String url; | |||
private static String usr; | |||
private static String pass; | |||
static { | |||
try (InputStream in = QuoteDaoMysql.class.getResourceAsStream("/app.properties")) { | |||
Properties properties = new Properties(); | |||
properties.load(in); | |||
url = properties.getProperty("url"); | |||
usr = properties.getProperty("usr"); | |||
pass = properties.getProperty("pass"); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
@Component("activeQuoteDao") | |||
@PropertySource("classpath:app.properties") | |||
class QuoteDaoMysql implements QuoteDao { | |||
@Value("${db.url}") | |||
private String url; | |||
@Value("${db.usr}") | |||
private String usr; | |||
@Value("${db.pass}") | |||
private String pass; | |||
@Override | |||
public Quote getRandomQuote() { |
@@ -1,3 +1,3 @@ | |||
url=jdbc:mysql://ID109462_crud.db.webhosting.be:3306/ID109462_crud | |||
usr=ID109462_crud | |||
pass=code4croissants | |||
db.url=jdbc:mysql://ID109462_crud.db.webhosting.be:3306/ID109462_crud | |||
db.usr=ID109462_crud | |||
db.pass=code4croissants |