|
From: Jean-Philippe G. <ga...@ya...> - 2005-02-21 02:29:39
|
(This is an opportunity for me to share my thoughts about the bean factory.)
I think the current XML syntax for the BeanFactory is very verbose. Two years
ago, I developed my own BeanFactory based on Rod Johnson's initial
XmlBeanFactory (before Spring was born). I'm still using this customized
BeanFactory today. First thing I did was to remove extra XML elements that
were always implied by the context (like '<bean>' and '<property>').
So instead of...
<bean name="myBean" class="myclass">
<property name="myProperty>value</property>
</bean>
...I only need...
<myBean class="myclass">
<myProperty>value</myProperty>
</myBean>
This is more readable and much more terse. The price to pay for that: I cannot
have XML grammar validation (such as DTD or Schema) but I see little value in
that (at least, for my own needs). My XML editor can still find errors if my
document is not well-formed.
The second step I took was to add a special syntax for bean references (using
the @ symbol:
So...
<bean name="beanA" class="ClassA"/>
<bean name="beanB" class="ClassB">
<property name="otherBean" beanRef="true">beanA</property>
</bean>
...can be expressed simply as...
<beanA class="ClassA"/>
<beanB class="ClassB">
<otherBean>@beanA</otherBean>
</beanB>
I've added other lightweight syntaxes for lists and maps.
The third major change I did was to allow bean definitions to be overridden by
values specified in other files. In Spring, you would use the
PropertyResourceConfigurer instead to resolve similar (but not all) problems.
For instance, I have the following 3 files:
========base-config.xml (base configuration for the prod):
<beans>
<logger class="com.xyz.Logger">
<file>/var/log/app/log.txt</file>
<level>ERROR</level>
</logger>
<messageMailer class="com.xyz.Mailer">
<smtpHostname>mail.xyz.com</smtpHostName>
<address>ab...@xy...</address>
</messageMailer>
</beans>
========development-environment.xml (overidding for all the developers):
<beans>
<logger>
<level>INFO</level>
</logger>
</beans>
========user.xml (developer specific overriding file):
<beans>
<mailer>
<address>us...@xy...</address>
</mailer>
</beans>
I create the BeanFactory using the 3 files:
//not very accurate but you can get the idea...
BeanDefinitionContainer container = new BeanDefinitionContainer();
container.add(new XmlBeanDefinitionSource("base-config.xml"));
container.add(new XmlBeanDefinitionSource("development-environment.xml"));
container.add(new XmlBeanDefinitionSource("user.xml"));
BeanFactory factory = new BeanFactory(container);
The bean factory (the definition container, in fact) will read the 3 files and
merge property definitions specified in each files. This is very handy when
you need to have multiple levels of configuration. This lets you assemble
configurations in a very flexible manner. I think this is interesting because
an "override file" shares the same syntax (and power) as a regular bean
definition file.
The key to a "custom bean instantiation language" is to keep bean definitions
separated from the BeanFactory. Currently, in Spring,
DefaultListableBeanFactory plays both roles (it's a BeanFactory and a
BeanDefinitionRegistry). Although both interfaces exists in the framework,
they are not used separately.
Having a definition container (or registry) allows the modification of the bean
definitions before they are used by the bean factory. For example, I have a
CommandLineBeanDefinitionOverride class that modifies bean definition based on
command-line arguments.
public class MyMain
{
public static void main(String[] args)
{
BeanDefinitionContainer container = new BeanDefinitionContainer();
container.add(new XmlBeanDefinitionSource("base-config.xml"));
container.add(new XmlBeanDefinitionSource("development-environment.xml"));
container.add(new XmlBeanDefinitionSource("user.xml"));
args = CommandLineBeanDefinitionOverride.processArguments(args, container);
BeanFactory factory = new BeanFactory(container);
...
}
}
I can invoke my java app and change bean values:
$java MyTest --logger +level=DEBUG
I use "--" to specify bean name and "+" to specify property name. This opens
up bean definitions customization beyond XML configuration file.
I'm not totally familiar with the Spring framework and maybe it's already
possible to do everything I've explained above. I just wanted to talk about my
experience about the subject.
Jean-Philippe
--- Paul Galbraith <pa...@pa...> wrote:
> Paul Galbraith wrote:
>
> > jbetancourt wrote:
> >
> >> There is support for scripting. I think it is part of the sandbox.
> >> Thus,
> >> you can use Beanshell, Groovy, etc.
> >>
> >> But, not sure if this is meant as a 'custom language' for bean
> >> instantiation.
> >>
> >>
> >>
> > That might do the trick...can I use a groovy script to wire up my bean
> > factories?
> >
> After thinking about this, I doubt scripting is what I'm interested
> in...I imagine that any creation of a BeanFactory through a scripting
> engine is still ultimately using the documented core Spring API?
>
> I'm more interested in something declarative, such as the XML
> declarations that are documented in the online reference...my motivation
> for raising the idea is simply that XML isn't a great language to use
> for interfacing with people. I was thinking of something more
> java-like, such as a bean definition like:
>
> singleton my.package.MyClass my.beannamespace.myBean
> (my.beannamespace.myBean2) {
> property String stringProperty "string-property";
> property my.pacakge.myClass3 my.beannamespace.myBean3;
> }
>
> Obviously I haven't put a whole lot of thought into it...just wondering
> if the same thing's already been discussed or considered?
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
=====
---------------------------------------
Jean-Philippe Gariépy (ga...@ya...)
"Quand l'appétit va, tout va."
-Obélix
__________________________________
Do you Yahoo!?
Meet the all-new My Yahoo! - Try it today!
http://my.yahoo.com
|