Japanese Services

Last updated by admin 3 years ago

???? {excerpt:hidden=true}Services{excerpt}

Grails ???? {excerpt:hidden=true}Grails Services{excerpt}

{excerpt:hidden=true} A service is a class that holds one or more methods that implement business logic. Logical parts of the business logic are contained in separate service classes. Services are demarcated by transactions to make units of work atomic on the persistence level. When certain types of exceptions are thrown transactions are rolled back. {excerpt} ???????????????????1????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????? ?????????????????????

{excerpt:hidden=true} Services need to be registered with the middle tier and also need transactional configuration next to the configuration of optional AOP services. {excerpt} ???????????????????????AOP????????????????????????????

{excerpt:hidden=true} Services are application-scoped, which means it is not a good idea to use instance variables because they can and will be modified concurrently (at the same time). {excerpt} ???????????????????????????????????????????????????????????????????????????????????????

????????????? {excerpt:hidden=true}Coding conventions for services{excerpt}

{excerpt:hidden=true} Service class names should start with the name of the service and end with "Service". A country service would thus be called "CountryService". {excerpt} ?????????????????????????"Service"?????????????????????????????"CountryService"???????????????

{excerpt:hidden=true} By default transaction demarcation is enabled for all services, meaning that by default all methods of a service are wrapped in a transaction. Transaction demarcation can be disabled for specific services by creating a "transactional" property and settting is to false: {excerpt} ????????????????????????????????????????????????????????????????????? ?????????????????????????"transactional"??????false????????????????????? ?????????????:

class CountryService {
    boolean transactional = false
}

?????(Dependency Injection) {excerpt:hidden=true}Dependency Injection{excerpt}

{excerpt:hidden=true} Other services can be injected into a service by creating corresponding properties. To inject the CountryService create a "countryService" property. {excerpt} ??????????????????????????????????????????????????CountryService????????"countryService"????????????

CountryService countryService
{excerpt:hidden=true} A javax.sql.DataSource instance can be retrieved for the configured database by adding the property: {excerpt} javax.sql.DataSource?????????????????????????????????????????????????????????:
javax.sql.DataSource dataSource
{excerpt:hidden=true} This could be used in conjunction with Spring's JdbcTemplate to perform direct transactional SQL operations or with Groovy Sql {excerpt} ??????Spring?JdbcTemplate???????????????????SQL???Groovy Sql?????????????????

??????? {excerpt:hidden=true}Accessing Services{excerpt}

{excerpt:hidden=true} In a typical multitier scenario you can easily access services using dependency injection, for example from a controller: {excerpt} ?????????????????????????????????????????????????????????????:

class CountryService {
    def String sayHello(String name) {
        return "hello ${name}"
    }
}

class GreetingController { CountryService countryService def helloAction = { render(countryService.sayHello(params.name)) } }

{excerpt:hidden=true} That's all, you can test it by typing this URL http://myserver:8080/myapp/greeting/helloAction?name=Falken in your browser location bar. A page containing "hello Falken" will be returned. See Grails controllers for more details about controllers. {excerpt} ??????????????????????URL http://myserver:8080/myapp/greeting/helloAction?name=Falken???????????????????"hello Falken"???????????????????????????????Grails ????????????????

{excerpt:hidden=true} Alternatively, you can access services from other places of your web application (like servlets) if you need it. Service objects are beans which can be retrieved from the Spring Bean Factory. You can proceed this way: {excerpt} ????????????(??????????)web??????????????????????????????????????????????Spring Bean Factory????????????Bean?????????????????????:

{excerpt:hidden=true} Implement a java interface containing the service method definitions which you want to be available, for example: {excerpt} ??????????????????????Java??????????????:

package serviceinterfaces;

public interface CountryServiceInt { public String sayHello(String name); }

{excerpt:hidden=true} Save it in your "<..>/myapp/src/java/serviceinterfaces" directory {excerpt} "<..>/myapp/src/java/serviceinterfaces"????????????????

{excerpt:hidden=true} Modify the CountryService groovy class for implementing the CountryServiceInt interface: {excerpt} CountryServiceInt?????????groovy?????CountryService??????:

class CountryService implements serviceinterfaces.CountryServiceInt {

def String sayHello(String name) { return "hello ${name}" } }

{excerpt:hidden=true} For accessing the service from a servlet you can do this: {excerpt} ????????????????????????????????????:
ApplicationContext ctx = (ApplicationContext) request.getSession().getServletContext()
  .getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
CountryServiceInt service = (CountryServiceInt) ctx.getBean("countryService");
String str = service.sayHello(request.getParameter.("name"));
//do something with str