package com.luffy.code;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.Resource;
public class App {
private static ApplicationContext app;
public static void main(String[] args) {
app = new StaticApplicationContext();
Resource res = app.getResource("classpath:sample.txt"); // 相对路径
if (res.exists()) {
try {
System.out.println("URI:" + res.getURI());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("*" + res.getFilename() + "* ==========");
InputStream input = null;
try {
input = res.getInputStream();
BufferedInputStream buf_input = new BufferedInputStream(input);
byte[] bytes = new byte[10240];
buf_input.read(bytes);
String xml_str = new String(bytes).trim();
System.out.println(xml_str);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("* end * ==========");
} else {
System.out.println("not found:" + res);
}
}
}
package jp.tuyano.spring.sample2;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
private static ApplicationContext app;
private static Properties mybeanProps;
public static void main(String[] args) {
app = new ClassPathXmlApplicationContext("classpath:/bean.xml");
// 通过Id取bean对象
MyBean bean = (MyBean) app.getBean("bean1");
// 通过Id取properties文件
mybeanProps = (Properties)app.getBean("mybeanprops");
// 读取
String from = mybeanProps.getProperty("keeper.from");
String to = mybeanProps.getProperty("keeper.to");
MyBeanKeeper keeper = new MyBeanKeeper(bean, from, to);
System.out.println(keeper);
}
}
classpath:bean.properties
PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
package jp.tuyano.spring.sample2;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
private static ApplicationContext app;
public static void main(String[] args) {
app = new ClassPathXmlApplicationContext("classpath:/bean.xml");
MyBean bean = (MyBean) app.getBean("bean1");
bean.setMessage("Hello, How are you?");
MyBeanKeeper keeper = new MyBeanKeeper(bean, "taro@yamada", "hanako@flower");
BeanWrapper wrapper = new BeanWrapperImpl(keeper);
// 取得所有的key value 的数组
PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
// 遍历
for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName();
Object value = wrapper.getPropertyValue(name);
System.out.println(name + ":" + value);
}
}
}
打印信息
bean:MyBean [message=Hello, How are you?, date=Sun Apr 21 15:04:35 JST 2019]
class:class jp.tuyano.spring.sample2.MyBeanKeeper // 每个bean都有这个信息
from:taro@yamada
to:hanako@flower
如果使用的type类型不是基本类型。则需要指定class
初始化:
没有构造函数的时候,设置属性(调用set get)
有构造函数的时候,设置构造函数的参数
需要准备
CustomDateEditor 解析properties中数据,定义format
CustomEditorConfigurer 设置解析器为CustomDateEditor
继承PropertyEditorSupport
package jp.tuyano.spring.sample2;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
// 可以同时实现2种editor
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
registry.registerCustomEditor(MyBean.class, new MyBeanTypeEditor());
}
}
获取properties
创建对象
配置对象
应用
-MyBeanKeeper.java
package jp.tuyano.spring.sample2;
public class MyBeanKeeper {
private MyBean bean;
private String from;
private String to;
public MyBeanKeeper(MyBean mybean, String from, String to) {
super();
this.bean = mybean;
this.from = from;
this.to = to;
}
public MyBean getBean() {
return bean;
}
public void setBean(MyBean bean) {
this.bean = bean;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
@Override
public String toString() {
return "MyBeanKeeper [bean=" + bean + ", from=" + from + ", to=" + to + "]";
}
}
-MyBean.java
package jp.tuyano.spring.sample2;
import java.util.Calendar;
import java.util.Date;
public class MyBean {
private String message;
private Date date = Calendar.getInstance().getTime();
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "MyBean [message=" + message + ", date=" + date + "]";
}
}