Spring: creating a bean of type java.util.Properties

I don’t know if there’s a simple way to crate a Spring bean of type java.util.Properties . This is what I needed to do.

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" depends-on="hibernateProperties.load">
  <property name="dataSource"><ref local="dataSource"/></property>
  <property name="configLocation"><value>classpath:net/lmb/resources/hibernate.cfg.xml</value></property>
  <property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>
  <property name="hibernateProperties"><ref local="hibernateProperties"/></property>
</bean>

<bean id="hibernateProperties" class="java.util.Properties"/>
<bean id="hibernateProperties.load" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetObject"><ref local="hibernateProperties"/></property>
  <property name="targetMethod"><value>putAll</value></property>
  <property name="arguments">
    <props>
      <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
      <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
      <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
      <prop key="hibernate.connection.username">${hibernate.connection.username}</prop>
      <prop key="hibernate.connection.password">${hibernate.connection.password}</prop>
    </props>
  </property>
</bean>

Basically you need to create an empty bean of type java.util.Properties, and after you add the properties using Spring <props> syntax calling the method putAll. One of the tricky parts is that you need to explicitly state the depends-on attribute in the LocalSessionFactoryBean so properties get loaded before the hibernate session is created.

Update:

Marc Logemann has posted a much more elegant solution using Spring's PropertiesFactoryBean

<bean id="hibernateProperties"
     class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="properties">
    <props>
      <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_refl}</prop>
      <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
      <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
      <prop key="hibernate.connection.username">${hibernate.connection.username}</prop>
      <prop key="hibernate.connection.password">${hibernate.connection.password}</prop>
    </props>
  </property>
</bean>

Spring JMX support

Spring provides a nice way to expose plain java objects and its
methods and properties as JMX managed objects. Maybe the best way to do
so is using annotations.

@ManagedResource(
        objectName = "domain:name=testBean",
        description = "A test Spring-JMX bean")
public class JmxTestBean {

    private int age;

    private String name;

    @ManagedAttribute(description = "age attribute")
    public void setAge(int age) {
        logger.info("setAge");
        this.age = age;
    }

    @ManagedAttribute(description = "age attribute")
    public int getAge() {
        logger.info("getAge");
        return age;
    }

    @ManagedAttribute(description = "name attribute")
    public void setName(String name) {
        logger.info("setNamee");
        this.name = name;
    }

    @ManagedAttribute(description = "name attribute")
    public String getName() {
        logger.info("getName");
        return name;
    }

    public String dontExposeThisMethod() {
        return "dontExposeThisMethod return";
    }

    @ManagedOperation(description = "method exposed")
    public String exposeThisMethod() {
        return "exposeThisMethod return";
    }

}

In the application context you need

    <bean id="jmxAdapter.parent" class="org.springframework.jmx.export.MBeanExporter" abstract="true">
        <property name="autodetect">
            <value>true</value>
        </property>
        <property name="assembler">
            <ref local="metadataMBeanInfoAssembler"/>
        </property>
        <property name="namingStrategy">
            <ref local="namingStrategy"/>
        </property>
    </bean>

    <!-- Use metadata to filter the exposed operations and attributes -->
    <bean id="metadataMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
        <property name="attributeSource">
            <ref local="annotationJmxAttributeSource"/>
        </property>
    </bean>
    <bean id="annotationJmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
    <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
        <property name="attributeSource">
            <ref local="annotationJmxAttributeSource"/>
        </property>
    </bean>

    <bean id="testBean" class="com.arcmind.springjmx.JmxTestBean">
        <property name="name">
            <value>TEST</value>
        </property>
        <property name="age">
            <value>100</value>
        </property>
    </bean>

For testing you'd need to explicitly set the mbean server

    <bean id="jmxAdapter" parent="jmxAdapter.parent">
        <property name="server">
            <ref local="mbeanServer"/>
        </property>
    </bean>

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>

For deploying it gets autodetected

    <bean id="jmxAdapter" parent="jmxAdapter.parent"/>

Other options for the assembler property are

    <!-- Expose everything through reflection -->
    <bean id="simpleReflectiveMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler"/>

    <!-- Use method names to filter the exposed operations and attributes -->
    <bean id="methodNameBasedMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
        <property name="managedMethods">
            <list>
                <value>exposeThisMethod</value>
                <value>getAge</value>
                <value>setAge</value>
                <value>getName</value>
                <value>setName</value>
            </list>
        </property>
    </bean>

    <!-- Expose only those methods in the interfaces implemented by the bean -->
    <bean id="interfaceBasedMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler"/>

But then you may need to explicitly say what beans you want to
expose instead of autodetecting them in the jmxAdapter.parent bean.

        <property name="beans">
            <map>
                <entry key="bean:name=testBean1">
                    <ref local="testBean"/>
                </entry>
            </map>
        </property>


					

Apache Maven 2.0 Alpha 3 Released

