返回顶部

收藏

Spring 无需进行类型转换的方法

更多

AppContainer.java

public interface AppContainer {

    <T> T getIns(Class<T> clazz);

    Object getInstanceSkipCast(Class clazz);

}

DefaultAppContainer.java

import static org.apache.commons.lang.StringUtils.uncapitalize;

public class DefaultAppContainer implements AppContainer {

    private ApplicationContext factory;

    public DefaultAppContainer() {
        factory = new ClassPathXmlApplicationContext("spring/application.xml");
    }

    public <T> T getIns(Class<T> clazz) {// this should work for 90% of the casses
        return (T) factory.getBean(uncapitalize(clazz.getSimpleName()));
    }

    public Object getInstanceSkipCast(Class clazz) {
        return factory.getBean(uncapitalize(clazz.getSimpleName()));
    }

}

标签:ApplicationContext,Spring

收藏

0人收藏

支持

0

反对

0

发表评论