如何租用网站服务器,教人做衣服得网站有哪些,网站模板插件,wordpress 不显示全文一、文件解析器的例子 在开发中我们可能会解析CSV#xff0c;JSON等文件类型#xff0c;显然要用策略模式 我们先定义一个Parser接口 
public interface Parser {void parse(String input);
} 
Parser接口的实现类有JSONParser#xff0c;CSVParser 
Component(CSVJSON等文件类型显然要用策略模式 我们先定义一个Parser接口 
public interface Parser {void parse(String input);
} 
Parser接口的实现类有JSONParserCSVParser 
Component(CSV)
public class CSVParser implements Parser{Overridepublic void parse(String input) {System.out.println(csv);}
}Component(JSON)
public class JSONParser implements Parser{Overridepublic void parse(String input) {System.out.println(json);}
}然后我们定义一个工厂来获取Parser对象 
public interface ParserFactory {Parser getParser(ContentType type);} 
在该工厂方法的参数是一个枚举 
public enum ContentType {JSON,XML,TEXT,HTML,IMAGE,OTHER;
} 写一个配置类 
Configuration
public class ParserConfig {Beanpublic FactoryBean serviceLocatorFactoryBean(){ServiceLocatorFactoryBean factoryBean  new ServiceLocatorFactoryBean();factoryBean.setServiceLocatorInterface(ParserFactory.class);return factoryBean;}} 
测试 Autowiredprivate ParserFactory parserFactory;Testvoid contextLoads() {Parser parser  parserFactory.getParser(ContentType.JSON);parser.parse({name:John});} 二、原理 ServiceLocatorFactoryBean实现了接口FactoryBean和InitializingBean实现InitializingBean会在初始化ServiceLocatorFactoryBean调用afterPropertiesSet方法具体实现如下代码 Overridepublic void afterPropertiesSet() {if (this.serviceLocatorInterface  null) {throw new IllegalArgumentException(Property serviceLocatorInterface is required);}// Create service locator proxy.this.proxy  Proxy.newProxyInstance(this.serviceLocatorInterface.getClassLoader(),new Class?[] {this.serviceLocatorInterface},new ServiceLocatorInvocationHandler());} 在afterPropertiesSet主要给ServiceLocatorFactoryBean的成员变量proxy赋值生成代理对象其中接口就是我们前面的ParserFactory。         我们知道实现FactoryBean可以帮助我们创建bean其中getObject可以获得bean实现内容如下  OverrideNullablepublic Object getObject() {return this.proxy;} getObject返回的ParserFactory的代理对象生成动态代理方式是采用JDK代理主要关注invoke方法ServiceLocatorFactoryBean的内部类实现类接口InvocationHandler Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {....return invokeServiceLocatorMethod(method, args);	}private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception {Class? serviceLocatorMethodReturnType  getServiceLocatorMethodReturnType(method);try {String beanName  tryGetBeanName(args);Assert.state(beanFactory ! null, No BeanFactory available);if (StringUtils.hasLength(beanName)) {// Service locator for a specific bean namereturn beanFactory.getBean(beanName, serviceLocatorMethodReturnType);}else {// Service locator for a bean typereturn beanFactory.getBean(serviceLocatorMethodReturnType);}}catch (BeansException ex) {if (serviceLocatorExceptionConstructor ! null) {throw createServiceLocatorException(serviceLocatorExceptionConstructor, ex);}throw ex;}} invoke主要先根据方法参数获得beanName通过beanFactory获得bean实例因此要特别注意传入工厂方法的参数要与被spring管理的bean的名字一致才行