1. 程式人生 > >Spring註解學習手札(一)構建web應用

Spring註解學習手札(一)構建web應用

近來工作發生了一些變化,有必要學習一下Spring註解了!
網上找了一些個例子,總的說來比較土,大多數是轉載摘抄,按照提示弄下來根本都執行不了,索性自己趟一遍這渾水,在這裡留下些個印記。
這次,先來構建一個極為簡單的web應用,從controller到dao。不考慮具體實現,只是先對整體架構有一個清晰的瞭解。日後在分層細述每一層的細節。




我們將用到如下jar包:
引用
aopalliance-1.0.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar

先看web.xml
Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-app
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns="http://java.sun.com/xml/ns/javaee"
  5.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  7.     id="WebApp_ID"
  8.     version="2.5">
  9.     <display-name>spring</display-name>
  10.     <!-- 應用路徑 -->
  11.     <context-param>
  12.         <param-name>webAppRootKey</param-name>
  13.         <param-value>spring.webapp.root</param-value>
  14.     </context-param>
  15.     <!-- Log4J 配置  -->
  16.     <context-param>
  17.         <param-name>log4jConfigLocation</param-name>
  18.         <param-value>classpath:log4j.xml</param-value>
  19.     </context-param>
  20.     <context-param>
  21.         <param-name>log4jRefreshInterval</param-name>
  22.         <param-value>60000</param-value>
  23.     </context-param>
  24.     <!--Spring上下文 配置  -->
  25.     <context-param>
  26.         <param-name>contextConfigLocation</param-name>
  27.         <param-value>/WEB-INF/applicationContext.xml</param-value>
  28.     </context-param>
  29.     <!-- 字符集 過濾器  -->
  30.     <filter>
  31.         <filter-name>CharacterEncodingFilter</filter-name>
  32.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  33.         <init-param>
  34.             <param-name>encoding</param-name>
  35.             <param-value>UTF-8</param-value>
  36.         </init-param>
  37.         <init-param>
  38.             <param-name>forceEncoding</param-name>
  39.             <param-value>true</param-value>
  40.         </init-param>
  41.     </filter>
  42.     <filter-mapping>
  43.         <filter-name>CharacterEncodingFilter</filter-name>
  44.         <url-pattern>/*</url-pattern>
  45.     </filter-mapping>
  46.     <!-- Spring 監聽器 -->
  47.     <listener>
  48.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  49.     </listener>
  50.     <listener>
  51.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  52.     </listener>
  53.     <!-- Spring 分發器 -->
  54.     <servlet>
  55.         <servlet-name>spring</servlet-name>
  56.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  57.         <init-param>
  58.             <param-name>contextConfigLocation</param-name>
  59.             <param-value>/WEB-INF/servlet.xml</param-value>
  60.         </init-param>
  61.     </servlet>
  62.     <servlet-mapping>
  63.         <servlet-name>spring</servlet-name>
  64.         <url-pattern>*.do</url-pattern>
  65.     </servlet-mapping>
  66.     <welcome-file-list>
  67.         <welcome-file>index.html</welcome-file>
  68.         <welcome-file>index.htm</welcome-file>
  69.         <welcome-file>index.jsp</welcome-file>
  70.         <welcome-file>default.html</welcome-file>
  71.         <welcome-file>default.htm</welcome-file>
  72.         <welcome-file>default.jsp</welcome-file>
  73.     </welcome-file-list>
  74. </web-app>

有不少人問我,這段程式碼是什麼:
Xml程式碼  收藏程式碼
  1. <!-- 應用路徑 -->
  2. <context-param>
  3.     <param-name>webAppRootKey</param-name>
  4.     <param-value>spring.webapp.root</param-value>
  5. </context-param>

這是當前應用的路徑變數,也就是說你可以在其他程式碼中使用${spring.webapp.root}指代當前應用路徑。我經常用它來設定log的輸出目錄。
為什麼要設定引數log4jConfigLocation?
Xml程式碼  收藏程式碼
  1. <!-- Log4J 配置  -->
  2.     <context-param>
  3.         <param-name>log4jConfigLocation</param-name>
  4.         <param-value>classpath:log4j.xml</param-value>
  5.     </context-param>
  6.     <context-param>
  7.         <param-name>log4jRefreshInterval</param-name>
  8.         <param-value>60000</param-value>
  9.     </context-param>

這是一種基本配置,spring中很多程式碼使用了不同的日誌介面,既有log4j也有commons-logging,這裡只是強制轉換為log4j!並且,log4j的配置檔案只能放在classpath根路徑。同時,需要通過commons-logging配置將日誌控制權轉交給log4j。同時commons-logging.properties必須放置在classpath根路徑。
commons-logging內容:
Properties程式碼  收藏程式碼
  1. org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger  

最後,記得配置log4j的監聽器:
Xml程式碼  收藏程式碼
  1. <listener>
  2.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  3. </listener>

接下來,我們需要配置兩套配置檔案,applicationContext.xml和servlet.xml。
applicationContext.xml用於對應用層面做整體控制。按照分層思想,統領service層和dao層。servlet.xml則單純控制controller層。
Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans
  3.     xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.     <import
  9.         resource="service.xml"/>
  10.     <import
  11.         resource="dao.xml"/>
  12. </beans>

applicationContext.xml什麼都不幹,它只管涉及到整體需要的配置,並且集中管理。
這裡引入了兩個配置檔案service.xml和dao.xml分別用於業務、資料處理。
service.xml
Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans
  3.     xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.     <context:component-scan
  9.         base-package="org.zlex.spring.service"/>
  10. </beans>

注意,這裡通過<context:component-scan />標籤指定了業務層的基礎包路徑——“org.zlex.spring.service”。也就是說,業務層相關實現均在這一層。這是有必要的分層之一。
dao.xml
Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans
  3.     xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:aop="http://www.springframework.org/schema/aop"
  6.     xmlns:context="http://www.springframework.org/schema/context"
  7.     xmlns:tx="http://www.springframework.org/schema/tx"
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  10.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  12.     <context:component-scan
  13.         base-package="org.zlex.spring.dao"/>
  14. </beans>

dao層如法炮製,包路徑是"org.zlex.spring.dao"。從這個角度看,註解還是很方便的!
最後,我們看看servlet.xml
Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans
  3.     xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.     <context:component-scan
  9.         base-package="org.zlex.spring.controller"/>
  10.     <bean
  11.         id="urlMapping"
  12.         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
  13.     <bean
  14.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  15. </beans>

包路徑配置就不細說了,都是一個概念。最重要的時候後面兩個配置,這將使得註解生效!
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是預設實現,可以不寫,Spring容器預設會預設使用該類。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接關係到多動作控制器配置是否可用!

簡單看一下程式碼結構,如圖:

Account類是來儲存賬戶資訊,屬於域物件,極為簡單,程式碼如下所示:
Account.java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.domain;  
  5. import java.io.Serializable;  
  6. /** 
  7.  *  
  8.  * @author <a href="mailto:[email protected]">樑棟</a> 
  9.  * @version 1.0 
  10.  * @since 1.0 
  11.  */
  12. publicclass Account implements Serializable {  
  13.     /** 
  14.      *  
  15.      */
  16.     privatestaticfinallong serialVersionUID = -533698031946372178L;  
  17.     private String username;  
  18.     private String password;  
  19.     /** 
  20.      * @param username 
  21.      * @param password 
  22.      */
  23.     public Account(String username, String password) {  
  24.         this.username = username;  
  25.         this.password = password;  
  26.     }  
  27.     /** 
  28.      * @return the username 
  29.      */
  30.     public String getUsername() {  
  31.         return username;  
  32.     }  
  33.     /** 
  34.      * @param username the username to set 
  35.      */
  36.     publicvoid setUsername(String username) {  
  37.         this.username = username;  
  38.     }  
  39.     /** 
  40.      * @return the password 
  41.      */
  42.     public String getPassword() {  
  43.         return password;  
  44.     }  
  45.     /** 
  46.      * @param password the password to set 
  47.      */
  48.     publicvoid setPassword(String password) {  
  49.         this.password = password;  
  50.     }  
  51. }  

