Ther Service Layer
Grails 定义了service 层的概念,grails团队不赞成把核心业务放入到Controllers里面去,因为它不能提升重用和划分清晰。
grails把主要的业务逻辑放在业务层里面,controllers去负责处理request流程,转向(redirect)等等。
创建service
grails create-service simple
他的存放地址是grails-app/services/SimpleService.groovy
声明性事务
service中最具代表性的棘手问题是在两个Domian classes中的并行逻辑操作,这是因为他们的持久化跨越了两个操作。service允许我们授权一个事务划分,本质上就是说明一个service中类的所有方法都是被声明的。
class CountryService {
static transactional = false
}
关闭一个自动事务声明。
注意:只要DI是唯一的方法让service declarative方法工作,如果你使用new service 将不会让事务有效。
service 范围
默认情况下访问service方法不是同步,service是单例的,所以你必须小心在里面处理数据库储存,最好或最简单的方法就是不要在service中保存数据。
想要改变这种行为,你可以改变service的scope,目前支持以下几种。
- prototype
- request
- flash
- flow
- conversation
- singleton (defalult)
使用scopes的方法
static scope = "flow"
依赖注入service
基础:
grails使用spring的DI,grails支持DI by convention(约定)。
Grails仅仅支持DI by name,并不支持DI by typed。
给controller注入
class BookController {
def bookService
…
}
class AuthorService {
BookService bookService
}
service给service注入
class AuthorService {
def bookService
}
给Domian 注入
class Book {
…
def bookService
def buyBook() {
bookService.buyBook(this)
}
}
使用java的service
如果把service类放入grails-app/services 中,java类是不能引用他的。
有一个办法可以解决,创建一个as grails-app/services/bookstore目录
在你创建的service中声明一个package bookstore
package bookstore
class BookService {
void buyBook(Book book) {
// logic
}
}
或者声明一个接口
package bookstore;
interface BookStore {
void buyBook(Book book);
}
实现这个service
class BookService implements bookstore.BookStore {
void buyBook(Book b) {
// logic
}
}
创建一个java类放入到src/java中
package bookstore;
// note: this is Java class
public class BookConsumer {
private BookStore store; public void setBookStore(BookStore storeInstance) {
this.store = storeInstance;
}
…
}
进行spring配置,在grails-app/conf/spring/resources.xml 添加这些代码
<bean id="bookConsumer" class="bookstore.BookConsumer">
<property name="bookStore" ref="bookService" />
</bean>
分享到:
相关推荐
The presentation layer handles user interactions, the service layer encapsulates business logic, and the persistence layer communicates with the database for data storage and retrieval. Spring ...
The transport layer's primary purpose is to offer a more dependable service to end-users compared to the underlying network service. It does this by establishing connections solely at the end hosts, ...
- **Create the Service Layer Class**:指导如何创建服务层类,以支持业务逻辑处理。 - **Security and Permissions Service**:讲解了安全性和权限服务的实现方法,保障了Liferay应用的安全运行。 #### 版权与...
These problems can degrade the quality of service for applications relying on this layer. - TCP congestion control mechanisms, such as slow start and congestion avoidance, help manage network ...
第五章:服务层安全(Securing the Service Layer) - 方法级安全:讲解如何对服务层的方法实现细粒度的访问控制,包括使用Spring Security注解进行安全控制。 - 服务层认证:探讨服务层的认证机制,如何确保只有...
* cs_convert: cslib user api layer: common library error: The conversion/operation was stopped due to a syntax error in the source field. * cs_convert: cslib user api layer: common library error: The ...
某些项目需要将单页应用程序与经典的 ASP.NET MVC 方法相...* and it uses a list of .json files to fetch data faking the service layer to retrive a list of users. 启动 ASP.NET MVC/AngularJS 项目的有用代码库
At the core of network applications lie several guiding concepts, such as understanding transport-layer service models. These models dictate how data is exchanged between applications, whether it's ...
You will start off by creating, implementing, and hosting a basic HelloWorld WCF Service followed by deploying the service and publishing it to IIS. Next, you will learn how to create and test a ...
23. The five layers in the Internet protocol stack are – from top to bottom – the application layer, the transport layer, the network layer, the link layer, and the physical layer. The principal ...
The application layer services section covers the general description of the services, the format of the service descriptions, and the format of service primitives. It also includes the specification ...
5. Congestion control is typically not a service provided by the link-layer protocol. Instead, it is a responsibility of the transport layer. Thus, A) congestion control is the correct answer. 6. The...
The Network Layer in the Internet Section 5.7. Summary Chapter 6. The Transport Layer Section 6.1. The Transport Service Section 6.2. Elements of Transport Protocols Section 6.3. A Simple ...
The Network Layer in the Internet Section 5.7. Summary Chapter 6. The Transport Layer Section 6.1. The Transport Service Section 6.2. Elements of Transport Protocols Section 6.3. A Simple...
The Network Layer in the Internet Section 5.7. Summary Chapter 6. The Transport Layer Section 6.1. The Transport Service Section 6.2. Elements of Transport Protocols Section 6.3. A Simple ...
The Network Layer in the Internet Section 5.7. Summary Chapter 6. The Transport Layer Section 6.1. The Transport Service Section 6.2. Elements of Transport Protocols Section 6.3. A Simple...
The Network Layer in the Internet Section 5.7. Summary Chapter 6. The Transport Layer Section 6.1. The Transport Service Section 6.2. Elements of Transport Protocols Section 6.3. A Simple...