一、使用外部属性

使用PropertyPlaceholderConfigurer引用属性文件

<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="location"value="classpath:com/smart/place/jdbc.properties"></property><propertyname="fileEncoding"value="UTF-8"></property></bean><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><propertyname="driverClassName"value="${driverClassName}"></property><propertyname="url"value="${url}"></property><propertyname="username"value="${userName}"></property><propertyname="password"value="${password}"></property></bean>

先引入属性文件,再通过${KEY}来使用


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

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


3.以下附录一个DES加密的程序

importjava.security.Key;importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;publicclassDESUtils{privatestaticKeykey;privatestaticStringKEY_STR="myKey";static{try{KeyGeneratorgenerator=KeyGenerator.getInstance("DES");generator.init(newSecureRandom(KEY_STR.getBytes()));key=generator.generateKey();generator=null;}catch(Exceptione){thrownewRuntimeException(e);}}/***对str进行DES加密**@paramstr*@return*/publicstaticStringgetEncryptString(Stringstr){BASE64Encoderbase64en=newBASE64Encoder();try{byte[]strBytes=str.getBytes("UTF8");Ciphercipher=Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE,key);byte[]encryptStrBytes=cipher.doFinal(strBytes);returnbase64en.encode(encryptStrBytes);}catch(Exceptione){thrownewRuntimeException(e);}}/***对str进行DES解密**@paramstr*@return*/publicstaticStringgetDecryptString(Stringstr){BASE64Decoderbase64De=newBASE64Decoder();try{byte[]strBytes=base64De.decodeBuffer(str);Ciphercipher=Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE,key);byte[]decryptStrBytes=cipher.doFinal(strBytes);returnnewString(decryptStrBytes,"UTF8");}catch(Exceptione){thrownewRuntimeException(e);}}}


二、容器事件

事件类:ApplicationEvent的唯一构造函数ApplicationEvent(Object Source)通过Source指定事件源。它有两个子类ApplicationContextEvent:容器事件。RequestHandleEvent:与web相关的事件,当http请求处理后,产生该事件,只有在web.xml中定义了DispatcherServlet时才会产生该事件。

事件监听器接口:ApplicationListener接口,该接口只有一个方法onApplicationEvent(E event)该方法接受ApplicationEvent事件对象,进行事件处理。

publicclassMailSenderimplementsApplicationContextAware{privateApplicationContextctx;//ApplicationContextAware的接口方法,以便容器启动时,注入容器实例。publicvoidsetApplicationContext(ApplicationContextctx)throwsBeansException{this.ctx=ctx;}publicvoidsendMail(Stringto){System.out.println("MailSender:模拟发送邮件...");MailSendEventmse=newMailSendEvent(this.ctx,to);//向容器中所有事件监听器发送事件ctx.publishEvent(mse);}}

publicclassMailSendEventextendsApplicationContextEvent{privateStringto;publicMailSendEvent(ApplicationContextsource,Stringto){super(source);this.to=to;}publicStringgetTo(){returnthis.to;}}

publicclassMailSendListenerimplementsApplicationListener<MailSendEvent>{publicvoidonApplicationEvent(MailSendEventevent){MailSendEventmse=(MailSendEvent)event;System.out.println("MailSendListener:向"+mse.getTo()+"发送完一封邮件");}}