通常,在構建域物件時,需要考慮該物件可能需要進行網路傳輸,本地快取,因此建議實現序列化介面Serializable 
我們再來看看控制器,這就稍微複雜了一點程式碼如下所示:
AccountController .java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.controller;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.ServletRequestUtils;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.zlex.spring.service.AccountService;  
  13. /** 
  14.  *  
  15.  * @author <a href="mailto:[email protected]">樑棟</a> 
  16.  * @version 1.0 
  17.  * @since 1.0 
  18.  */
  19. @Controller
  20. @RequestMapping("/account.do")  
  21. publicclass AccountController {  
  22.     @Autowired
  23.     private AccountService accountService;  
  24.     @RequestMapping(method = RequestMethod.GET)  
  25.     publicvoid hello(HttpServletRequest request, HttpServletResponse response)  
  26.             throws Exception {  
  27.         String username = ServletRequestUtils.getRequiredStringParameter(  
  28.                 request, "username");  
  29.         String password = ServletRequestUtils.getRequiredStringParameter(  
  30.                 request, "password");  
  31.         System.out.println(accountService.verify(username, password));  
  32.     }  
  33. }  

分段詳述:
Java程式碼  收藏程式碼
  1. @Controller
  2. @RequestMapping("/account.do")  

這兩行註解,@Controller是告訴Spring容器,這是一個控制器類,@RequestMapping("/account.do")是來定義該控制器對應的請求路徑(/account.do)
Java程式碼  收藏程式碼
  1. @Autowired
  2. private AccountService accountService;  

