Development, starting from demand & #183; 3. Where is spring and where is spring?

Source: Internet
Author: User

Development, starting from requirements · where is spring and where is spring?


The first song in "three hundred children's songs" in "Journey to the West" is called "Where is Spring".

The lyrics are as follows:

Where is spring?

Where is spring?

Spring is in the eyes of children


The result is as follows:

Where spring is

Where spring is

The fucking spring is

In javatar's eyes

Yo

Yo

Check it out


I believe that java programmers have realized what spring is :)

But does spring have something to do with what we are talking about now? No-_-B

Let's get down to the truth and start with what we mentioned in the previous chapter, to make the problem more complicated.


We assume that this search service requires support from lucene and mysql.

Obtain the Document ID through lucene search, and then query the mysql database based on the ID to obtain the document title and text content.


Let's start with the requirement.SearchServiceInRealBizClass:

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchServiceInRealBiz implements SearchService {public List search(String keywords) {int[] idlist = findDocIDs(keywords);List<Result> results = getResultsByDocIDs(idlist);return results;}}

Of course, you will see an error message in your eclipse:


Because these two methods are not defined. But it does not matter. eclipse comes with tools,

The automatically generated code is like this:


Just implement them :)

We follow the previous development process to provide a simulation implementation first:

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchServiceInRealBiz implements SearchService {public List search(String keywords) {int[] idlist = findDocIDs(keywords);List<Result> results = getResultsByDocIDs(idlist);return results;}private List<Result> getResultsByDocIDs(int[] idlist) {List<Result> results = new ArrayList<Result>(idlist.length);for(int i = 0; i < idlist.length; i++) {int id = idlist[i];String title = "result " + id;String content = "something..................";results.add(new Result(title, content));}return results;}private int[] findDocIDs(String keywords) {return new int[]{1, 2, 3, 4};}}
Keep in mind that we should test each step, and we will not go into detail here.

For example, we can run BeanFactory once to check whether the output of the main method will change unexpectedly.

Now, although I have not run tomcat to view the webpage, I am sure that the content displayed on the webpage must be correct.


Due to the nature of java programmers, I think writing a DAO layer, then... Of course, it is the interface and implementation to separate Xiami...

Lucene implementation (simulation phase ):

Interface:

package cn.com.sitefromscrath.dao;public interface LuceneDAO {public int[] findDocIDs(String keywords);}

Implementation:

package cn.com.sitefromscrath.dao;public class LuceneDAOMock implements LuceneDAO {@Overridepublic int[] findDocIDs(String keywords) {return new int[]{1, 2, 3, 4};}}

Mysql implementation (simulation phase ):

Interface:

package cn.com.sitefromscrath.dao;import java.util.List;import cn.com.sitefromscrath.entity.Result;public interface MysqlDAO {public List<Result> getResultsByDocIDs(int[] idlist);}

Implementation:

package cn.com.sitefromscrath.dao;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class MysqlDAOMock implements MysqlDAO {@Overridepublic List<Result> getResultsByDocIDs(int[] idlist) {List<Result> results = new ArrayList<Result>(idlist.length);for(int i = 0; i < idlist.length; i++) {int id = idlist[i];String title = "result " + id;String content = "something..................";results.add(new Result(title, content));}return results;}}

Then, we organize the SearchServiceInRealBiz code again, and we will:

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchServiceInRealBiz implements SearchService {public List search(String keywords) {int[] idlist = findDocIDs(keywords);List<Result> results = getResultsByDocIDs(idlist);return results;}private List<Result> getResultsByDocIDs(int[] idlist) {}private int[] findDocIDs(String keywords) {}}

Replace:

