一、使用外部屬性
使用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);
}
}
}二、容器事件
事件類:ApplicationEvent的唯一構造函數ApplicationEvent(Object Source)通過Source指定事件源。它有兩個子類ApplicationContextEvent:容器事件。RequestHandleEvent:與web相關的事件,當http請求處理后,產生該事件,只有在web.xml中定義了DispatcherServlet時才會產生該事件。
事件監聽器接口: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() + "發送完一封郵件");
}
}免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。