|
Size: 10770
Comment:
|
Size: 11117
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 397: | Line 397: |
=== src/main/resources/templates/greeting.html === {{{ <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html> }}} |
SpringBoot
Example
pom.xml
<?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>hello</groupId>
<artifactId>test-spring-boot</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
<properties>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
src/main/java/hello/GreetingController.java
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.ArrayList;
@Controller
public class GreetingController {
private final Logger logger = LoggerFactory.getLogger(GreetingController.class);
@Autowired
DummyDAO dummyDAO;
public GreetingController(){
logger.debug("Greeting controller created.");
}
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
logger.info("Greeting endpoint called.");
model.addAttribute("name", name);
return "greeting";
}
@RequestMapping(value="/dummy",produces="application/json")
@ResponseBody
public List<Dummy> dummy(){
List<Dummy> list= new java.util.ArrayList<Dummy>();
Dummy dummy = new Dummy();
dummy.setFieldA("AAA");
dummy.setFieldB("CCC");
list.add(dummy);
Dummy dummy2 = new Dummy();
dummy2.setFieldA("AAA2");
dummy2.setFieldB("CCC2");
list.add(dummy2);
return list;
}
@RequestMapping(value="/dummyname",produces="application/json")
@ResponseBody
public String getDummyName(){
return dummyDAO.getNameFromDummy();
}
}
src/main/java/hello/Application.java
package hello;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ComponentScan
@EnableAutoConfiguration
public class Application {
private static Logger logger;
public static void main(String[] args) {
logger = LoggerFactory.getLogger(Application.class);
logger.info("Starting application");
SpringApplication.run(Application.class, args);
}
@Bean(name="basicDataSource",destroyMethod="close")
public BasicDataSource createBasicDataSource(){
logger.info("Creating basicDataSource");
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("org.mariadb.jdbc.Driver");
bds.setUrl("jdbc:mariadb://localhost:3306/springmvchtml");
bds.setUsername("usertest");
bds.setPassword("usertest");
bds.setInitialSize(3);
return bds;
}
/*
@Bean(name="jdbcTemplate")
public JdbcTemplate jdbcTemplate(){
logger.info("Creating jdbcTemplate");
return new JdbcTemplate( basicDataSource() );
}
*/
@Bean(name="jdbcTemplate")
public JdbcTemplate createJdbcTemplate(@Autowired @Qualifier("basicDataSource") BasicDataSource bds ){
logger.info("Creating jdbcTemplate");
return new JdbcTemplate( bds );
}
}
src/main/java/hello/ThreadTimer.java
package hello;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.MessageFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
@Component
public class ThreadTimer extends Thread {
private int delaySeconds;
private Logger logger;
private boolean running;
private Object monitor=new Object();
private ArrayList<Object> subscribers;
//@Autowired
//WaitThread waitThread;
public ThreadTimer() {
this.logger = LoggerFactory.getLogger(ThreadTimer.class);
logger.info("Created instance of " + this.getClass().getSimpleName());
this.running = true;
this.delaySeconds = 5 * 1000;
this.setName(this.getClass().getSimpleName() + "_" + this.getName());
this.subscribers = new ArrayList<Object>();
}
public void addSubscriber(Object subscriber){
this.subscribers.add(subscriber);
}
@PostConstruct
public void init() {
logger.info("Starting the thread");
this.start();
}
@Override
public void run() {
while (running) {
try {
Thread.sleep(this.delaySeconds);
logger.info("Delay " + this.getClass().getSimpleName());
for(Object o: this.subscribers){
synchronized(o){
o.notify();
}
}
}
catch (InterruptedException e) {
logger.info("ThreadTimer interrupted exception:" + e.getMessage() );
}
catch (Exception e) {
e.printStackTrace();
logger.info("ThreadTimer exception:" + e.getMessage() );
stopRunning();
}
}
logger.info("Exited " + this.getClass().getSimpleName());
}
public void startRunning() {
this.running = true;
}
public void stopRunning() {
this.running = false;
}
public void destroy(){
logger.info("Called destroy");
this.stopRunning();
this.interrupt();
}
}
src/main/java/hello/WaitThread.java
package hello;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class WaitThread extends Thread {
private Logger logger;
private boolean running;
private Object monitor;
@Autowired
ThreadTimer timerThread;
public WaitThread() {
this.logger = LoggerFactory.getLogger(WaitThread.class);
logger.info("Created instance of " + this.getClass().getSimpleName());
this.running = true;
this.setName(this.getClass().getSimpleName() + "_" + this.getName());
this.monitor=new Object();
}
@PostConstruct
public void init() {
this.timerThread.addSubscriber(this);
logger.info("Starting the thread");
this.start();
}
public void run() {
while (running) {
try {
synchronized(this){
this.wait();
logger.info("Notification received.");
}
}
catch (InterruptedException e) {
logger.info("ThreadTimer interrupted exception:" + e.getMessage() );
}
catch (Exception e) {
logger.info("WaitThread exception:" + e.getMessage() );
stopRunning();
}
}
logger.info("Exited " + this.getClass().getSimpleName());
}
public void startRunning() {
this.running = true;
}
public void stopRunning() {
this.running = false;
}
public void destroy(){
logger.info("Called destroy");
this.stopRunning();
this.interrupt();
}
}
src/main/java/hello/Dummy.java
package hello;
public class Dummy{
private String fieldA;
private String fieldB;
public Dummy(){
}
public String getFieldA(){
return fieldA;
}
public String getFieldB(){
return fieldB;
}
public void setFieldA(String arg){
fieldA = arg;
}
public void setFieldB(String arg){
fieldB = arg;
}
}
src/main/java/hello/DummyDAO.java
package hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class DummyDAO {
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
private Logger logger;
public DummyDAO() {
logger = LoggerFactory.getLogger(DummyDAO.class);
logger.info("Created " + this.getClass().getSimpleName());
}
public String getNameFromDummy() {
return this.jdbcTemplate.queryForObject("select name from dummy limit 1", String.class);
}
}
src/main/resources/templates/greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>