SPRING FRAMEWORK

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

在工作中,很多專案基於Spring框架開發。在Spring內部,有一些模式或框架的封裝比較有用,偶爾用之,可使程式碼更加簡潔、健壯。

FactoryBean

使用FactoryBean註冊bean物件到容器,通過繼承AbstractFactoryBean,可以方便地控制singleton或prototype型別。

http://www.baeldung.com/spring-factorybean

ResourceResolver

Spring在載入類路徑資源時除了提供字首“classpath:”的來支援載入一個Resource,還提供一個字首“classpath*:”來支援載入所有匹配的類路徑Resource。

Spring提供ResourcePatternResolver介面來載入多個Resource,該介面繼承了ResourceLoader.

Ant路徑萬用字元支援“?”、“*”、“**”,注意萬用字元匹配不包括目錄分隔符“/”

“? ”:匹配一個字元 ,如“config?.xml”將匹配“config1.xml”;
“* ”:匹配零個或多個字串 ,如“cn/*/config.xml”將匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”將匹配“cn/config-dao.xml”;
“** ”:匹配路徑中的零個或多個目錄 ,如“cn/**/config.xml”將匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml”將匹配“cn/javass/config-dao.xml”,即把“**”當做兩個“*”處理。

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();      
//然後進行遍歷模式匹配
Resource[] resources=resolver.getResources("classpath*:META-INF/*.xml");

 

public class CustomResourceLoader implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void showResourceData() throws IOException 
{
//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
reader.close();
}
}

http://howtodoinjava.com/spring/spring-core/how-to-load-external-resources-files-into-spring-context/

此外,spring支援通過ClassPathResource/FileSystemResource/ByteArrayResource等Resource實現來直接載入資源。

ApplicationEvent

釋出並監聽事件,繼承ApplicationEvent

http://howtodoinjava.com/spring/spring-core/how-to-publish-and-listen-application-events-in-spring/

HttpServletRequestWrapper

HttpServletRequestWrapper包裝HttpServletRequest,對引數進行處理,如字元過濾,sql攻擊過濾等。

MetadataReaderFactory

用來解析特定條件的資原始檔元資訊MetadataReader

protected static List<Class<?>> getSubTypesOf(Class<?> baseClass, String packageName) throws InitializationError {
List<Class<?>> candidates = new ArrayList<Class<?>>();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
String resourcePath = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName));
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX   resourcePath   "/**/*.class";
try {
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
for (Resource resource : resources) {
if (resource.isReadable()) {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
Class<?> candidate = Class.forName(metadataReader.getClassMetadata().getClassName());
if (baseClass.isAssignableFrom(candidate)) {
candidates.add(candidate);
}
} catch(Exception e){
// skip this resource
}
}
}
} catch (IOException e) {
throw new InitializationError(e);
}
return candidates;
}

可以配合TypeFilter使用,過濾包含指定註解或類名的類。

RegexPatternTypeFilter: 過濾類名

AnnotationTypeFilter: 過濾註解

@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
for (TypeFilter filter: this.filters) {
if (!filter.match(metadataReader, metadataReaderFactory)) {
return false;
}
}
return true;
}

 

相關文章

程式語言 最新文章