這是用來自動織入業務層實現AccountService,有了這個註解,我們就可以不用寫setAccountService()方法了!
同時,JSR-250標準註解,推薦使用@Resource來代替Spring專有的@Autowired註解。
引用 Spring 不但支援自己定義的@Autowired註解,還支援幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。

  @Resource的作用相當於@Autowired,只不過@Autowired按byType自動注入,而@Resource預設按 byName自動注入罷了。@Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource註解的name屬性解析為bean的名字,而type屬性則解析為bean的型別。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。

  @Resource裝配順序

  1. 如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則丟擲異常

  2. 如果指定了name,則從上下文中查詢名稱(id)匹配的bean進行裝配,找不到則丟擲異常

  3. 如果指定了type,則從上下文中找到型別匹配的唯一bean進行裝配,找不到或者找到多個,都會丟擲異常

  4. 如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有匹配,則回退為一個原始型別(UserDao)進行匹配,如果匹配則自動裝配;

  1.6. @PostConstruct(JSR-250)

在方法上加上註解@PostConstruct,這個方法就會在Bean初始化之後被Spring容器執行(注:Bean初始化包括,例項化Bean,並裝配Bean的屬性(依賴注入))。

這有點像ORM最終被JPA一統天下的意思! 大家知道就可以了,具體使用何種標準由專案說了算!
最後,來看看核心方法:
Java程式碼  收藏程式碼
  1. @RequestMapping(method = RequestMethod.GET)  
  2. publicvoid hello(HttpServletRequest request, HttpServletResponse response)  
  3.         throws Exception {  
  4.     String username = ServletRequestUtils.getRequiredStringParameter(  
  5.             request, "username");  
  6.     String password = ServletRequestUtils.getRequiredStringParameter(  
  7.             request, "password");  
  8.     System.out.println(accountService.verify(username, password));  
  9. }  

