public class JavaResourceDemo {
public static void main(String[] args) throws IOException {
//构建URL 指定资源的协议为http协议
URL url = new URL("http://www.baidu.com");
//打开资源连接
URLConnection urlConnection = url.openConnection();
//获取资源输入流
InputStream inputStream = urlConnection.getInputStream();
//通过hutool工具类读取流中数据
String content = IoUtil.read(new InputStreamReader(inputStream));
System.out.println(content);
}
}
//构建资源
Resource resource = new UrlResource("http://www.baidu.com");
//获取资源输入流
InputStream inputStream = resource.getInputStream();
public interface PropertyResolver {
boolean containsProperty(String key);
String getProperty(String key);
<T> T getProperty(String key, Class<T> targetType);
<T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
String resolvePlaceholders(String text);
}
@SpringBootApplication
public class EnvironmentDemo {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(EnvironmentDemo.class, args);
//从ApplicationContext中获取到ConfigurableEnvironment
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//获取name属性对应的值
String name = environment.getProperty("name");
System.out.println("name = " + name);
}
}
name = 三友的java日记
public class PropertySourceDemo {
public static void main(String[] args) {
Map<String, Object> source = new HashMap<>();
source.put("name", "三友的java日记");
PropertySource<Map<String, Object>> propertySource = new MapPropertySource("myPropertySource", source);
Object name = propertySource.getProperty("name");
System.out.println("name = " + name);
}
}
@Value("http://www.baidu.com")
private Resource resource;
public class TypeConverterDemo {
public static void main(String[] args) {
SimpleTypeConverter typeConverter = new SimpleTypeConverter();
//设置ConversionService
typeConverter.setConversionService(DefaultConversionService.getSharedInstance());
//将字符串"true"转换成Boolean类型的true
Boolean b = typeConverter.convertIfNecessary("true", Boolean.class);
System.out.println("b = " + b);
}
}
<bean class="com.sanyou.spring.core.basic.User">
<property name="username" value="三友的java日记"/>
</bean>
@Data
public class User {
private String username;
}
public class BeanWrapperDemo {
public static void main(String[] args) {
//创建user对象
User user = new User();
//创建BeanWrapper对象,把需要进行属性绑定的user对象放进去
BeanWrapper beanWrapper = new BeanWrapperImpl(user);
//进行数据绑定,将三友的java日记这个属性值赋值到username这个属性上
beanWrapper.setPropertyValue(new PropertyValue("username", "三友的java日记"));
System.out.println("username = " + user.getUsername());
}
}
public class MyMap extends HashMap<Integer, List<String>> {
}
public class ResourceBundleDemo {
public static void main(String[] args) {
//获取ResourceBundle,第一个参数baseName就是我们的文件名称,第二个参数就是地区
ResourceBundle chineseResourceBundle = ResourceBundle.getBundle("message", Locale.SIMPLIFIED_CHINESE);
//根据name键取值
String chineseName = chineseResourceBundle.getString("name");
System.out.println("chineseName = " + chineseName);
ResourceBundle englishResourceBundle = ResourceBundle.getBundle("message", Locale.ENGLISH);
String englishName = englishResourceBundle.getString("name");
System.out.println("englishName = " + englishName);
}
}
public class MessageFormatDemo {
public static void main(String[] args) {
String message = MessageFormat.format("你好:{0}", "张三");
System.out.println("message = " + message);
}
}
public class MessageSourceDemo {
public static void main(String[] args) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
//Spring已经扩展了ResourceBundle的Control,支持资源文件的不同编码方式,但是需要设置一下
messageSource.setDefaultEncoding("UTF-8");
//添加 baseName,就是前面提到的文件中的basename
messageSource.addBasenames("message");
//中文,传个中文名字
String chineseWelcome = messageSource.getMessage("welcome", new Object[]{"张三"}, Locale.SIMPLIFIED_CHINESE);
System.out.println("chineseWelcome = " + chineseWelcome);
//英文,英语国家肯定是英文名
String englishWelcome = messageSource.getMessage("welcome", new Object[]{"Bob"}, Locale.ENGLISH);
System.out.println("englishWelcome = " + englishWelcome);
}
}
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
//设置Bean className
void setBeanClassName(@Nullable String beanClassName);
//获取Bean className
@Nullable
String getBeanClassName();
//设置是否是懒加载
void setLazyInit(boolean lazyInit);
//判断是否是懒加载
boolean isLazyInit();
//判断是否是单例
boolean isSingleton();
}
public class BeanFactoryDemo {
public static void main(String[] args) {
//创建一个BeanFactory
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//创建一个BeanDefinitionReader,构造参数是一个BeanDefinitionRegistry
//因为DefaultListableBeanFactory实现了BeanDefinitionRegistry,所以直接把beanFactory当做构造参数传过去
AnnotatedBeanDefinitionReader beanDefinitionReader = new AnnotatedBeanDefinitionReader(beanFactory);
//读取当前类 BeanFactoryDemo 为一个Bean,让Spring帮我们生成这个Bean
beanDefinitionReader.register(BeanFactoryDemo.class);
//从容器中获取注册的BeanFactoryDemo的Bean
BeanFactoryDemo beanFactoryDemo = beanFactory.getBean(BeanFactoryDemo.class);
System.out.println("beanFactoryDemo = " + beanFactoryDemo);
}
}
public class MyApplicationContext implements ApplicationContext {
private final ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
@Override
public Resource[] getResources(String locationPattern) throws IOException {
return resourcePatternResolver.getResources(locationPattern);
}
}
// 火灾事件
public class FireEvent extends ApplicationEvent {
public FireEvent(String source) {
super(source);
}
}
public class Call119FireEventListener implements ApplicationListener<FireEvent> {
@Override
public void onApplicationEvent(FireEvent event) {
System.out.println("打119");
}
}
public class SavePersonFireEventListener implements ApplicationListener<FireEvent> {
@Override
public void onApplicationEvent(FireEvent event) {
System.out.println("救人");
}
}
public class Application {
public static void main(String[] args) {
//创建一个Spring容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//将 事件监听器 注册到容器中
applicationContext.register(Call119FireEventListener.class);
applicationContext.register(SavePersonFireEventListener.class);
applicationContext.refresh();
// 发布着火的事件,触发监听
applicationContext.publishEvent(new FireEvent("着火了"));
}
}
public class EventPropagateApplication {
public static void main(String[] args) {
// 创建一个父容器
AnnotationConfigApplicationContext parentApplicationContext = new AnnotationConfigApplicationContext();
//将 打119监听器 注册到父容器中
parentApplicationContext.register(Call119FireEventListener.class);
parentApplicationContext.refresh();
// 创建一个子容器
AnnotationConfigApplicationContext childApplicationContext = new AnnotationConfigApplicationContext();
//将 救人监听器 注册到子容器中
childApplicationContext.register(SavePersonFireEventListener.class);
childApplicationContext.refresh();
// 设置一下父容器
childApplicationContext.setParent(parentApplicationContext);
// 通过子容器发布着火的事件,触发监听
childApplicationContext.publishEvent(new FireEvent("着火了"));
}
}