这成了我脖子上的痛!!!我有三个疑问。
1)我想在我的项目中配置 CommonsPool2TargetSource 以汇集我的自定义 POJO 类。
到目前为止我做了什么:
MySpringBeanConfig 类:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.redirect.controller","com.redirect.business","com.redirect.dao.impl","com.redirect.model"})
@EnableTransactionManagement
@PropertySource("classpath:" + JioTUConstant.SYSTEM_PROPERTY_FILE_NAME + ".properties")
@Import({JioTUCouchbaseConfig.class,JioTUJmsConfig.class})
public class JioTUBeanConfig extends WebMvcConfigurerAdapter {
private static final Logger LOGGER = Logger.getLogger(JioTUConfig.class);
@Bean
public CommonsPool2TargetSource poolTargetSource() {
CommonsPool2TargetSource commonsPool2TargetSource = new CommonsPool2TargetSource();
commonsPool2TargetSource.setTargetBeanName("jioTUURL");
commonsPool2TargetSource.setMinIdle(5);
commonsPool2TargetSource.setMaxIdle(5);
commonsPool2TargetSource.setMaxSize(10);
return commonsPool2TargetSource;
}
@Bean
public ProxyFactoryBean proxyFactoryBean() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTargetSource(poolTargetSource());
return proxyFactoryBean;
}
@Bean
public MethodInvokingFactoryBean poolConfigAdvisor() {
MethodInvokingFactoryBean poolConfigAdvisor = new MethodInvokingFactoryBean();
poolConfigAdvisor.setTargetObject(poolTargetSource());
poolConfigAdvisor.setTargetMethod("getPoolingConfigMixin");
return poolConfigAdvisor;
}
}
我在“com.redirect.model”包中的 POJO 类:
@Repository
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Document
public class JioTUURL{
@Id
private String keyword;
@Field
private String url;
@Field
private String title;
@Field
private String timestamp;
@Field
private String ip;
@Field
private Integer clicks;
@Field
private String user;
//Getter/Setter
}
我得到的例外:
org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [com.redirect.model.JioTUURL] 类型的合格 bean:预期的单个匹配 bean 但找到了 2:jioTUURL,proxyFactoryBean
仅供参考,我没有为 JioTUURL 明确定义任何 bean。由spring的@ComponentScan决定
如果我在JioTUConfig.java类的proxyFactoryBean()方法中注释以下行
@Bean
public ProxyFactoryBean proxyFactoryBean() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
// proxyFactoryBean.setTargetSource(poolTargetSource());
return proxyFactoryBean;
}
然后它运行良好,日志信息如下
09-08-2016 16:28:13.866|INFO |localhost-startStop-1|[class org.springframework.aop.target.CommonsPool2TargetSource] 类型的 Bean 'poolTargetSource' 不符合所有 BeanPostProcessors 处理的条件(例如:不符合自动代理条件)|[PostProcessorRegistrationDelegate.java:328]
2)如何从池中获取对象?
@Controller
public class JioTUController {
private static final Logger LOGGER = Logger.getLogger(JioTUController.class);
@Autowired
private JioTUCommonBusiness jioTUCommonBusiness;
@Autowired
private ObjectFactory<JioTUURL> jioTUURLObjectFactory;//Need to replace I guess
public JioTUController() {
LOGGER.info("Loading JioTUController complete");
}
@RequestMapping(method = RequestMethod.GET, value="/*")
public ModelAndView postDataAsJSON(HttpServletRequest request,HttpServletResponse response, ModelAndView modelAndView) {
//Should be replace with code that fetch object for me from the pool
JioTUURL jioTUURL = jioTUURLObjectFactory.getObject();
//App Business
}
}
3) 池中的那些对象是回收的还是在每个 HTTP 请求服务后重新实例化?