註解@RequestMapping(method = RequestMethod.GET)指定了訪問方法型別。
注意,如果沒有用這個註解標識方法,Spring容器將不知道那個方法可以用於處理get請求!
對於方法名,我們可以隨意定!方法中的引數,類似於“HttpServletRequest request, HttpServletResponse response”,只要你需要方法可以是有參也可以是無參!
解析來看Service層,分為介面和實現:
AccountService.java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.service;  
  5. /** 
  6.  *  
  7.  * @author <a href="mailto:[email protected]">樑棟</a> 
  8.  * @version 1.0 
  9.  * @since 1.0 
  10.  */
  11. publicinterface AccountService {  
  12.     /** 
  13.      * 驗證使用者身份 
  14.      *  
  15.      * @param username 
  16.      * @param password 
  17.      * @return 
  18.      */
  19.     boolean verify(String username, String password);  
  20. }  

介面不需要任何Spring註解相關的東西,它就是一個簡單的介面!
重要的部分在於實現層,如下所示:
AccountServiceImpl.java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.service.impl;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Service;  
  7. import org.springframework.transaction.annotation.Transactional;  
  8. import org.zlex.spring.dao.AccountDao;  
  9. import org.zlex.spring.domain.Account;  
  10. import org.zlex.spring.service.AccountService;  
  11. /** 
  12.  *  
  13.  * @author <a href="mailto:[email protected]">樑棟</a> 
  14.  * @version 1.0 
  15.  * @since 1.0 
  16.  */
  17. @Service
  18. @Transactional
  19. publicclass AccountServiceImpl implements AccountService {  
  20.     @Autowired
  21.     private AccountDao accountDao;  
  22.     /* 
  23.      * (non-Javadoc) 
  24.      *  
  25.      * @see org.zlex.spring.service.AccountService#verify(java.lang.String, 
  26.      * java.lang.String) 
  27.      */
  28.     @Override
  29.     publicboolean verify(String username, String password) {  
  30.         Account account = accountDao.read(username);  
  31.         if (password.equals(account.getPassword())) {  
  32.             returntrue;  
  33.         } else {  
  34.             returnfalse;  
  35.         }  
  36.     }  
  37. }  

注意以下內容:
Java程式碼  收藏程式碼
  1. @Service
  2. @Transactional

註解@Service用於標識這是一個Service層實現,@Transactional用於控制事務,將事務定位在業務層,這是非常務實的做法!
接下來,我們來看持久層:AccountDao和AccountDaoImpl類
AccountDao.java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.dao;  
  5. import org.zlex.spring.domain.Account;  
  6. /** 
  7.  *  
  8.  * @author <a href="mailto:[email protected]">樑棟</a> 
  9.  * @version 1.0 
  10.  * @since 1.0 
  11.  */
  12. publicinterface AccountDao {  
  13.     /** 
  14.      * 讀取使用者資訊 
  15.      *  
  16.      * @param username 
  17.      * @return 
  18.      */
  19.     Account read(String username);  
  20. }  

這個介面就是簡單的資料提取,無需任何Spring註解有關的東西!
再看其實現類:
AccountDaoImpl.java
Java程式碼  收藏程式碼
  1. /** 
  2.  * 2010-1-23 
  3.  */
  4. package org.zlex.spring.dao.impl;  
  5. import org.springframework.stereotype.Repository;  
  6. import org.zlex.spring.dao.AccountDao;  
  7. import org.zlex.spring.domain.Account;  
  8. /** 
  9.  *  
  10.  * @author <a href="mailto:[email protected]">樑棟</a> 
  11.  * @version 1.0 
  12.  * @since 1.0 
  13.  */
  14. @Repository
  15. publicclass AccountDaoImpl implements AccountDao {  
  16.     /* (non-Javadoc) 
  17.      * @see org.zlex.spring.dao.AccountDao#read(java.lang.String) 
  18.      */
  19.     @Override
  20.     public Account read(String username) {  
  21.         returnnew Account(username,"wolf");  
  22.     }  
  23. }  

這裡只需要注意註解:
Java程式碼  收藏程式碼
  1. @Repository

意為持久層,Dao實現這層我沒有過於細緻的介紹通過註解呼叫ORM或是JDBC來完成實現,這些內容後續細述!
這裡我們沒有提到註解@Component,共有4種“元件”式註解:
引用  
@Component:可裝載元件 
@Repository:持久層元件 
@Service:服務層元件 
@Controller:控制層元件

