溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用外部屬性文件和spring的事件

發布時間:2020-07-12 18:23:56 來源:網絡 閱讀:385 作者:cnslp 欄目:開發技術

一、使用外部屬性

  1. 使用PropertyPlaceholderConfigurer引用屬性文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="classpath:com/smart/place/jdbc.properties"></property>
   <property name="fileEncoding" value="UTF-8"></property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="${driverClassName}"></property>
   <property name="url" value="${url}"></property>
   <property name="username" value="${userName}"></property>
   <property name="password" value="${password}"></property>
</bean>

先引入屬性文件,再通過${KEY}來使用


2.使用context:property-placeholder來引入

<context:property-placeholder location="classpath:hibernate.properties" />


3.以下附錄一個DES加密的程序

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESUtils {
   private static Key key;
   private static String KEY_STR = "myKey";
   static {
      try {
         KeyGenerator generator = KeyGenerator.getInstance("DES");
         generator.init(new SecureRandom(KEY_STR.getBytes()));
         key = generator.generateKey();
         generator = null;
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   /**
    * 對str進行DES加密
    * 
    * @param str
    * @return
    */
   public static String getEncryptString(String str) {
      BASE64Encoder base64en = new BASE64Encoder();
      try {
         byte[] strBytes = str.getBytes("UTF8");
         Cipher cipher = Cipher.getInstance("DES");
         cipher.init(Cipher.ENCRYPT_MODE, key);
         byte[] encryptStrBytes = cipher.doFinal(strBytes);
         return base64en.encode(encryptStrBytes);
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   /**
    * 對str進行DES解密
    * 
    * @param str
    * @return
    */
   public static String getDecryptString(String str) {
      BASE64Decoder base64De = new BASE64Decoder();
      try {
         byte[] strBytes = base64De.decodeBuffer(str);
         Cipher cipher = Cipher.getInstance("DES");
         cipher.init(Cipher.DECRYPT_MODE, key);
         byte[] decryptStrBytes = cipher.doFinal(strBytes);
         return new String(decryptStrBytes, "UTF8");
      } catch (Exception e) {
         throw new RuntimeException(e);
      }

   }

}


二、容器事件

  1. 事件類:ApplicationEvent的唯一構造函數ApplicationEvent(Object Source)通過Source指定事件源。它有兩個子類ApplicationContextEvent:容器事件。RequestHandleEvent:與web相關的事件,當http請求處理后,產生該事件,只有在web.xml中定義了DispatcherServlet時才會產生該事件。

  2. 事件監聽器接口:ApplicationListener接口,該接口只有一個方法onApplicationEvent(E event)該方法接受ApplicationEvent事件對象,進行事件處理。

public class MailSender implements ApplicationContextAware {

   private ApplicationContext ctx ;
    //ApplicationContextAware的接口方法,以便容器啟動時,注入容器實例。
   public void setApplicationContext(ApplicationContext ctx)
         throws BeansException {
      this.ctx = ctx;
   }

   public void sendMail(String to){
      System.out.println("MailSender:模擬發送郵件...");
      MailSendEvent mse = new MailSendEvent(this.ctx,to);
      //向容器中所有事件監聽器發送事件
      ctx.publishEvent(mse);
   }
}
public class MailSendEvent extends ApplicationContextEvent {
   private String to;
   
   public MailSendEvent(ApplicationContext source, String to) {
      super(source);
      this.to = to;
   }
   public String getTo() {
      
      return this.to;
   }
}
public class MailSendListener implements ApplicationListener<MailSendEvent>{

   public void onApplicationEvent(MailSendEvent event) {
         MailSendEvent mse = (MailSendEvent) event;
         System.out.println("MailSendListener:向" + mse.getTo() + "發送完一封郵件");
   }
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女