閱讀本文需要又一定的sping基礎,最起碼要成功的運行過一個SpringMvc項目。
在傳統的Spring項目中,我們要寫一堆的XML文件。而這些XML文件格式要求又很嚴格,很不便于開發。而網上所謂的0配置,并不是純粹的0配置,還是要寫一些xml配置,只是用了幾個@Service,@Controller注解而已。
在這里,我講介紹一種新的配置方式,一行XML代碼都不需要,什么web.xml,Application-context.xml,Beans.xml,統統去死吧!
首先建立一個Maven項目,Packageing方式為war,項目結構為標準Maven WebApp結構。
pom文件如下(很多依賴都沒用,懶得去掉了):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
< 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/maven-v4_0_0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.csonezp</ groupId > < artifactId >springdemo</ artifactId > < packaging >war</ packaging > < version >1.0-SNAPSHOT</ version > < name >dataplatform Maven Webapp</ name > < url >http://maven.apache.org</ url > < properties > < spring.version >4.0.1.RELEASE</ spring.version > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > </ dependency > <!-- Spring dependencies --> < dependency > < groupId >asm</ groupId > < artifactId >asm-commons</ artifactId > < version >2.2.3</ version > </ dependency > < dependency > < groupId >asm</ groupId > < artifactId >asm</ artifactId > < version >2.2.3</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-orm</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-aop</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-expression</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-tx</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >cglib</ groupId > < artifactId >cglib</ artifactId > < version >2.2.2</ version > </ dependency > <!-- Servlet API, JSTL --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > < version >3.0.1</ version > < scope >provided</ scope > </ dependency > < dependency > < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > </ dependency > < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-core-asl</ artifactId > < version >1.8.4</ version > </ dependency > < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-mapper-asl</ artifactId > < version >1.8.4</ version > </ dependency > < dependency > < groupId >c3p0</ groupId > < artifactId >c3p0</ artifactId > < version >0.9.1.2</ version > </ dependency > < dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >1.2.8</ version > </ dependency > < dependency > < groupId >org.json</ groupId > < artifactId >json</ artifactId > < version >20090211</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.6</ version > </ dependency > < dependency > < groupId >spy</ groupId > < artifactId >spymemcached</ artifactId > < version >2.6</ version > </ dependency > < dependency > < groupId >org.slf4j</ groupId > < artifactId >slf4j-api</ artifactId > < version >1.6.6</ version > </ dependency > </ dependencies > < build > < finalName >dataplatform</ finalName > </ build > </ project > |
這個時候,就該進行Spring配置了。按傳統方式來的話,首先要去web.xml寫一堆配置,然后建立個管理beab的Beans.xml,管理spring mvc 的xml,再寫一坨一坨Bean。就是先進一點的(也就是很多人說的0配置),也就是自己的業務Bean不用寫進xml了,還是很麻煩。
而我這里講的方式,則是完全不修改任何web.xml代碼,不寫一行XML代碼的方式。
首先,在項目立建立一個Config.java文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/** * Created by zhangpeng on 16-3-22. * 取代Beans.xml,純注解配置各種BEAN */ @Configuration @ComponentScan ( "com.csonezp" ) @EnableWebMvc public class Config { /** * jsp視圖解析器的bean * @return */ @Bean public UrlBasedViewResolver setupViewResolver() { UrlBasedViewResolver resolver = new UrlBasedViewResolver(); resolver.setPrefix( "/WEB-INF/" ); resolver.setSuffix( ".jsp" ); resolver.setViewClass(JstlView. class ); return resolver; } /** * 配置數據源 * @return */ @Bean (name = "dataSource" ) public ComboPooledDataSource getDataSource() { try { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setJdbcUrl( "jdbc:mysql://localhost:3306/mfdb" ); dataSource.setDriverClass( "com.mysql.jdbc.Driver" ); dataSource.setUser( "root" ); dataSource.setPassword( "zp1228" ); dataSource.setMaxPoolSize( 75 ); return dataSource; } catch (Exception e) { return null ; } } } |
@Configuration注解就是告訴Spring這個是一個配置文件類,這里配置的Bean要交給Spring去管理。這個就是用來取代Beans.xml這種文件的。
@ComponentScan("com.csonezp")這個注解就是配置包掃描用的,不必多說了
@EnableWebMvc ,啟用Spring MVC支持
這里面配置了兩個Bean,第一個就是JSP的視圖解析器,第二個則是配置了一個數據源。這些都可以對應到XML文件里面去的。找一個傳統Spring項目,用xml文件對比著我這個類去看,會對這種配置方式有更深的了解。
下面要建立一個WebInitializer類繼承WebApplicationInitializer,在這里添加一個servlet。這一步是用來取代在web.xml中添加servlet的步驟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; public class WebInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(Config. class ); ctx.setServletContext(servletContext); Dynamic servlet = servletContext.addServlet( "dispatcher" , new DispatcherServlet(ctx)); servlet.addMapping( "/" ); servlet.setLoadOnStartup( 1 ); } } |
這樣就OK啦!讓我們寫一些代碼來測試一下吧!
1
2
3
4
5
6
7
8
9
10
11
|
import org.springframework.stereotype.Service; /** * Created by zhangpeng on 16-3-22. */ @Service public class HelloService { public String getHello(String name) { return name + ",Hello!" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import com.guduo.dataplatform.service.HelloService; import com.guduo.dataplatform.service.MovieService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * Created by zhangpeng on 16-3-22. */ @Controller @RequestMapping ( "/test" ) public class TestController { @Autowired HelloService helloService; @Autowired MovieService movieService; @RequestMapping ( "/hello" ) public String sayHello( @RequestParam ( "name" ) String name, ModelMap model) { model.put( "hello" , helloService.getHello(name)); return "hello" ; } } |
一個service,一個controller。
啟動項目,在瀏覽器中輸入地址http://localhost:8080/dp/test/hello?name=sss
頁面顯示sss,Hello!
完工!
額,忘了演示Confgig里配置的bean如何使用了。其實和XML里的一樣。這里拿一個DAO做演示吧,這里就注入了我們在config里配置的那個數據源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Repository public class MoviePlayIncDao { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; @Autowired public void setDataSource(DataSource dataSource) { this .dataSource = dataSource; this .jdbcTemplateObject = new JdbcTemplate(dataSource); } public List<MoviePlayIncreament> getMoviePlayInc( int movieId) { try { String SQL = "select * from movieplayincreament where movieid=?" ; List<MoviePlayIncreament> list = jdbcTemplateObject.query(SQL, new MoviePlayIncMapper(), new Object[]{movieId}); return list; } catch (Exception e) { return null ; } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/csonezp/p/5315725.html