顯示具有 spring 標籤的文章。 顯示所有文章
顯示具有 spring 標籤的文章。 顯示所有文章

2017年3月25日 星期六

【Spring Boot】Why using spring boot

When I need to produce restful api using spring framework,I use Spring MVC4 ,that I need to config spring structure as below

We saw in project have some config file(xml),in spring xml config file was common usage for spring developer,everything look like well.
But in spring boot ,we don’t need config file anymore,and we don’t need to deploy web app to container.

Lets take a look what the spring boot project structure
Yes,no any config file,and no web.xml,amazing!

When we try to add to tomcat to test out project,ide can not let us add boot app to server

So how can we test out app?
Let's take a look on below file

DomoApplication.java



Lets run it as boot app


We can see this app running on 8080 port
Lets try it on browser,look like it running,but content a bit odd,actually we need to add something
We add some content in DomoApplication.java,and run it again
Looks good now



Spring boot let us drop config files,spring developer all know if our project became big or huge ,we always miss in config files so pain,spring boot let us more config again.

So here we got 2 nice feature
  1. No config anymore
  2. No need to install container before deploy to production server
Great~~~

In next post ,I will write more about why I like to use spring boot for my further java web development.see you~~




2013年7月9日 星期二

【Spring】Test in Spring MVC3

   1: package com.lywebii.goldprice;
   2:  
   3: import static org.junit.Assert.*;
   4:  
   5: import java.util.List;
   6:  
   7: import org.junit.After;
   8: import org.junit.Before;
   9: import org.junit.Test;
  10: import org.junit.runner.RunWith;
  11: import org.springframework.beans.factory.annotation.Autowired;
  12: import org.springframework.data.mongodb.core.MongoTemplate;
  13: import org.springframework.data.mongodb.core.geo.Box;
  14: import org.springframework.data.mongodb.core.geo.Point;
  15: import org.springframework.data.mongodb.core.index.GeospatialIndex;
  16: import org.springframework.data.mongodb.core.query.Criteria;
  17: import org.springframework.data.mongodb.core.query.Order;
  18: import org.springframework.data.mongodb.core.query.Query;
  19: import org.springframework.test.context.ContextConfiguration;
  20: import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  21:  
  22: import com.lywebii.goldprice.model.Floating;
  23:  
  24: @RunWith(SpringJUnit4ClassRunner.class)
  25: @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
  26: public class TestFloatingController {
  27:     @Autowired
  28:     MongoTemplate mongoTemplate;
  29:     
  30:     @Before
  31:     public void setUp() throws Exception {
  32:         Floating floating = new Floating();
  33:         floating.setPosition(new double[]{25.0017035,121.5000121});//235台灣新北市中和區中和路37號
  34:         this.mongoTemplate.save(floating);
  35:         
  36:         floating = new Floating();
  37:         floating.setPosition(new double[]{25.0209304,121.4595818}); //220台灣新北市板橋區金華街90-140號
  38:         this.mongoTemplate.save(floating);
  39:         
  40:         floating = new Floating();
  41:         floating.setPosition(new double[]{25.0052171,121.4198273}); //238台灣新北市樹林區中正路214號
  42:         this.mongoTemplate.save(floating);
  43:         
  44:         floating = new Floating();
  45:         floating.setPosition(new double[]{22.6092776,120.316238}); //806台灣高雄市前鎮區南寧街
  46:         this.mongoTemplate.save(floating);
  47:     }
  48:  
  49:     @After
  50:     public void tearDown() throws Exception {
  51:         this.mongoTemplate.dropCollection(Floating.class);
  52:     }
  53:  
  54:     @Test
  55:     public void test() {
  56:         Query query = new Query();
  57:         query.sort().on("date", Order.DESCENDING);
  58:         
  59:  
  60:         this.mongoTemplate.indexOps(Floating.class).ensureIndex(new GeospatialIndex("position"));
  61:  
  62:  
  63:         List<Floating> list = this.mongoTemplate.find(new Query(Criteria.where("position").near(new Point(25.0052171,121.4198273)).maxDistance(5/111.12)),Floating.class);
  64:         System.out.println("Circle:"+list);
  65:  
  66:  
  67:         list = this.mongoTemplate.find(new Query(Criteria.where("position").within(new Box(new Point(0.2, 0.2), new Point(1, 1)))),Floating.class);
  68:         System.out.println("Box:"+list);
  69:  
  70:         
  71:     }
  72:  
  73: }

 


Above code was about usging monogdb template to save postion data and query near point that given.


IOC Configuration file using below code:



   1: @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/root-context.xml"})