## 8.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 re-use 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 a critical step 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 bookstore
    class BookService {
    	void buyBook(Book book) {
    		// logic
    	}
    }

An alternative to packages is to instead have an interface within a package that the service implements:

    package bookstore;
    interface 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. 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` package, and provide a setter that uses the type and the name of the bean in Spring:

    package bookstore;
    // note: this is Java class
    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 one this see the section on Grails and Spring):

    <bean id="bookConsumer" class="bookstore.BookConsumer">
    	<property name="bookStore" ref="bookService" />
    </bean>