11.4 Using Services from Java 2.3.8

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 11.4 Using Services from Java - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 2.3.8 ## 11.4 Using Services from Java One of the powerful things about services is that since they encapsulate re-usable logic, you can use them from other classes, including Java classes. There are a couple of ways you can reuse a service from Java. The simplest way is to move your service into a package within the `grails-app/services` directory. The reason this is important is that it is not possible to import classes into Java from the default package (the package used when no package declaration is present). So for example the `BookService` below cannot be used from Java as it stands:
    class BookService {
        void buyBook(Book book) {
            // logic
        }
    }
However, this can be rectified by placing this class in a package, by moving the class into a sub directory such as `grails-app/services/bookstore` and then modifying the package declaration:

    package bookstoreclass BookService {
        void buyBook(Book book) {
            // logic
        }
    }
An alternative to packages is to instead have an interface within a package that the service implements:

    package bookstoreinterface BookStore {
        void buyBook(Book book)
    }
And then the service:
    class BookService implements bookstore.BookStore {
        void buyBook(Book b) {
            // logic
        }
    }
This latter technique is arguably cleaner, as the Java side only has a reference to the interface and not to the implementation class (although it's always a good idea to use packages). Either way, the goal of this exercise to enable Java to statically resolve the class (or interface) to use, at compile time. Now that this is done you can create a Java class within the `src/java` directory and add a setter that uses the type and the name of the bean in Spring:

    // src/java/bookstore/BookConsumer.java
    package bookstore;public class BookConsumer {    private BookStore store;    public void setBookStore(BookStore storeInstance) {
            this.store = storeInstance;
        }
        …
    }
Once this is done you can configure the Java class as a Spring bean in `grails-app/conf/spring/resources.xml` (for more information see the section on Grails and Spring):
    <bean id="bookConsumer" class="bookstore.BookConsumer">
        <property name="bookStore" ref="bookService" />
    </bean>
or in `grails-app/conf/spring/resources.groovy`:

    import bookstore.BookConsumerbeans = {
        bookConsumer(BookConsumer) {
            bookStore = ref("bookService")
        }
    }
| Quick Reference (hide)