美文网首页
2018-07-09 Understanding the rel

2018-07-09 Understanding the rel

作者: 猪迹 | 来源:发表于2018-07-09 14:12 被阅读0次

Entry of application

Class ServiceApplication acts as the entry of the whole application, which is annotated by @SpringBootApplication

@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        // Application entry
        // Start the embedded Tomcat , initial Spring environment & components
        SpringApplication.run(ServerApplication.class,args);
    }
}

Service inside the application

A service is registered by using annotation @Service, with a class implements a service-interface.
The service implementation may use some repository, which provides the ability to interact with database through JPA.
The service implementation uses @override to override methods exposed by the service-interface

@Service
public class SomeServiceImpl implements ISomeService {
    @Autowired
    private SomeRepository someRepository;

    @Override
    public List<SomeObject> getList() {
        return someRepository.findAll();
    }

    @Override
    public SomeObject getById(String id) {
        return someRepository.findById(id);
    }

    @Override
    public SomeObject save(SomeObject item) {
        return someRepository.save(item);
    }
}

Service interface

Service interface will be used for registering to ZooKeeper, to declare that 'I can provide such a service'.
There is no annotation on the service-interface.
The dubbo-annotation Service on service-implementation class will guide the system to find the service-interface implemented by the service-implementation class.
Then the system will automatically register this interface to ZooKeeper.

public interface ISomeService {
    List<SomeObject > getList();
    SomeObject getById(String id);
    SomeObject save(SomeObject someObject );
}

Repository

Repository can be used to simplify the coding to interact with databases.

public interface SomeObjectRepository extends JpaRepository<SomeObject, String> {
    Page<SomeObject> findById(String id, Pageable pageable);
    SomeObjectfindById(String id);
}

JPA entity

JPA use the entity annotated with @Entity to create back-end table in the databases

@Entity
@Table(name = "someobject")
public class SomeObject extends IDEntity {
    @Column
    private Integer count;
    //getter, setter
    //...
}

相关文章

网友评论

      本文标题:2018-07-09 Understanding the rel

      本文链接:https://www.haomeiwen.com/subject/cskzuftx.html