import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import oracle.jdbc.pool.OracleDataSource; import org.apache.commons.lang.StringUtils; import com.opensymphony.module.propertyset.PropertySet; import com.opensymphony.module.propertyset.PropertySetManager; public class PropertySetDemo2 { public static void main(String[] args) throws Exception { PropertySetDemo2 demo = new PropertySetDemo2(); demo.initJNDI(); demo.jdbcPsDemo(); } public void initJNDI() throws NamingException, SQLException { //生成一个DataSource实例 OracleDataSource ds = new OracleDataSource(); ds.setDriverType("oracle.jdbc.driver.OracleDriver"); ds.setURL("jdbc:oracle:thin:@localhost:1521:oracl"); ds.setUser("user"); ds.setPassword("password"); //注册JNDI System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); Context context = new InitialContext(); context.bind("awp1", ds); } public void jdbcPsDemo() { System.out.println(StringUtils.center("JDBC PropertySet Demo", 80, '*')); Map<String, String> map = new HashMap<String, String>(); map.put("globalKey", "a");//不可缺省。不同项目或不同模块可以用不同的globalKey,并且持久化到同一个表中 PropertySet ps = PropertySetManager.getInstance("jdbc", map); ps.setBoolean("BooleanPS", true); ps.setString("name", "Jack"); ps.setDouble("dvalue", 0.1234567); System.out.println(ps.getBoolean("BooleanPS")); System.out.println(ps.getString("name")); System.out.println(ps.getDouble("dvalue")); System.out.println(StringUtils.repeat("*", 80)); } }
?
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.HashMap; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import com.opensymphony.module.propertyset.PropertySet; import com.opensymphony.module.propertyset.PropertySetManager; import com.opensymphony.module.propertyset.xml.XMLPropertySet; public class PropertySetDemo { public static void main(String[] args) throws Exception { PropertySetDemo demo = new PropertySetDemo(); demo.mapPsDemo(); demo.memoryPsDemo(); demo.xmlPsDemo(); } public void memoryPsDemo() { System.out.println(StringUtils.center("Memory PropertySet Demo", 80, '*')); // memory是配置文件中定义的:<propertyset name="memory" // class="com.opensymphony.module.propertyset.memory.MemoryPropertySet"/> PropertySet ps = PropertySetManager.getInstance("memory", null); ps.setBoolean("BooleanPS", true); ps.setString("name", "Tom"); System.out.println(ps.getBoolean("BooleanPS")); System.out.println(ps.getString("name")); System.out.println(StringUtils.repeat("*", 80)); } public void mapPsDemo() { System.out.println(StringUtils.center("Map PropertySet Demo", 80, '*')); @SuppressWarnings("unchecked") HashMap map = new HashMap(); // memory是配置文件中定义的:<propertyset name="memory" // class="com.opensymphony.module.propertyset.memory.MemoryPropertySet"/> PropertySet ps = PropertySetManager.getInstance("map", map); ps.setBoolean("BooleanPS", true); ps.setString("name", "Tom"); System.out.println(ps.getBoolean("BooleanPS")); System.out.println(ps.getString("name")); System.out.println(StringUtils.repeat("*", 80)); } public void xmlPsDemo() throws Exception { System.out.println(StringUtils.center("XML PropertySet Demo", 80, '*')); PropertySet ps = PropertySetManager.getInstance("xml", null); InputStream is = new FileInputStream("config.xml"); ((XMLPropertySet) ps).load(is); IOUtils.closeQuietly(is); System.out.println(ps.getBoolean("aa")); System.out.println(ps.getInt("ab")); System.out.println(ps.getLong("ac")); System.out.println(ps.getDouble("ad")); System.