posts - 93,  comments - 2,  trackbacks - 0
            2020年4月9日
          1.必須安裝nodejs
          2.安裝cnpm用cnpm替代npm
              地址:http://npm.taobao.org/
          安裝cnpm:
          npm install -g cnpm --registry=https://registry.npm.taobao.org

          3、用yarn替代npm
          yarn的安裝:
          第一種方法:參考官方文檔https://yarn.bootcss.com/
          第二種方法:cnpm install -g yarn  或者 npm install -g yarn
          4、搭建React開發環境的第一種方法(老-現在推薦):
          https://reactjs.org/docs/create-a-new-react-app.html
          1、必須要安裝nodejs     注意:安裝nodejs穩定版本      教程中的nodejs版本:v8.11.2            教程中的npm版本:v5.6.0
          2.安裝腳手架工具   (單文件組件項目生成工具)   只需要安裝一次
          npm install -g create-react-app   /  cnpm install -g create-react-app
          3.創建項目   (可能創建多次)
          找到項目要創建的目錄:
                          create-react-app reactdemo
          4.cd  到項目里面
                  cd  reactdemo
                          npm start             yarn start運行項目
                          npm run build         yarn build 生成項目
          5、搭建React的開發環境的第二種方法(新-未來推薦):
                  https://reactjs.org/docs/create-a-new-react-app.html
                  1、必須要安裝nodejs     注意:安裝nodejs穩定版本      教程中的nodejs版本:v8.11.2            教程中的npm版本:v5.6.0
                  2.安裝腳手架工具并創建項目
                      找到項目要創建的目錄執行:
          npx create-react-app reactdemo
          4.cd  到項目里面
                  cd  reactdemo
                          npm start  運行項目(調試)
                          npm run build 生成項目(發布)
          npx介紹:
          npm v5.2.0引入的一條命令(npx),引入這個命令的目的是為了提升開發者使用包內提供的命令行工具的體驗。
          詳情:
                  npx create-react-app reactdemo這條命令會臨時安裝 create-react-app 包,命令完成后create-react-app 會刪掉,不會出現在 global 中。下次再執行,還是會重新臨時安裝。
          npx 會幫你執行依賴包里的二進制文件。
                  再比如 npx http-server 可以一句話幫你開啟一個靜態服務器
          posted @ 2020-04-16 15:25 Terry Zou 閱讀(315) | 評論 (0)編輯 收藏
          @PostConstruct
          PostConstruct注釋用于在完成依賴項注入以執行任何初始化之后需要執行的方法。必須在類投入使用之前調用此方法。
          所有支持依賴注入的類都必須支持此注釋。即使類沒有請求注入任何資源,也必須調用使用PostConstruct注釋的方法。
          只有一個方法可以使用此批注進行批注。
          應用PostConstruct注釋的方法必須滿足以下所有條件:除了攔截器之外,方法絕不能有任何參數,在這種情況下它采用Interceptor規范定義的InvocationContext對象。
          在攔截器類上定義的方法必須具有以下簽名之一:
          void <METHOD>(InvocationContext)Object <METHOD>(InvocationContext)拋出異常注意:
          PostConstruct攔截器方法不能拋出應用程序異常,但可以聲明它拋出檢查異常,包括java.lang.Exception,
          如果相同的攔截器方法除了生命周期事件之外插入業務或超時方法。
          如果PostConstruct攔截器方法返回一個值,容器將忽略它。
          在非攔截器類上定義的方法必須具有以下簽名:void <METHOD>()應用PostConstruct的方法可以是publicprotectedpackage privateprivate。
          除應用程序客戶端外,該方法絕不能是靜態的。
          該方法可能是最終的。如果該方法拋出一個未經檢查的異常,那么該類絕不能投入使用,除非EJB可以處理異常甚至從它們恢復的EJB

          然后就會思考問題,這個注釋是修飾初始化之后需要執行的方法,那么它和@Autowired、構造函數的執行順序是什么呢?(當然注釋中已經說明了PostConstruct注釋用于在完成依賴項注入之后)
          @Service
          public class BeanA {

              @Autowired
              private BeanB beanB;

              public BeanA() {
                  System.out.println("這是Bean A 的構造方法");
              }
              @PostConstruct
              private void init() {
                  System.out.println("這是BeanA的 init 方法");
                  beanB.testB();
              }
          }
          @Service
          public class BeanB {

              @PostConstruct
              private void init() {
                  System.out.println("這是BeanB 的init 方法");
              }
              public BeanB() {
                  System.out.println("這是Bean B的 構造方法");
              }
              void testB() {
                  System.out.println("這是Bean B 的 testB 方法");
              }
          }

          啟動后輸出:
          這是Bean A 的構造方法 
          這是Bean B的 構造方法
          這是BeanB 的init 方法
          這是BeanA的 init 方法
          這是Bean B 的 testB 方法

          所以得到結論: 構造方法 > @Autowired > @PostConstruct
          posted @ 2020-04-09 15:29 Terry Zou 閱讀(315) | 評論 (0)編輯 收藏
          1、ApplicationContext
          Spring的核心,Context我們通常解釋為上下文環境。ApplicationContext則是應用的容器。 Spring把Bean(object)放在容器中,需要用就通過get方法取出來。在ApplicationContext接口的眾多實現類中,有3個是我們經常用到的(見表1-1),并且使用這3個實現類也基本能滿足我們Java EE應用開發中的絕大部分需求。
          表1-1 ApplicationContext接口的常用實現類介紹
          ClassPathXmlApplicationContext
          從類路徑ClassPath中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實例化工作。例如: //裝載單個配置文件實例化ApplicationContext容器
          ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
          //裝載多個配置文件實例化ApplicationContext容器
          String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
          ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
          FileSystemXmlApplicationContext
          從指定的文件系統路徑中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實例化工作。例如://裝載單個配置文件實例化ApplicationContext容器
          ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml");
          //裝載多個配置文件實例化ApplicationContext容器
          String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
          ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);
          XmlWebApplicationContext
          從Web應用中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實例化工作。這是為Web工程量身定制的,使用WebApplicationContextUtils類的getRequiredWebApplicationContext方法可在JSP與Servlet中取得IoC容器的引用
          2、ApplicationEvent
          是個抽象類,里面只有一個構造函數和一個長整型的timestamp。其源碼如下

          public abstract class ApplicationEvent extends EventObject {
           
              /** use serialVersionUID from Spring 1.2 for interoperability */
              private static final long serialVersionUID = 7099057708183571937L;
           
              /** System time when the event happened */
              private final long timestamp;
           
              /**
               * Create a new ApplicationEvent.
               * 
          @param source the object on which the event initially occurred (never {@code null})
               
          */
              public ApplicationEvent(Object source) {
                  super(source);
                  this.timestamp = System.currentTimeMillis();
              }
           
              /**
               * Return the system time in milliseconds when the event happened.
               
          */
              public final long getTimestamp() {
                  return this.timestamp;
              }
          }

          3、ApplicationListener

          是一個接口,里面只有一個onApplicationEvent方法。如果在上下文中部署一個實現了ApplicationListener接口的bean,那么每當在一個ApplicationEvent發布到 ApplicationContext時,調用ApplicationContext.publishEvent()方法,這個bean得到通知。類似于Oberver設計模式。
          其源碼如下:

          public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
              /**
               * Handle an application event.
               * 
          @param event the event to respond to
               
          */
              void onApplicationEvent(E event);
           
          }
          下面舉個例子
          自定義事件NotifyEvent:
          import org.springframework.context.ApplicationEvent;

          public class NotifyEvent  extends ApplicationEvent  {
              private String email;
              private String content;
              public NotifyEvent(Object source){
                  super(source);
              }

              public NotifyEvent(Object source,String email,String content){
                  super(source);
                  this.email = email;
                  this.content = content;
              }

              public String getEmail() {
                  return email;
              }

              public void setEmail(String email) {
                  this.email = email;
              }

              public String getContent() {
                  return content;
              }

              public void setContent(String content) {
                  this.content = content;
              }
          }

          定義監聽器NotifyListener:
          import org.springframework.context.ApplicationListener;
          import org.springframework.context.annotation.Configuration;

          @Configuration
          public class NotifyListener implements ApplicationListener<NotifyEvent>{
              @Override
              public void onApplicationEvent(NotifyEvent event) {
                  System.out.println("郵件地址:" + event.getEmail());
                  System.out.println("郵件內容:" + event.getContent());
              }
          }

          單元測試類ListenerTest:
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.boot.test.context.SpringBootTest;
          import org.springframework.test.context.junit4.SpringRunner;
          import org.springframework.web.context.WebApplicationContext;

          @RunWith(SpringRunner.class)
          @SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
          public class ListenerTest {
              @Autowired
              private WebApplicationContext webApplicationContext;

              @Test
              public void testListener(){
                  NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
                  webApplicationContext.publishEvent(event);
              }
          }
          posted @ 2020-04-09 14:47 Terry Zou 閱讀(1292) | 評論 (0)編輯 收藏

          之前用戶使用的是3個注解注解他們的main類。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于這些注解一般都是一起使用,spring boot提供了一個統一的注解@SpringBootApplication。

          @SpringBootApplication = (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。

          @SpringBootApplication 
          public class ApplicationMain { 
              public static void main(String[] args) { 
                  SpringApplication.run(Application.class, args); 
              } 
          }

          分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
          1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個注解就可以創建一個簡單的spring配置類,可以用來替代相應的xml配置文件。

          <beans> 
              <bean id = "car" class="com.test.Car"> 
                  <property name="wheel" ref = "wheel"></property> 
              </bean> 
              <bean id = "wheel" class="com.test.Wheel"></bean> 
          </beans> 

           相當于:

          @Configuration 
          public class Conf { 
              @Bean 
              public Car car() { 
                  Car car = new Car(); 
                  car.setWheel(wheel()); 
                  return car; 
              } 
              @Bean  
              public Wheel wheel() { 
                  return new Wheel(); 
              } 
          }

          @Configuration的注解類標識這個類可以使用Spring IoC容器作為bean定義的來源。@Bean注解告訴Spring,一個帶有@Bean的注解方法將返回一個對象,該對象應該被注冊為在Spring應用程序上下文中的bean。

          2、@EnableAutoConfiguration:能夠自動配置spring的上下文,試圖猜測和配置你想要的bean類,通常會自動根據你的類路徑和你的bean定義自動配置。

          3、@ComponentScan:會自動掃描指定包下的全部標有@Component的類,并注冊成bean,當然包括@Component下的子注解@Service,@Repository,@Controller。


          posted @ 2020-04-09 09:10 Terry Zou 閱讀(133) | 評論 (0)編輯 收藏
          <2020年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青海省| 鲜城| 松原市| 行唐县| 沾化县| 灯塔市| 古浪县| 科技| 东安县| 河北区| 三台县| 丰原市| 彰武县| 阳曲县| 怀来县| 徐州市| 卢湾区| 文安县| 兰溪市| 阳曲县| 瓮安县| 宁远县| 亳州市| 康保县| 稻城县| 璧山县| 兰西县| 涡阳县| 韩城市| 永州市| 靖州| 余干县| 迭部县| 库伦旗| 蓝山县| 崇州市| 武功县| 海门市| 哈尔滨市| 大悟县| 桃源县|