public class SearchServiceInRealBiz implements SearchService {public List search(String keywords) {//int[] idlist = findDocIDs(keywords);//List<Result> results = getResultsByDocIDs(idlist);LuceneDAO luceneDAO = new LuceneDAOMock();int[] idlist = luceneDAO.findDocIDs(keywords);MysqlDAO mysqlDAO = new MysqlDAOMock();List<Result> results = mysqlDAO.getResultsByDocIDs(idlist);return results;}}

Test, stdout/eclipse console output is correct.

[result 1]something..................[result 2]something..................[result 3]something..................[result 4]something..................

Of course, we will also find a problem. Our current class is analog data. How can we switch in the future?

Fortunately, there is a factory. Since we switched in the factory

SearchService
So,
LuceneDAO MysqlDAO
It can also be processed there, so we also execute the process I mentioned above,

Starting from the requirement, I need to define a method first, then use the eclipse tool to generate the skeleton method skeleton of the method, and then implement it.

First, rewrite SearchServiceInRealBiz:

Then, the implementation method SKELETON:

package cn.com.sitefromscrath;import java.util.List;import javax.xml.rpc.ServiceFactory;import cn.com.sitefromscrath.dao.LuceneDAO;import cn.com.sitefromscrath.dao.LuceneDAOMock;import cn.com.sitefromscrath.dao.MysqlDAO;import cn.com.sitefromscrath.dao.MysqlDAOMock;import cn.com.sitefromscrath.entity.Result;import cn.com.sitefromscrath.service.SearchService;import cn.com.sitefromscrath.service.SearchServiceMock;import cn.com.sitefromscrath.service.SearchServiceInRealBiz;public class BeanFactory {public static boolean MOCK = true; public static Object getBean(String id) {if("searchService".equals(id)) {if(MOCK) {return new SearchServiceMock();} else {return getSearchService();}}throw new RuntimeException("cannot find the bean with id :" + id);}public static LuceneDAO getLuceneDAO() {if(MOCK) {return new LuceneDAOMock();} else {throw new RuntimeException("cannot find the LuceneDAO bean");}}public static MysqlDAO getMysqlDAO() {if(MOCK) {return new MysqlDAOMock();} else {throw new RuntimeException("cannot find the MysqlDAO bean");}}public static SearchService getSearchService() {if(MOCK) {return new SearchServiceMock();} else {return new SearchServiceInRealBiz();}}public static void main(String ... arg) {String keywords = "test";SearchService searchService = (SearchService)BeanFactory.getBean("searchService");List results = searchService.search(keywords);for(int i = 0; i < results.size(); i++) {Result result = (Result) results.get(i);System.out.print("[" + result.title + "]");System.out.println(result.content);}}}

The test is correct. Bingo!

I gotta get the GREEN BAR !!! (Dedicated for JUnit ~~~)


Now, I start to find out

BeanFactory 
It is a bit ubiquitous. Once you need to modify it, even if you change the name of BeanFactory, it is quite troublesome to work without the refactor tool.

This is the so-called"God Class" "God Object"

To reduce the impact, at least I should try to remove the word BeanFactory from other classes.


Let's sing an international song, "There has never been any savior, no god emperor", and make a drastic revolution to BeanFactory ~~!


In our current Code, only SearchServiceInRealBiz embraces "God ". Therefore, consider the following methods:

public class SearchServiceInRealBiz implements SearchService {private LuceneDAO luceneDAO;private MysqlDAO mysqlDAO;private SearchServiceInRealBiz(LuceneDAO luceneDAO, MysqlDAO mysqlDAO) {super();this.luceneDAO = luceneDAO;this.mysqlDAO = mysqlDAO;}public List search(String keywords) {int[] idlist = luceneDAO.findDocIDs(keywords);List<Result> results = mysqlDAO.getResultsByDocIDs(idlist);return results;}}


Of course, BeanFactory will certainly report an error,


Description required: I expect such an error (Compilation Error), So that we can find that the changes to the Code will affect those places. Then make corresponding adjustments.

Refer to another method:

private LuceneDAO luceneDAO;private MysqlDAO mysqlDAO;public void setLuceneDAO(LuceneDAO luceneDAO) {this.luceneDAO = luceneDAO;}public void setMysqlDAO(MysqlDAO mysqlDAO) {this.mysqlDAO = mysqlDAO;}public List search(String keywords) {int[] idlist = luceneDAO.findDocIDs(keywords);List<Result> results = mysqlDAO.getResultsByDocIDs(idlist);return results;}

In this way, eclipse will not report errors, ....... You have lost the opportunity to make corrections!

If it is detected during compilation and cannot run, the error D is returned! In addition, this problem will cause you to vomit blood ~~~~

(In fact, I am suggesting which assembly method should you choose in spring xml ......)


Now, let's fix the BeanFactory error:

public static SearchService getSearchService() {if(MOCK) {return new SearchServiceMock();} else {LuceneDAO luceneDAO = getLuceneDAO();MysqlDAO mysqlDAO = getMysqlDAO();return new SearchServiceInRealBiz(luceneDAO, mysqlDAO);}}

RUN Once, OK, no problem. I LOVE GREE BAR!


The first song of children's songs is by no means a real name ~~


To be continued ....


Where is the music in spring?

Haha, Haha, I DON't mean [I DON't know ]......

Third-grade music lesson in spring video

Video.baidu.com/..developer3&rsp
Click the URL above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.