The next alpha release of maven 2 is now ready. I’ve tried it
with

very good impressions, things are going the right way. The main
problem

is the wrong and missing information (POMs) about the jars in the

repository, but it’s getting better, and will improve faster as soon
as

more people use it.

In the other hand I’m using maven 1.1 beta 1 and the only
problem I’ve found was MAVEN-1625

when trying to install a plugin in the repository. Seems that the

memory usage was greatly improved and some odd problems with jelly are

fixed.

The Apache Maven team are proud to announce the third alpha
release of Maven 2.0.

Download it from http://maven.apache.org/maven2/download.html

Maven is a software project management and comprehension tool. Based on
the concept of a project object model (POM), Maven can manage a project’s
build, reporting and documentation from a central piece of
information.

This release includes 83 bug fixes and enhancements since the previous
release on 13 May.

Maven 2.0 is a rewrite of the popular Maven application to achieve a
number of goals, and to provide a stable basis to take it into the future. While
it can be considered quite stable, and future versions are now expected to retain
a high amount of backwards compatibility, this is still a technology preview,
and not yet complete or considered ready for a production
environment.

The main new features in this release are:

   * Improved dependency
management
   * Build profiles for
environment specific settings and dependencies
 
 * Finalised build lifecycle
   *
Proper handling of derived dependency type such as sources, javadocs
and ejb clients
   * Beanshell plugin
support
   * Improved reporting support,
including internationalisation.
   *
Improvements to the Ant tasks
   * Better
plugin management, with the ability to select specific versions for
use
   * Various plugin
improvements

This release is very near to being feature complete, and the next
release will be a feature complete beta-1.

We hope you enjoy using Maven! If you have any questions, please
consult:

   * the web site: http://maven.apache.org/maven2<!–

D(["mb","/
   * the maven-user
mailing list: http://maven.apache.org/mail-lists.html

For
news and information, see:

   *
Maven Blogs: http://www.mavenblogs.com/

---------------------------------------------------------------------
To
unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For
additional commands, e-mail: dev-help@maven.apache.org

",0]

);

D(["mi",0,2,"104ae6762b00b70a",0,"0","Will
Gwaltney","Will","Will.Gwaltney@sas.com","Maven","5:51 am (0 minutes
ago)",["Maven Users List "]

,[]

,[]

,["Maven Users List "]

,”Jun 24, 2005 5:51 AM”,”RE: Apache Maven 2.0 Alpha 3 Released”,””,[]

,0,,,”Fri Jun 24 2005_5:51 AM”,”On 6/24/05, Will Gwaltney
wrote:”,”On 6/24/05, Will Gwaltney
<Will.Gwaltney@sas.com> wrote:”]

);

//–>/
   *
the maven-user mailing list: http://maven.apache.org/mail-lists.html

For news and information, see:

   * Maven Blogs: http://www.mavenblogs.com/

Maven 1.1-beta-1 released

The Apache Maven team is pleased to announce the release of Maven 1.1-beta-1

http://maven.apache.org/start/download.html

Maven is a project management and project comprehension tool. Maven is
based on the concept of a project object model: builds, documentation
creation, site publication, and distribution publication are all
controlled from the project object model. Maven also provides tools to
create source metrics, change logs based directly on source repository,
and source cross-references.

This release focuses on the following objectives:

   * Integration of Maven 2 technologies such as Maven Wagon, Maven SCM
and the new model code
   * Ant 1.6.5 support
   * Upgrade to later releases of dependencies, in particular Jelly
   * Significant improvements in memory usage
   * Improved POM layout
   * Bugfixes

With just a few exceptions [1], Maven 1.1 is backwards compatible with
Maven 1.0.

For a full list of changes, please see JIRA [2].

*IMPORTANT: * You must ensure that Maven 1.1 is first in your path if
you want to have it installed side-by-side with Maven 1.0.2

We hope you enjoy using Maven! If you have any questions, please consult:

   * the FAQ: http://maven.apache.org/faq.html
   * the maven-user mailing list: http://maven.apache.org/mail-lists.html

For news and information, see:

   * Maven Blogs: http://www.mavenblogs.com/
<!–
D(["mb","

- The Apache Maven Team

[1] http://maven.apache.org/reference/backwards-compatibility.html
[2]
http://jira.codehaus.org/secure/ReleaseNote.jspa?version=11371&styleName=Html&projectId=10030&Create=Create

“,0]
);
D(["ce"]);
D(["ms","c76a"]
);

//–>

- The Apache Maven Team

[1] http://maven.apache.org/reference/backwards-compatibility.html
[2]
http://jira.codehaus.org/secure/ReleaseNote.jspa?version=11371&styleName=Html&projectId=10030&Create=Create

Spring framework 1.2 RC1 released

Some more jars that I’m adding to the 7000 already at iBiblio (see
my previous entry ;) ) are the ones from Spring framework 1.2 RC1, just
released by Juergen