這樣spring容器就完成了控制層、業務層和持久層的構建。
啟動應用,訪問http://localhost:8080/spring/account.do?username=snow&password=wolf
觀察控制檯,如果得到包含“true”的輸出,本次構建就成功了!

程式碼見附件!

順便說一句:在Spring之前的XML配置中,如果你想在一個類中獲得檔案可以通過在xml配置這個類的某個屬性。在註解的方式(Spring3.0)中,你可以使用@Value來指定這個檔案。
例如,我們想要在一個類中獲得一個檔案,可以這樣寫:
Java程式碼  收藏程式碼
  1. @Value("/WEB-INF/database.properties")  
  2. private File databaseConfig;  


如果這個properties檔案已經正常在容器中載入,可以直接這樣寫:

Java程式碼  收藏程式碼
  1. @Value("${jdbc.url}")   
  2. private String url;  

獲得這個url引數! 

容器中載入這個Properties檔案:
Xml程式碼  收藏程式碼
  1. <util:propertiesid="jdbc"location="/WEB-INF/database.properties"/>


這樣,我們就能通過註解@Value獲得/WEB-INF/database.properties這個檔案!
如果我們想要獲得注入在xml中的某個類,例如dataSource(<bean id ="dataSource">)可以在註解的類中這麼寫:
Java程式碼  收藏程式碼
  1. @Resource(name = "dataSource")  
  2. private BasicDataSource dataSource;  


