Yan container can be used within a web application.
Configuration
In order for the Nuts xml configuration file to be picked up by the servlet container, one has to edit the web.xml file to include the following entry:
- yanConfigFile
<context-param> <param-name>yanConfigFile</param-name> <param-value>/WEB-INF/classes/yan.xml</param-value> </context-param>
This parameter points to the Nuts configuration file.
- YanLoaderListener
<listener> <listener-class> jfun.yan.web.YanLoaderListener </listener-class> </listener>This listener class is responsible for reading the yan configuration file.
If your servlet container is too old to support listeners, you can alternatively use the jfun.yan.web.YanLoaderServlet for the same purpose. If you are familiar with Spring MVC, this is surprisingly similar to the Spring ContextLoaderListener and ContextLoaderServlet. - yanSpringIntegration
<context-param> <param-name>yanSpringIntegration</param-name> <param-value>true</param-value> </context-param>This parameter is used to enable Spring framework integration for Yan. When Spring is integrated, one can use Spring FactoryBean classes such as the wonderful "TransactionProxyFactoryBean". Of course, the Spring jar files need to be placed in the classpath.
That's all for the configuration.
Use Yan container in servlet.
Then, within any servlet, you can retrieve the jfun.yan.Container object from the ServletContext as such:
Container yan = (Container)servletContext.getAttribute("jfun.yan.Container");
This ServletContext instance is also automatically registered in the container if there's no component of type ServletContext already registered in the container. The key is typically "servletContext". If there's already a component registered under this key, the ServletContext class is used as the key.
Another feature of the web integration is that, components with a property setter "setServletContext(ServletContext context)" will be auto-magically injected with the ServletContext object. There's no proprietary interface like "ServletContextAware" to implement. Thus the component has no dependency on the infrastructure. (Well, it has to depend on ServletContext)
This property setter name can be specified explicitly using the "servlet-context-property" attribute in the configuration file such that:
<bean id="mycomponent" ... servlet-context-property="servletContextInstance"/>
Thus, the "mycomponent" class can define a "setServletContextInstance(ServletContext context)" instead.
Note, if this "setServletContext" property is involved in wiring either explicitly (by manual wiring or "property-names") or implicitly (by using "autowire"), the wiring overrides the auto-magic injection.
Extending container
It is sometimes desirable to register custom services, custom autowire modes or custom tags at startup, so that one can use these tags and autowire modes in the nuts configuration file.
This can be done by subclassing YanLoaderListener and YanLoader.
Since YanLoaderListener's only responsibility is to delegate to YanLoader, the subclass of YanLoaderListener just needs to create a different YanLoader object.
The subclass of YanLoader class does the actual customization. Please refer to the API documentation for detailed information.
