Master Spring’s Core Features: Resource Management, Environment, Type Conversion and More

This article provides a comprehensive guide to Spring’s essential infrastructure, covering Java and Spring resource management, environment property handling, type conversion APIs, data binding mechanisms, generic type inspection with ResolvableType, internationalization support, the BeanFactory and ApplicationContext architecture, and the built‑in event system, all illustrated with code examples and diagrams.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Master Spring’s Core Features: Resource Management, Environment, Type Conversion and More

Resource Management

Spring’s resource management builds on Java’s standard resource handling using java.net.URL and URLConnection. A simple demo shows how to read the Baidu homepage via URL, and how to load local files with the file:// protocol.

public class JavaResourceDemo {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.baidu.com");
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        String content = IoUtil.read(new InputStreamReader(in));
        System.out.println(content);
    }
}

Spring abstracts resources with Resource and WritableResource interfaces. Common implementations include FileSystemResource, UrlResource, ClassPathResource, and ByteArrayResource. Loading a resource can be done via ResourceLoader or ResourcePatternResolver.

Resource resource = new UrlResource("http://www.baidu.com");
InputStream is = resource.getInputStream();

Environment

The Environment abstraction provides access to configuration properties, type conversion, and placeholder resolution. It extends PropertyResolver and can be used to read values from application.yml or other property sources.

@SpringBootApplication
public class EnvironmentDemo {
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(EnvironmentDemo.class, args);
        ConfigurableEnvironment env = ctx.getEnvironment();
        String name = env.getProperty("name");
        System.out.println("name = " + name);
    }
}

Type Conversion

Spring supports several conversion APIs: PropertyEditor, Converter, GenericConverter, ConversionService, and TypeConverter. ConversionService is the façade that delegates to registered converters, while TypeConverter first tries PropertyEditor then ConversionService.

public class TypeConverterDemo {
    public static void main(String[] args) {
        SimpleTypeConverter tc = new SimpleTypeConverter();
        tc.setConversionService(DefaultConversionService.getSharedInstance());
        Boolean b = tc.convertIfNecessary("true", Boolean.class);
        System.out.println("b = " + b);
    }
}

Data Binding

Data binding binds configuration values to bean properties using PropertyValues, BeanWrapper, and DataBinder. BeanWrapper implements TypeConverter and performs conversion before setting properties.

public class BeanWrapperDemo {
    public static void main(String[] args) {
        User user = new User();
        BeanWrapper bw = new BeanWrapperImpl(user);
        bw.setPropertyValue(new PropertyValue("username", "三友的java日记"));
        System.out.println("username = " + user.getUsername());
    }
}

Generic Type Handling

Spring’s ResolvableType utility can inspect generic parameters at runtime. Example shows how to retrieve Integer and String type arguments from a custom MyMap class that extends HashMap<Integer, List<String>>.

ResolvableType myMap = ResolvableType.forClass(MyMap.class);
ResolvableType hashMap = myMap.getSuperType();
Class<?> keyClass = hashMap.getGeneric(0).resolve(); // Integer
Class<?> valueClass = hashMap.getGeneric(1).getGeneric(0).resolve(); // String

Internationalization

Spring’s MessageSource builds on Java’s Locale, ResourceBundle, and MessageFormat. By defining message_zh_CN.properties and message_en.properties, Spring can resolve locale‑specific messages with placeholders.

ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
ms.setDefaultEncoding("UTF-8");
ms.addBasenames("message");
String zh = ms.getMessage("welcome", new Object[]{"张三"}, Locale.SIMPLIFIED_CHINESE);
String en = ms.getMessage("welcome", new Object[]{"Bob"}, Locale.ENGLISH);

BeanFactory

The core IoC container is represented by the BeanFactory hierarchy. DefaultListableBeanFactory implements BeanFactory, BeanDefinitionRegistry, and related sub‑interfaces. Bean definitions are read by BeanDefinitionReader (e.g., XmlBeanDefinitionReader) or scanned with ClassPathBeanDefinitionScanner, stored in a BeanDefinitionRegistry, and instantiated on demand.

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(factory);
reader.register(BeanFactoryDemo.class);
BeanFactoryDemo demo = factory.getBean(BeanFactoryDemo.class);

ApplicationContext

ApplicationContext

extends BeanFactory and adds resource loading, event publishing, and environment access. The abstract base AbstractApplicationContext delegates to an internal DefaultListableBeanFactory and requires a refresh() call before use.

Spring Events

Spring implements the observer pattern with ApplicationEvent, ApplicationListener, and ApplicationEventPublisher. Custom events can be published via ApplicationContext.publishEvent(), and listeners in parent or child contexts receive the events.

public class FireEvent extends ApplicationEvent {
    public FireEvent(String source) { super(source); }
}
public class Call119Listener implements ApplicationListener<FireEvent> {
    public void onApplicationEvent(FireEvent e) { System.out.println("Call 119"); }
}
public class SavePersonListener implements ApplicationListener<FireEvent> {
    public void onApplicationEvent(FireEvent e) { System.out.println("Save person"); }
}
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Call119Listener.class, SavePersonListener.class);
ctx.refresh();
ctx.publishEvent(new FireEvent("fire"));
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaResource Managementspringdata bindinginternationalizationtype conversionEnvironmentapplicationcontext
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.