Skip to content

Commit 9106bc8

Browse files
Merge pull request #3 from ximanta/master
Added Post on Configuring Spring Boot for PostgreSQL
2 parents 6962e2d + 8dba4a5 commit 9106bc8

File tree

23 files changed

+585
-11
lines changed

23 files changed

+585
-11
lines changed

pom.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,33 @@
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-web</artifactId>
4242
</dependency>
43-
43+
<!--WebJars-->
44+
<dependency>
45+
<groupId>org.webjars</groupId>
46+
<artifactId>bootstrap</artifactId>
47+
<version>3.3.4</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.webjars</groupId>
51+
<artifactId>jquery</artifactId>
52+
<version>2.1.4</version>
53+
</dependency>
4454
<dependency>
4555
<groupId>com.h2database</groupId>
4656
<artifactId>h2</artifactId>
4757
<scope>runtime</scope>
4858
</dependency>
59+
<dependency>
60+
<groupId>org.postgresql</groupId>
61+
<artifactId>postgresql</artifactId>
62+
<version>9.4-1206-jdbc42</version>
63+
</dependency>
4964
<dependency>
5065
<groupId>org.springframework.boot</groupId>
5166
<artifactId>spring-boot-starter-test</artifactId>
5267
<scope>test</scope>
5368
</dependency>
69+
5470
</dependencies>
5571

5672
<build>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package guru.springframework.bootstrap;
2+
3+
import guru.springframework.domain.Product;
4+
import guru.springframework.repositories.ProductRepository;
5+
import org.apache.log4j.Logger;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.ApplicationListener;
8+
import org.springframework.context.event.ContextRefreshedEvent;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.math.BigDecimal;
12+
13+
@Component
14+
public class ProductLoader implements ApplicationListener<ContextRefreshedEvent> {
15+
16+
private ProductRepository productRepository;
17+
18+
private Logger log = Logger.getLogger(ProductLoader.class);
19+
20+
@Autowired
21+
public void setProductRepository(ProductRepository productRepository) {
22+
this.productRepository = productRepository;
23+
}
24+
25+
@Override
26+
public void onApplicationEvent(ContextRefreshedEvent event) {
27+
28+
Product shirt = new Product();
29+
shirt.setDescription("Spring Framework Guru Shirt");
30+
shirt.setPrice(new BigDecimal("18.95"));
31+
shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
32+
shirt.setProductId("235268845711068308");
33+
productRepository.save(shirt);
34+
35+
log.info("Saved Shirt - id: " + shirt.getId());
36+
37+
Product mug = new Product();
38+
mug.setDescription("Spring Framework Guru Mug");
39+
mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
40+
mug.setProductId("168639393495335947");
41+
mug.setPrice(new BigDecimal("11.95"));
42+
productRepository.save(mug);
43+
44+
log.info("Saved Mug - id:" + mug.getId());
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package guru.springframework.configuration;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
6+
7+
@Configuration
8+
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
9+
10+
@Override
11+
protected void configure(HttpSecurity httpSecurity) throws Exception {
12+
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
13+
.authorizeRequests().antMatchers("/console/**").permitAll();
14+
15+
httpSecurity.csrf().disable();
16+
httpSecurity.headers().frameOptions().disable();
17+
}
18+
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package guru.springframework.configuration;
2+
3+
import org.h2.server.web.WebServlet;
4+
import org.springframework.boot.context.embedded.ServletRegistrationBean;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class WebConfiguration {
10+
@Bean
11+
ServletRegistrationBean h2servletRegistration(){
12+
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
13+
registrationBean.addUrlMappings("/console/*");
14+
return registrationBean;
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package guru.springframework.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
public class IndexController {
8+
@RequestMapping("/")
9+
String index(){
10+
return "index";
11+
}
12+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package guru.springframework.controllers;
2+
3+
import guru.springframework.domain.Product;
4+
import guru.springframework.services.ProductService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
12+
@Controller
13+
public class ProductController {
14+
15+
private ProductService productService;
16+
17+
@Autowired
18+
public void setProductService(ProductService productService) {
19+
this.productService = productService;
20+
}
21+
22+
@RequestMapping(value = "/products", method = RequestMethod.GET)
23+
public String list(Model model){
24+
model.addAttribute("products", productService.listAllProducts());
25+
System.out.println("Returning rpoducts:");
26+
return "products";
27+
}
28+
29+
@RequestMapping("product/{id}")
30+
public String showProduct(@PathVariable Integer id, Model model){
31+
model.addAttribute("product", productService.getProductById(id));
32+
return "productshow";
33+
}
34+
35+
@RequestMapping("product/edit/{id}")
36+
public String edit(@PathVariable Integer id, Model model){
37+
model.addAttribute("product", productService.getProductById(id));
38+
return "productform";
39+
}
40+
41+
@RequestMapping("product/new")
42+
public String newProduct(Model model){
43+
model.addAttribute("product", new Product());
44+
return "productform";
45+
}
46+
47+
@RequestMapping(value = "product", method = RequestMethod.POST)
48+
public String saveProduct(Product product){
49+
50+
productService.saveProduct(product);
51+
52+
return "redirect:/product/" + product.getId();
53+
}
54+
55+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package guru.springframework.domain;
2+
3+
import javax.persistence.*;
4+
import java.math.BigDecimal;
5+
6+
@Entity
7+
public class Product {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.AUTO)
10+
private Integer id;
11+
12+
@Version
13+
private Integer version;
14+
15+
private String productId;
16+
private String description;
17+
private String imageUrl;
18+
private BigDecimal price;
19+
20+
public String getDescription() {
21+
return description;
22+
}
23+
24+
public void setDescription(String description) {
25+
this.description = description;
26+
}
27+
28+
public Integer getVersion() {
29+
return version;
30+
}
31+
32+
public void setVersion(Integer version) {
33+
this.version = version;
34+
}
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public String getProductId() {
45+
return productId;
46+
}
47+
48+
public void setProductId(String productId) {
49+
this.productId = productId;
50+
}
51+
52+
public String getImageUrl() {
53+
return imageUrl;
54+
}
55+
56+
public void setImageUrl(String imageUrl) {
57+
this.imageUrl = imageUrl;
58+
}
59+
60+
public BigDecimal getPrice() {
61+
return price;
62+
}
63+
64+
public void setPrice(BigDecimal price) {
65+
this.price = price;
66+
}
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package guru.springframework.repositories;
2+
3+
import guru.springframework.domain.Product;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface ProductRepository extends CrudRepository<Product, Integer>{
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package guru.springframework.services;
2+
3+
4+
import guru.springframework.domain.Product;
5+
6+
public interface ProductService {
7+
Iterable<Product> listAllProducts();
8+
9+
Product getProductById(Integer id);
10+
11+
Product saveProduct(Product product);
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.domain.Product;
4+
import guru.springframework.repositories.ProductRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class ProductServiceImpl implements ProductService {
10+
private ProductRepository productRepository;
11+
12+
@Autowired
13+
public void setProductRepository(ProductRepository productRepository) {
14+
this.productRepository = productRepository;
15+
}
16+
17+
@Override
18+
public Iterable<Product> listAllProducts() {
19+
return productRepository.findAll();
20+
}
21+
22+
@Override
23+
public Product getProductById(Integer id) {
24+
return productRepository.findOne(id);
25+
}
26+
27+
@Override
28+
public Product saveProduct(Product product) {
29+
return productRepository.save(product);
30+
}
31+
}

0 commit comments

Comments
 (0)