Development, starting from demand & #183; spring is here, spring is here

Source: Internet
Author: User

Development, starting from demand · spring here, spring here

First, I want to express my position in the big-character slogans:

The framework & non-core features you use is the same as the clothes women wear. the fewer the clothes, the better!


Let's talk about it.


Let's keep staring at huajie -- ah, no -- it's BeanFactory.

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

Why does the title "spring here" mean?

Nana? Didn't you see it?

Well, maybe you are used to spring's xml assembly method, so it seems that you need extraordinary imagination to associate the two,

I changed BeanFactory to a simple assembly based on text strings. Let's take a look at the effect:


Package cn.com. sitefromscrath; import java. lang. reflect. constructor; import java. util. hashMap; import java. util. list; import java. util. map; public class BeanFactory {private static Map <String, String> appBean = new HashMap <String, String> (); private static Map <String, string []> appRef = new HashMap <String, String []> (); static {appBean. put ("luceneDAO", "cn.com. sitefromscrath. dao. luceneDAOMock "); appBean. put ("mysqlDAO", "cn.com. sitefromscrath. dao. mysqlDAOMock "); appBean. put ("searchService", "cn.com. sitefromscrath. service. searchServiceInRealBiz "); appRef. put ("searchService", new String [] {"luceneDAO", "mysqlDAO"});} public static Object getBean (String id) {try {String className = appBean. get (id); Class clazz = Class. forName (className); Constructor constructor; String [] ref = appRef. get (id); if (ref = null | ref. length = 0) {constructor = clazz. getConstructor (); return (Object) constructor. newInstance ();} Class [] parameterTypes = new Class [ref. length]; Object [] initargs = new Object [ref. length]; for (int I = 0; I <ref. length; I ++) {String r = ref [I]; String rclassName = appBean. get (r); parameterTypes [I] = Class. forName (rclassName ). getInterfaces () [0]; // here I am lazy :) initargs [I] = getBean (r);} constructor = clazz. getConstructor (parameterTypes); return (Object) constructor. newInstance (initargs);} catch (Exception e) {e. printStackTrace (); return null ;}} public static void main (String... arg) {LuceneDAO luceneDAO = (LuceneDAO) getBean ("luceneDAO"); int [] vals = luceneDAO. findDocIDs ("test"); for (int v: vals) {System. out. println (v);} String keywords = "test"; SearchService searchService = (SearchService) 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 );}}}

Running result output:

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

The result is correct!


Let's look at our assembly of classes:

private static Map<String, String> appBean = new HashMap<String, String>();private static Map<String, String[]> appRef = new HashMap<String, String[]>();static {appBean.put("luceneDAO", "cn.com.sitefromscrath.dao.LuceneDAOMock");appBean.put("mysqlDAO", "cn.com.sitefromscrath.dao.MysqlDAOMock");appBean.put("searchService", "cn.com.sitefromscrath.service.SearchServiceInRealBiz");appRef.put("searchService", new String[]{"luceneDAO", "mysqlDAO"});}

Compare the differences with spring. xml:

<?xml version="1.0" encoding="UTF-8"?><beans ><bean id="luceneDAO" class="cn.com.sitefromscrath.dao.LuceneDAOMock" /><bean id="mysqlDAO" class="cn.com.sitefromscrath.dao.MysqlDAOMock" /> <bean id="searchService" class="cn.com.sitefromscrath.service.SearchServiceInRealBiz"><constructor-arg index="1" ref="luceneDAO" /><constructor-arg index="2" ref="mysqlDAO" /></bean>  </beans>

Well, there is no fundamental difference. We can also Parse xml and get all the elements required for BeanFactory.

Well, after we refactored the code from scratch to a more "classic" mode, we found "spring", which is also the core features of spring.


Maybe from a different perspective on spring, I found that the use of spring dependency injection and control reversal is different from that of many people. In the next chapter, I plan to discuss my views with some development and testing skills.

But now, it's time to spit out spring:

I have mentioned in my blog "thinking in asp" appendix A-tucao JAVA:

There is also a new version of spring. How can this problem be solved? It has played the annotation mechanism of java to the level of "amazing.

Since the series mainly focuses on video encoding and transcoding technology, it is only a bit of a thing for spring. Now we have finally found the opportunity :)


Long long ago, in the old good times, spring is still a little girl who relies on xml for assembly. She is so young and beautiful that she is very angry with others;

But now we can see that this girl has been smeared and put into the upper class. Although she is not annoying in business scenarios, she is not very close to anyone-_-B.


Looking at the traditional spring. xml is like looking at the circuit board design diagram. The components are clear, the model, the cabling, and the Assembly. Although there is no graphical view, it is easy to understand. Basically, you don't need to view the source code, so you can sort out the logical relationship of the entire system.

Since annotation, such as autowire and xml, is not important, you cannot quickly find a natural "outline" from a large spring-based project ".

-- Spring began to show off all the vulgarity. -- Maybe I am a masochistic, but "ruby way" and "proud" python are obviously more appetizing to me-the design principles cannot be relaxed :)

I have asked some spring users how to find a class that is assembled through annotation, especially automatic assembly, or a URL that is hidden in the annotation declaration under the spring-mvc framework,

The answer is as follows:

First:


Then:


Okay, I got the conclusion: It's better not to use it :)

Of course, if you say that using some lexical/syntax parser, you can also get an "outline" based on annotation, such as using lex + yacc or anlr to create a tool,

The answer from this seat is: I am not sleeping for a few nights due to the compilation principle of Lao Tzu. Be careful when I try to stamp you out to three kilometers to go ~~~!


Next, let's talk about how the spring framework works.Strong TestingProblems:

Always remember that every module, or even the "smallest" method, must first design how to test it before implementing it.


Because we are currently discussing a web project, I would like to introduce a method that many developers will use:

ContextListener --> WebApplication --> BeanFactory

Because of spring's "humanization", this step does not even require you to write code.

Now the question is, if our project adopts this method, how do you perform the dao or service layer unit test?

For example, the SearchService mentioned above needs to assemble LuceneDAO and MysqlDAO through spring, but the problem arises:

To run the SearchService method, you must start TOMCAT and other web containers!

This degree of tight coupling is simply horrible: Making all unit tests impossible ~~~~

In the previous chapter, I repeatedly stressed that "you do not need to start tomcat, you do not need to view the actual effect of the web page, and the system module is correct :)


Of course, spring provides ApplicationContext, which can also be used in web containers -- this is also my method of use -- however, what I want to say is that spring's WebApplication pattern error has caused developers, caused a lot of bad taste.


After playing with how to start from the simplest needs and rebuild to the pattern/framework, I will go back in the next chapter. spring has already said enough :)

To be continued .....


The legend "four hours in the mountains" describes the spring scene. Which of the following statements about the summer scenery is _______?

They are: ye fangfa and Youxiang, Jia Mu Xiu and fan Yin
 
Assignment of public management in administration (this) of Beijing TV University in spring 11, with a high score. required before four o'clock P.M.

You can use your student ID to log on to the central power University online to find your request.
 

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.