如果只有這麼一個類使用該配置檔案:
Java程式碼  收藏程式碼
  1. @ImportResource("/WEB-INF/database.properties")  
  2. publicclass AccountDaoImpl extends AccountDao {  

就這麼簡單!

相關推薦

Spring註解學習手札構建web應用

近來工作發生了一些變化,有必要學習一下Spring註解了! 網上找了一些個例子,總的說來比較土,大多數是轉載摘抄,按照提示弄下來根本都執行不了,索性自己趟一遍這渾水,在這裡留下些個印記。 這次,先來構建一個極為簡單的web應用,從controller到dao。不考慮具體實現

Spring 註解學習手札 構建簡單Web應用

轉載自 http://snowolf.iteye.com/blog/577989 我們將用到如下jar包:  引用 aopalliance-1.0.jar  commons-logging-1.1.1.jar  log4j-1.2.15.ja

Spring 註解學習手札 持久層淺析

這裡將用到以下幾個包: 引用 aopalliance-1.0.jar commons-collections.jar commons-dbcp.jar commons-logging-1.1.1.jar commons-pool.jar jstl.jar log4j-1.

Spring 註解學習手札補遺——@ExceptionHandler

Spring註解,改變了我的開發思路。前段時間,用@RequestBody,@ResponseBody,不費吹灰之力就解決了JSon自動繫結。接著就發現,如果遇到RuntimeException,需要給出一個預設返回JSON。 以前都是用SimpleMappingExcep

Spring學習手札

Spring能做什麼 1. 能根據配置檔案建立及組裝物件之間的依賴關係; 2. 面向切面程式設計,能幫助我們無耦合的實現日誌記錄,效能統計,安全控制等; 3. 提供第三方資料訪問框架(如Hibernate),而且自己也提供了一套JDBC訪問模板方便訪問資料庫; 4. 非常簡單的管理資料庫事務;

Spring Boot學習筆記——專案構建-使用-部署

Spring Boot簡介很榮幸剛開始學習java框架就接觸到了spring boot,並得到大神的指點,通過查閱資料以及教程學會在實際專案中去使用它,相對於其他框架只有一個字來形容 - “爽”。最近也

spring boot學習系列

web服務器 應用程序 spring 控制器 做什麽 spring boot開發第一個應用程序1、spring boot是什麽?2、spring boot容易上手嗎?寫這篇文章技術文章,主要是記錄日常的學習以及理解。我們重新認識一下spring假設你受命使用spring開發一個簡單的hel

1、spring-boot學習筆記簡單入門

ava project nal run plugin mailto 5.4 安全 class a 一、新建普通Maven工程 pom.xml <parent> <groupId>org.springframework.boot</gr

Spring Boot 學習筆記—— 快速搭建Spring Boot專案

一、前言 Spring Boot是Spring系列框架的整合,使用“習慣優於配置”的理念快速搭建專案,在專案打包時還能根據需求打包為jar(內建servlet容器)或war檔案,相對以前的Spring、Spring MVC來說Spring Boot並沒有技術上的升級,而是為我們做好了大部

Spring AOP學習筆記:基礎概念

AOP產生背景 AOP(Aspect Oriented Programming),即面向切面程式設計,可以說是OOP(Object Oriented Programming,面向物件程式設計)的補充和完善。OOP引入封裝、繼承、多型等概念來建立一種物件層次結構,用於模擬公共行為的一個集合。不

Spring Boot 學習總結 ---入口類和@SpringBootApplication

入口類和@SpringBootApplication SpringBoot通常有一個名為*Application的入口類,入口類裡有一個main方法,這個main方法是一個標準的java應用的入口方法。在main方法中使用SpringApplication.run(*App

Spring原始碼學習筆記 bean是怎麼生成的

bean 實在 bean 重新整理過程中產生的,首先我們看下 bean 的重新整理方法。下面是 AbstractApplicationContext 的 refresh 方法。 @Override public void refresh() throws

Spring Boot學習實踐1建立一個簡單的spring boot應用

一、使用idea建立一個簡單的Spring Boot應用程式 環境準備: idea:2018.2 jdk: 1.8 spring boot:是2.0版本以上的 以上環境可以根據實際情況去調整。 (1)首先找到idea建立應用的New Project,選擇好

SpringCloud學習心得 構建最基礎的SpringCloud專案

首先本系列部落格參照 史上最簡單的 SpringCloud 教程,地址:https://blog.csdn.net/forezp/article/details/70148833根據工作需要,筆者最近研究SpringCloud微服務框架,將最近的學習過程寫下來供大家參考。好,

Spring Boot學習日誌Hello World

目錄 一,系統環境 JDK:1.8.0_144 maven:3.5.2 二,使用IntelliJ IDEA建立專案 1,選擇專案型別 2,編輯專案資訊 這裡packaging可以選擇jar 或者 war 3,選擇需要的war包

Spring MVC學習記錄——網站基礎知識1

Spring MVC學習之——網路架構及演變過程 1.軟體的三大型別:單機型別、CS型別、BS型別。 單機型別:曾經的主要軟體型別,不聯網,實現電腦的基本功能,如打字、畫圖。 CS型別:單機之後為了統一管理軟體的資料,產生了客戶端和伺服器端,客戶端管理業務,伺服器端管理資料

Spring Boot 學習筆記入門

Spring boot 簡介 1、Spring boot是Spring家族中的一個全新的框架,它用來簡化Spring應用程式的建立和開發過程,也可以說Spring boot能簡化我們之前採用Spring mvc + Spring + MyBatis 框架進行開發的過程; 2、在以往我們採用 Spring

Spring Cloud 學習筆記——入門、特徵、配置

目錄 0 放在前面 0.1 參考文件 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc/ http://projects.spring.io/spring-cloud/ 0.2 maven配置 &

Spring註解驅動開發

[email protected]註解 這個相當於配置檔案,即告訴spring這個一個配置類。 [email protected] 給容器註冊一個Bean;型別為返回值的型別,id預設是用方法名作為id. @Configurat

spring boot 學習筆錄

(1)、spring Framework(Core):spring專案的核心。Spring Framework中包含了一系列的IoC容器的設計,提供了依賴反轉模式的實現。同時,還集成了AOP功能,包含了其他Spring的基本模組,如MVC,JDBC,事務處理模組的實現。 (2)、spring Web Flo