Dear Spring community,

It's Spring time :-)

I'm pleased to announce that Spring 1.2 RC1 has just been released.
This release introduces a number of major new features:

* finer-grained distribution jar files, alongside the full spring.jar
* AOP Alliance interfaces are now contained in spring-aop.jar and spring.jar
* XML bean definition improvements ("ref" and "value" shortcut attributes etc)
* improved AOP TargetSourceCreator mechanism (supporting LazyInitTargetSource too)
* transaction annotation support for JDK 1.5+ (annotation called "Transactional")
* improved WebLogicJtaTransactionManager (transaction names, isolation levels)
* SqlRowSet support for JDBC (in conjunction with JdbcTemplate's "queryForRowSet")
* Hibernate3 support (in orm.hibernate3; Hibernate 2.1 support is still available)
* JMX support for export of Spring beans as managed resources and for MBean access
* Commons Attributes and JDK 1.5+ annotations for JMX MBean export

This release also contains many minor enhancements, for example:

* factored out BindingErrorProcessor strategy for ServletRequestDataBinder
* improved ParameterMethodNameResolver for Web MVC MultiActionController

For a detailed list of enhancements and bug fixes, see the changelog.

This release candidate is already considered stable and recommended for development use.
We expect Spring 1.2 final to be released in late April.

Watch out for the Spring Web Flow preview release to follow later this week
(for use with Spring 1.2)!
Web Flow will also become part of the nightly build at that time.

Cheers,

Juergen

Just let me know if any spring dependency is missing and I'll add it.

Now in Los Angeles

I got a gig in Los Angeles thanks to Rick Hightower,
so I’m staying in Santa Monica for the next 6 months aprox. It’s a
pretty cool place to live and the job is great, working with latest
technology: JSF, Hibernate, Spring, …
So if you live near or have any suggestions about what to see and what to do, please let me know.
I’m starting a weblog in spanish
for my friends and other people who wants to  read it about the
differencies between Spain and USA, it’ll be kind of funny.

Using Java 1.5 in real world app

It’s almost sure that the current app we’re developing right
now will use Java 5. That’s the first option and will be used
unless some problem arises. So it was time to remember the Taming the Tiger presentation from Joshua Bloch and Neal Gafter in JavaHispano II. The slides are available from the Denver Java Users Group Archives, and it’s a pity that the link to the video of the session seems broken.

As Joshua and Neal advised the only trouble porting pre 1.5 code to 1.5 is that enum can’t
be used as variable name because it’s a reserved word. And of course
the old code does, so it’s time to refactor that variable names. Seems
that appart from that there won’t be any problem.

BTW I’ve also enjoyed the Still More Java Puzzlers slides.

Hibernate 3 + Annotations + Spring 1.2 + Maven

Today I upgraded a prototype working with Hibernate 2 + Spring 1.1
to the latest Hibernate 3.0.1 + Spring 1.2rc2 + Hibernate annotations
3.0beta1 to avoid writing mapping files and use annotations under Java 5.

To use annotation under Maven you need to add the following properties to project.properties

maven.compile.source=1.5
maven.compile.target=1.5

and these all the dependencies you will need. Note that some of them
are not available at ibiblio yet due to a problem of space that will be
solved soon.

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-hibernate</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-dao</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>1.2-rc2</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.0</version>
            <url>http://jakarta.apache.org/commons/collections</url>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>ejb</groupId>
            <artifactId>ejb</artifactId>
            <version>3.0-edr2</version>
        </dependency>
        <dependency>
            <groupId>hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.0beta1</version>
        </dependency>
        <dependency>
            <groupId>jta</groupId>
            <artifactId>jta</artifactId>
            <version>1.0.1B</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>hibernate</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.5H3</version>
        </dependency>

Soon with Maven 2 you won't need all this stuff ;)

As far as the Hibernate 2 to 3 migration as many people have already
said it's just a matter of renaming the net.sf.hibernate imports to
org.hibernate. About the Hibernate annotations I should give Rick the credit as he has already posted the sample code.

To use Spring with the new Hibernate 3 you need to upgrade to Spring
1.2 (1.2rc2 is the latest version currently available) and change org.springframework.orm.hibernate.LocalSessionFactoryBean to org.springframework.orm.hibernate3.LocalSessionFactoryBean in you applicatin context.

To use Java5 annotations instead of mapping files you need to create a hibernate config file (say hibernate.cfg.xml)
with the classes you want to map (or add them to the one you already
have). The reason you need this file is so the Spring framework doesn't
depend on JDK 5.

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="your.domain.class"/>
        <mapping class="another.domain.class"/>
    </session-factory>
</hibernate-configuration>

And then add to your Spring LocalSessionFactoryBean two properties

<property name="configLocation">
    <value>hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
    <value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>

And that's all for the moment.