ActivityContext, the heart of an Aspectran application, is created through a systematic process by the ActivityContextBuilder. This build process is largely divided into four stages: Configuration, Parsing, Creation, and Initialization, and the entire process is led by the com.aspectran.core.context.builder.HybridActivityContextBuilder class.
1. Builder Creation & Configuration
Everything starts with creating an instance of ActivityContextBuilder. After the builder is created, the developer provides the core information needed to configure the context through various setter methods.
setBasePath(String basePath): Specifies the root directory of the application. It serves as the base point for relative paths.setContextRules(String[] contextRules): Passes the paths of Aspectran’s core configuration files (in XML or APON format) as an array. This allows for managing multiple configuration files like modules.setBasePackages(String... basePackages): Specifies the packages to be used for annotation-based component scanning. The builder finds annotations like@Component,@Bean, and@Aspectunder these packages and automatically generates rules.setActiveProfiles(String... activeProfiles): Specifies the application’s execution environment profiles (e.g.,dev,prod). Only configurations that match the specified profiles are activated.setResourceLocations(String... resourceLocations): Allows for dynamically adding resource paths to be included in the classpath.setAponWriter(AponWriter aponWriter): Used for debugging by outputting the parsed rules in APON format.
2. Build & Parsing
Once the configuration is complete, the build() method is called to start the actual build process. The substantive work of this stage is handled by com.aspectran.core.context.rule.parser.HybridActivityContextRuleParser.
Parser Initialization:
HybridActivityContextRuleParserinternally creates a core helper object calledRuleParsingContext.RuleParsingContexttemporarily stores all parsed rules (*Ruleobjects), sets up relationships between rules, and is ultimately responsible for configuring the registries.- Rule Parsing: The parser performs its work based on the configured
contextRulesandbasePackages.- File-based Parsing: It iterates through the configuration files specified in
contextRulesand parses their content usingXmlActivityContextParserorAponActivityContextParserdepending on the file extension. The parsed result is converted into a tree of rule objects defined in thecom.aspectran.core.context.rulepackage, such asAspectRule,BeanRule, andTransletRule. - Annotation-based Parsing: It scans the package paths specified in
basePackagesto find classes annotated with@Component,@Bean,@Aspect, etc.AnnotatedClassParseranalyzes these classes and creates equivalentBeanRuleandAspectRuleobjects.
- File-based Parsing: It iterates through the configuration files specified in
- Rule Registration: All created
*Ruleobjects are added to the appropriate internal collections byRuleParsingContext. At this point, the rules are only loaded into memory; no actual instances have been created yet.
3. Context Creation
This is the stage where the actual ActivityContext instance is created based on the parsed rules.
DefaultActivityContextCreation: ThecreateActivityContext()method creates an instance ofcom.aspectran.core.context.DefaultActivityContext.Registry Injection:
RuleParsingContextcategorizes all the temporarily stored rules and creates final registry objects likeAspectRuleRegistry,BeanRuleRegistry, andTransletRuleRegistry, which are then injected intoDefaultActivityContext.Validation: The validity of the rules is checked to ensure that the context can operate correctly.
BeanReferenceInspector: It tracks all relationships that reference other beans from@Autowiredor configuration files and checks for errors such as referencing a non-existent bean or circular references.AspectRuleValidator: It verifies whether the AOP rules are valid, for example, whether the bean and method specified by the advice actually exist.
4. Initialization & Completion
This is the final stage of activating the created context so that it can be used by the application.
ActivityContext.initialize()Call: When this method is called, the initialization of the context begins.- Singleton Bean Instantiation: All singleton-scoped beans registered in the
BeanRegistryare actually instantiated. During this process, constructor injection occurs, and dependency injection for fields and methods annotated with@Autowiredis completed. - Initialization Callback Execution: The initialization methods of beans that are annotated with
@Initializeor implement theInitializableBeaninterface are called.
- Singleton Bean Instantiation: All singleton-scoped beans registered in the
Auto-Reloading Activation (Optional): If the
autoReloadsetting is enabled,ContextReloadingTimeris started. This timer monitors the configuration files for changes at a specified interval (scanInterval), and if a change is detected, it callsserviceLifeCycle.restart()to perform the build process described above from the beginning. This allows for dynamically reflecting configuration changes without restarting the application.- Context Return: After all processes are successfully completed, the fully activated
ActivityContextobject is provided as the final return value of thebuild()method.