Last updated by admin 4 years ago
??
Grails??(Grails Services)
??????????????A service is a class that holds one or more methods that implement business logic. Logical parts of the business logic are contained in seperate service classes. ??????????????????????????????????????????????????Services need to be registered with the middle tier and also need transactional configuration next to the configuration of optional AOP services.??????????????(application-scoped)??????????(instance variables)??????????????????????????
????????????????????"Service"."??"???????"CountryService"?????????????????????????????"transactional"?false????????class CountryService {
boolean transactional = false
}????(Dependency Injection)
????????????????????????CountrySerice???????"countryService"?CountryService countryService
javax.sql.DataSource dataSource
????
???????????????????????????????????controller??class CountryService {
def String sayHello(String name) {
return "hello ${name}"
}
}class GreetingController {
CountryService countryService
def helloAction = {
render(countryService.sayHello(params.name))
}
}package serviceinterfaces;public interface CountryServiceInt { public String sayHello(String name); }
class CountryService implements serviceinterfaces.CountryServiceInt { def String sayHello(String name) { return "hello ${name}" } }
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



