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

一、使用外部属性

  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() + "发送完一封邮件");
   }
}
时间: 2024-08-15 08:46:02

使用外部属性文件和spring的事件的相关文章

spring 使用外部属性文件

概要: 使用外部属性文件 在配置文件中配置Bean时.有时须要在Bean的配置里混入系统部署的细节信息(比如:文件路径.数据源配置信息等)而这些部署细节实际上须要和Bean配置相分离 Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,整个处理器同意用户将Bean配置的部分内容外移到属性文件中.能够在Bean配置文件中使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件中载入属性,并使用

十八 Spring的JDBC模板:引入外部属性文件

配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: 原文地址:https://www.cnblogs.com/ltfxy/p/9888591.html

spring4学习:使用外部属性文件

在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离: spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的内容外移到属性文件中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换属性变量: S

Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.kejian.spring.bean.autowire.Student" p:name="Tony" autowire="byName"></bean> 1.2 类型 byName 目标bean的id与属性名一置,若不匹配置为null byTy

Spring---Bean使用外部属性文件

在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离 Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 为变量赋值, PropertyPlaceholderConfigurer

Spring_使用外部属性文件

beans-properties.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.spr

使用外部属性文件

1.在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(如:文件路径,数据源配置信息等).而这些部署细节实际上需要和Bean配置相分离. 2.Spring提供了一个PropertyPlacehoiderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中.可以在Bean配置文件里使用形式为${var}的变量,PropertyPlacehoiderConfigurer从属性文件里加载属性,并使用这些属性来替换变

Spring 应用外部属性文件 配置 context 错误

在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析文件,当然你必须在把Spring相应的包导入正确的情况下. 解决方案就是如下: xmlns:context="http://www.springframework.org/schema/context" 同时在xsi:schemaLocation这个字符串中添加context相关的解析文件

22Spring_JdbcTemplatem模板工具类的使用——使用外部属性文件来配置(properties)

前一篇文章写得是xml文件来配置数据库连接的.但是为了方便,我们实际中采用的是properties文件的方式来配置数据库的.修改properties 文件 会比 修改 xml文件 方便. 做法是: 将经常需要修属性参数值,配置到独立properties文件 ,然后在xml文件引入properties 先给出整个案例的结构图: 第一步:编写properties文件.(new 新建一个db.properties文件) 内容如下: driver= com.mysql.jdbc.Driver url=