Some Useful Note I Took From Introduction to Node.js From FEM.


Spring Dependency Injection & Auto-wiring

  • what’s going on at runtime is when, Spring instantiates your video service, it goes and it reads, and it sees
    @AutoWired. It then goes and says, what type do I need? And, it looks, at the type of the member variable that’s
    being AutoWired. And then what it does is it goes through and it looks in the configuration object and it says,
    is there a method that’s annotated with @Beam that returns the appropriate type that I’m looking for? If yes, I
    need to call it and get the return
    value from it and use that to plug into this member variable.

Spring Configuration Annotations

  • if it finds an interface for something that is @Autowired throughout your application and then it finds an
    implementer of that interface, it will automatically associate that implemented with that interface and then
    wherever there is an @Autowired for that interface it will take the implementer in and insert it.

H2 Database

OAuth 2.0

Java Persistent API (JPA)

  • Steps to implement it in your application:
    1. Create a class that you want to store information for. (Video.class)
      1. Tell JPA the object you need to store by putting @Entity annotation on top of that class,
        signaling the JPA this is something we need to store in the database
        public class Video {}
      2. We need a unique way to identify each object. Adding @Id annotation to the property in the video
        that we want to make unique. Also, JPA can automatically generate unique IDs for each of our
        object instances that we store by adding @GeneratedValue(strategy = GenerationType.AUTO)
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    2. Now, we need to provide the information that Spring Data JPA needs to figure out how to automatically
      provide this storage logic for us by creating a repository class.
      public interface VideoRepository extends CrudRepository<Video, Long>{
           // Find all videos with a matching title (e.g.,
           public Collection-video- findByName(String title);
      1. we take our video repository and we add the @repository annotation to it
      2. second thing we do is we make sure that the repository interface extends the crud repository interface. crud repository specifies a variety of default methods like, Save to save an object, Find a particular object by its ID.
      3. in the crud repository parameterization, is the type of object we’re going to store, which is a video. And then the type of ID that videos have. In this case, if we go back and look at our video, we see that their ID is a long. So when we were parameterize it, we parameterize the crud repository with a long.
    3. In order to tell Spring to go and instantiate a implementation of this interface that automatically provides the logic that we need, we go, and in our Application class, all we have to do is add one new annotation, and that is @EnableJPARepositories. (Application.class)
      1. we’re going to tell it where we want it to go and find those repository interfaces that it should provide implementations for.
        In this case we’re saying, go and look at the package associated with the video repository class, and any classes that are in the video repository classes parent package, you should automatically go and create implementations of them.
        @EnableJpaRepositories(basePackageClasses = VideoRepository.class)
    4. Create a class that you want to use the Repository and create an instance of that object you want to store in
      the database. Then add @Autowired annotation on top of it. (videoService.class)
      private VideoRepository videos;
      1. if we go back to the application, we are no longer providing the actual implementation. Instead, because
        we’ve added this enableJPArepositories, Spring is going to automatically dynamically create an
        implementation of that repository that provides all of the functionality we need to save video objects
        into a database. And then once it’s created it, it’s going to automatically go and inject it or
        auto-wire it into our video repository users. So all of the places that we are, at auto-wiring a video
        repository, Spring will take the JPA video repository implementation that it dynamically generates and
        attach it to all of the right locations. And then if we go and look down, we’ll see that even though we
        didn’t directly define it, we now have a method to save videos, we have a method to find all the videos
        that have been saved in the database, or we can even go and search for particular videos.

Spring Data Rest

  • override exported repository:
  • Step to create Spring Data Rest:
    1. Why Spring Data Rest instead of JPA:
      1. if you look at the video service controller from the video controller with JPA example, if we open this controller up, one of the things that we’ll rapidly see is that we’ve created this video repository that is dynamically provided by spraying an at auto wired into our controller, and then most of our controller methods are essentially just delegating to the repository. So when somebody wants to add a video, we’re simply calling and then returning. When somebody wants to get the list of videos, we’re simply getting the list of videos and returning it to them from videos.findall. When somebody wants to find a particular video by name, we’re simply delegating to the repository and asking it to do the work.
      2. Spring data rest can automatically take a repository and expose it and it’s various methods through a restful interface.So it can automatically create a controller that will expose a video repository and all of the methods on it like save, or find by name or delete through a restful interface that can receive HTTP requests, so we don’t even actually have to go and create a controller that can delegate to it.
    2. In VideoRepository.class:
      1. we’ve taken our initial video repository andwe’ve added an annotation to it @RepositoryRestResource,and we’ve provided a path to it which if we go andlook at the path, we’ll see that that path is /video. we can actually completely eliminate the videoservice controller because we can simply add this single annotation toour repository, and suddenly, it will go andexpose this entire repository through rest and allow clients to send HTTP requests.
        @RepositoryRestResource(path = VideoSvcApi.VIDEO_SVC_PATH)
        public interface VideoRepositoryextends CrudRepository{}
      2. The other interesting new annotation that we’ll addis we want to be able to have methods to find by name and find by duration.In this case, we’ve actually added a find by duration less than method,when Spring is going to provide an implementation of that we’ll find allvideos with a duration less than what we provide.But one thing we need to do is tell it how to map the parameters from an HTTPrequest to these particular repository methods.So the way that we do that is we add these app param annotations andwhat this annotation says is go and look at the title parameter whichis the title parameter in either the query string,or however it’s sent to the server, extract it,and provide it as the title that we’re searching for in this find by name method.Similarly, we’re saying, go andget the duration parameter out of the request in this case marked by @param,and provide it for the duration that we’re going and searching by in the database.
        public Collection

Leave a Reply

Close Menu