Java Reference
In-Depth Information
Solution
Spring supports JMX by allowing you to export any beans in its IoC container as model MBeans. This can
be done simply by declaring an MBeanExporter instance. With Spring's JMX support, you no longer need
to deal with the JMX API directly, so you can write code that is not JMX specific. In addition, Spring
enables you to declare JSR-160 (Java Management Extensions Remote API) connectors to expose your
MBeans for remote access over a specific protocol by using a factory bean. Spring provides factory beans
for both servers and clients.
Spring's JMX support comes with other mechanisms by which you can assemble an MBean's
management interface. These options include using exporting beans by method names, interfaces, and
annotations. Spring can also detect and export your MBeans automatically from beans declared in the
IoC container and annotated with JMX-specific annotations defined by Spring. The MBeanExporter class
exports beans, delegating to an instance of MBeanInfoAssembler to do the heavy lifting.
How It Works
Suppose that you are developing a utility for replicating files from a source directory to a destination
directory. Let's design the interface for this utility as follows:
package com.apress.springenterpriserecipes.replicator;
...
public interface FileReplicator {
public String getSrcDir();
public void setSrcDir(String srcDir);
public String getDestDir();
public void setDestDir(String destDir);
public void replicate() throws IOException;
}
The source and destination directories are designed as properties of a replicator object, not method
arguments. That means each file replicator instance replicates files only for a particular source and
destination directory. You can create multiple replicator instances in your application.
Before you implement this replicator, you need another class that copies a file from one directory to
another, given its name.
package com.apress.springenterpriserecipes.replicator;
...
public interface FileCopier {
public void copyFile(String srcDir, String destDir, String filename)
throws IOException;
}
There are many strategies for implementing this file copier. For instance, you can make use of the
FileCopyUtils class provided by Spring.
package com.apress.springenterpriserecipes.replicator;
...
import org.springframework.util.FileCopyUtils;
Search WWH ::




Custom Search