これは私の首の痛みになります!!! 3つの質問があります。
1) カスタム POJO クラスをプールするために、プロジェクトで CommonsPool2TargetSource を構成したいと考えています。
私がこれまでに行ったこと:
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 を明示的に定義していません。春の @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' は、すべての BeanPostProcessor によって処理される資格がありません (例:自動プロキシには不適格)|[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 要求が処理されるたびに再インスタンス化されますか?