简体   繁体   English

AEM / CQ5上的Java批注(构造后和预销毁)

[英]Java Annotations (Postconstruct and PreDestroy) on AEM/CQ5

Is it possible to use a method with the annotations @PostConstruct or @PreDestroy on the Service class of an OSGI Bundle in AEM 6, because I am planning to create a separate bundle for the database connectivity and the way I will use these 2 annotations is to open the database connection on the @PostConstruct and to close the connection and to perform to commit or rollback on the @PreDestroy . 是否可以在AEM 6中的OSGI Bundle的Service类上使用带有注释@PostConstruct@PreDestroy的方法,因为我计划为数据库连接创建一个单独的捆绑包,而我将使用这两个注释的方式是打开@PostConstruct上的数据库连接并关闭该连接,并执行@PreDestroy上的提交或回滚。 If it is not possible are there any other way or work around on the approach that I am planning? 如果不可能,是否有其他方法或无法解决我计划的方法?

OSGi uses a different set of annotations for handling the activation and deactivation of a bundle. OSGi使用一组不同的注释来处理捆绑软件的激活和停用。

These are the @Activate and @Deactivate annotations from org.apache.felix.scr.annotations 这些是org.apache.felix.scr.annotations中的@Activate@Deactivate注释

For example: 例如:

@Component
public class Foo implements Bar {

   @Activate
   void activate(ComponentContext ctx) {
      System.out.println("Activating bundle: " + ctx.getUsingBundle());
   }

   @Deactivate
   void deactivate(ComponentContext ctx) {
      System.out.println("Deactivating bundle: " + ctx.getUsingBundle());
   }
}

For more information, check out the OSGi wiki 有关更多信息,请查看OSGi Wiki

However, I don't think you should keep a database connection open throughout the entire lifecycle of the bundle. 但是,我认为您不应该在捆绑软件的整个生命周期中保持数据库连接打开。 Just open and close it ad-hoc when you really need it. 只需在真正需要时临时打开和关闭它即可。

What you should create when activating your bundle is not a connection but an object capable of building and maintaining a connection pool. 激活捆绑软件时,您应该创建的不是连接而是一个能够构建和维护连接池的对象。

Google around for the DataSourceFactory interface Google周围的DataSourceFactory接口

Also, I think AEM already provides a set of JDBC connectors OOTB. 另外,我认为AEM已经提供了一组JDBC连接器OOTB。 Be sure to check the Adobe documentation on the subject before diving into developing a custom solution. 在着手开发自定义解决方案之前,请务必查看有关此主题Adobe文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM