{"id":378475,"date":"2024-06-09T15:22:28","date_gmt":"2024-06-09T15:22:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=378475"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=378475","title":{"rendered":"<span>Lazy Loading \u0432 Java<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/890\/51d\/4a7\/89051d4a75663ef837ca29599aaa8026.jpg\" width=\"1280\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/890\/51d\/4a7\/89051d4a75663ef837ca29599aaa8026.jpg\" data-blurred=\"true\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u043b\u0438, \u043a\u0430\u043a \u043a\u043e\u0442\u0438\u043a\u0438, \u043b\u0435\u043d\u0438\u0432\u043e \u043f\u043e\u0442\u044f\u0433\u0438\u0432\u0430\u044f\u0441\u044c \u0438 \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f\u0441\u044c \u043a\u043b\u0443\u0431\u043a\u043e\u043c, \u044d\u043a\u043e\u043d\u043e\u043c\u044f\u0442 \u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e?<\/p>\n<p>\u041a\u0430\u043a \u0438 \u043d\u0430\u0448\u0438 \u0445\u0432\u043e\u0441\u0442\u0430\u0442\u044b\u0435 \u0434\u0440\u0443\u0437\u044c\u044f, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043a\u0430\u043a <strong>Lazy Loading, <\/strong>\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0442\u0438\u043a\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432 Java. <em>\u0411\u0443\u0434\u0435\u043c \u043a\u0430\u043a \u043a\u043e\u0442\u0438\u043a\u0438 \u2014 \u0443\u043c\u043d\u044b\u043c\u0438, \u044d\u043a\u043e\u043d\u043e\u043c\u043d\u044b\u043c\u0438 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438!<\/em><\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412 Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Lazy Loading: <strong>Lazy Initialization<\/strong>, <strong>Proxy<\/strong> \u0438 <strong>Holder<\/strong>.<\/p>\n<h4>Lazy Initialization<\/h4>\n<p>Lazy Initialization \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c. \u042d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Lazy Loading:<\/p>\n<pre><code class=\"java\">public class LazyInitializedSingleton {     private static LazyInitializedSingleton instance;      private LazyInitializedSingleton() {         \/\/ private constructor     }      public static LazyInitializedSingleton getInstance() {         if (instance == null) {             instance = new LazyInitializedSingleton();         }         return instance;     }      public void displayMessage() {         System.out.println(\"Lazy Initialization Singleton instance.\");     } }  public class Main {     public static void main(String[] args) {         LazyInitializedSingleton instance = LazyInitializedSingleton.getInstance();         instance.displayMessage();     } }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 <code>LazyInitializedSingleton<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>getInstance()<\/code>. \u0425\u043e\u0442\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c.<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">public class ThreadSafeLazyInitializedSingleton {     private static ThreadSafeLazyInitializedSingleton instance;      private ThreadSafeLazyInitializedSingleton() {         \/\/ private constructor     }      public static synchronized ThreadSafeLazyInitializedSingleton getInstance() {         if (instance == null) {             instance = new ThreadSafeLazyInitializedSingleton();         }         return instance;     }      public void displayMessage() {         System.out.println(\"Thread-Safe Lazy Initialization Singleton instance.\");     } }<\/code><\/pre>\n<h4>Proxy<\/h4>\n<p>\u041f\u0430\u0442\u0442\u0435\u0440\u043d Proxy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0443, \u043e\u0442\u043b\u043e\u0436\u0438\u0432 \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f. \u0412 Java \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u043e\u043a\u0441\u0438 \u0438\u043b\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u043a\u0441\u0438-\u043a\u043b\u0430\u0441\u0441\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u043a\u0441\u0438:<\/p>\n<pre><code class=\"java\">import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;  interface Image {     void display(); }  class RealImage implements Image {     private String filename;      public RealImage(String filename) {         this.filename = filename;         loadImageFromDisk();     }      private void loadImageFromDisk() {         System.out.println(\"Loading \" + filename);     }      public void display() {         System.out.println(\"Displaying \" + filename);     } }  class ImageProxyHandler implements InvocationHandler {     private String filename;     private Image realImage;      public ImageProxyHandler(String filename) {         this.filename = filename;     }      @Override     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {         if (realImage == null) {             realImage = new RealImage(filename);         }         return method.invoke(realImage, args);     } }  public class Main {     public static void main(String[] args) {         Image imageProxy = (Image) Proxy.newProxyInstance(                 Image.class.getClassLoader(),                 new Class[]{Image.class},                 new ImageProxyHandler(\"test.jpg\"));          imageProxy.display();  \/\/ \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f     } }<\/code><\/pre>\n<p><code>ImageProxyHandler<\/code> \u043e\u0442\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>RealImage<\/code> \u0434\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <code>display()<\/code>.<\/p>\n<h4>Holder<\/h4>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 Holder \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u0435\u044c\u0441\u043c\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"java\">public class HolderSingleton {     private HolderSingleton() {         \/\/ private constructor     }      private static class Holder {         private static final HolderSingleton INSTANCE = new HolderSingleton();     }      public static HolderSingleton getInstance() {         return Holder.INSTANCE;     }      public void displayMessage() {         System.out.println(\"Holder Singleton instance.\");     } }  public class Main {     public static void main(String[] args) {         HolderSingleton instance = HolderSingleton.getInstance();         instance.displayMessage();     } }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Holder<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u043b\u0435 <code>INSTANCE<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>getInstance()<\/code>.<\/p>\n<h3>Lazy Loading \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445<\/h3>\n<h4>Hibernate<\/h4>\n<p>\u0412 <a href=\"https:\/\/hibernate.org\/\">Hibernate<\/a>, Lazy Loading \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@ManyToOne<\/code>, <code>@OneToMany<\/code>, <code>@OneToOne<\/code>, <code>@ManyToMany<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>fetch = FetchType.LAZY<\/code>:<\/p>\n<pre><code class=\"java\">@Entity public class Company {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;     private String name;      @OneToMany(mappedBy = \"company\", fetch = FetchType.LAZY)     private List&lt;Employee> employees;      \/\/ getters and setters }  @Entity public class Employee {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;     private String name;      @ManyToOne(fetch = FetchType.LAZY)     @JoinColumn(name = \"company_id\")     private Company company;      \/\/ getters and setters }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043d\u0435\u0439 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0441\u0440\u0430\u0437\u0443, \u0430 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043f\u043e\u043b\u044e <code>employees<\/code>.<\/p>\n<p>\u041c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 Lazy \u0432 Hibernate:<\/p>\n<p><strong>LazyInitializationException <\/strong>\u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043b\u0435\u043d\u0438\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u044b\u0442\u0430\u044e\u0442\u0441\u044f \u0431\u044b\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0441\u0435\u0441\u0441\u0438\u0438.<\/p>\n<p><strong>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <a class=\"mention\" href=\"\/users\/Transactional\">@Transactional<\/a>:<\/strong> \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u0435\u0441\u0441\u0438\u044f Hibernate \u0430\u043a\u0442\u0438\u0432\u043d\u0430 \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043b\u0435\u043d\u0438\u0432\u044b\u043c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@Service public class CompanyService {     @Autowired     private CompanyRepository companyRepository;      @Transactional     public Company getCompanyWithEmployees(Long companyId) {         Company company = companyRepository.findById(companyId).orElseThrow();         \/\/ \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438         company.getEmployees().size();         return company;     } }<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p><strong>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/strong> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043b\u0435\u043d\u0438\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@EntityGraph(attributePaths = {\"employees\"}) @Query(\"SELECT c FROM Company c WHERE c.id = :id\") Optional&lt;Company> findByIdWithEmployees(@Param(\"id\") Long id);<\/code><\/pre>\n<h4>@Lazy \u0432 Spring<\/h4>\n<p>Spring \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@Lazy<\/code> \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0438\u043d\u043e\u0432. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">@Configuration public class AppConfig {      @Bean     @Lazy     public ServiceBean serviceBean() {         return new ServiceBean();     } }  @Component public class ClientBean {      private final ServiceBean serviceBean;      @Autowired     public ClientBean(@Lazy ServiceBean serviceBean) {         this.serviceBean = serviceBean;     }      public void doSomething() {         serviceBean.performAction();     } }<\/code><\/pre>\n<p>\u0411\u0438\u043d <code>ServiceBean<\/code> \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043d\u0435\u043c\u0443 \u0447\u0435\u0440\u0435\u0437 <code>ClientBean<\/code>.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439:<\/strong><\/p>\n<p>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<pre><code class=\"java\">@Lazy @Configuration @ComponentScan(basePackages = \"com.example.lazy\") public class LazyConfig {      @Bean     public MainService mainService() {         return new MainService();     }      @Bean     @Lazy     public SecondaryService secondaryService() {         return new SecondaryService();     } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"java\">@RunWith(SpringRunner.class) @ContextConfiguration(classes = LazyConfig.class) public class LazyInitializationTest {      @Autowired     private ApplicationContext context;      @Test     public void testLazyInitialization() {         assertFalse(context.containsBean(\"secondaryService\"));         MainService mainService = context.getBean(MainService.class);         mainService.callSecondaryService();         assertTrue(context.containsBean(\"secondaryService\"));     } }<\/code><\/pre>\n<p>\u0412 \u0442\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0431\u0438\u043d <code>secondaryService<\/code> \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 <code>callSecondaryService<\/code>.<\/p>\n<hr\/>\n<p>Lazy loading \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c lazy loading, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430\u043c \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, lazy loading \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043d\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0443.<\/p>\n<hr\/>\n<p>\u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e Java-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u0443\u0440\u043e\u043a\u0438 \u043e\u0442 Otus:<\/p>\n<ul>\n<li>\n<p>11 \u0438\u044e\u043d\u044f: \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 batch-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 Jdbc. \u041d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u043d\u0438 \u0441\u0442\u0440\u043e\u043a \u0441\u0440\u0430\u0437\u0443. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/lasq\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>25 \u0438\u044e\u043d\u044f: Redis \u0438 Java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432 java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043c\u043e\u0436\u043d\u043e<br \/>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Redis \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 in-memory \u043a\u0435\u0448\u0430, \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/rmlh\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/819357\/\"> https:\/\/habr.com\/ru\/articles\/819357\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u043b\u0438, \u043a\u0430\u043a \u043a\u043e\u0442\u0438\u043a\u0438, \u043b\u0435\u043d\u0438\u0432\u043e \u043f\u043e\u0442\u044f\u0433\u0438\u0432\u0430\u044f\u0441\u044c \u0438 \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f\u0441\u044c \u043a\u043b\u0443\u0431\u043a\u043e\u043c, \u044d\u043a\u043e\u043d\u043e\u043c\u044f\u0442 \u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e?<\/p>\n<p>\u041a\u0430\u043a \u0438 \u043d\u0430\u0448\u0438 \u0445\u0432\u043e\u0441\u0442\u0430\u0442\u044b\u0435 \u0434\u0440\u0443\u0437\u044c\u044f, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043a\u0430\u043a <strong>Lazy Loading, <\/strong>\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0442\u0438\u043a\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432 Java. <em>\u0411\u0443\u0434\u0435\u043c \u043a\u0430\u043a \u043a\u043e\u0442\u0438\u043a\u0438 \u2014 \u0443\u043c\u043d\u044b\u043c\u0438, \u044d\u043a\u043e\u043d\u043e\u043c\u043d\u044b\u043c\u0438 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438!<\/em><\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0412 Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Lazy Loading: <strong>Lazy Initialization<\/strong>, <strong>Proxy<\/strong> \u0438 <strong>Holder<\/strong>.<\/p>\n<h4>Lazy Initialization<\/h4>\n<p>Lazy Initialization \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c. \u042d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Lazy Loading:<\/p>\n<pre><code class=\"java\">public class LazyInitializedSingleton {     private static LazyInitializedSingleton instance;      private LazyInitializedSingleton() {         \/\/ private constructor     }      public static LazyInitializedSingleton getInstance() {         if (instance == null) {             instance = new LazyInitializedSingleton();         }         return instance;     }      public void displayMessage() {         System.out.println(\"Lazy Initialization Singleton instance.\");     } }  public class Main {     public static void main(String[] args) {         LazyInitializedSingleton instance = LazyInitializedSingleton.getInstance();         instance.displayMessage();     } }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 <code>LazyInitializedSingleton<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>getInstance()<\/code>. \u0425\u043e\u0442\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c.<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">public class ThreadSafeLazyInitializedSingleton {     private static ThreadSafeLazyInitializedSingleton instance;      private ThreadSafeLazyInitializedSingleton() {         \/\/ private constructor     }      public static synchronized ThreadSafeLazyInitializedSingleton getInstance() {         if (instance == null) {             instance = new ThreadSafeLazyInitializedSingleton();         }         return instance;     }      public void displayMessage() {         System.out.println(\"Thread-Safe Lazy Initialization Singleton instance.\");     } }<\/code><\/pre>\n<h4>Proxy<\/h4>\n<p>\u041f\u0430\u0442\u0442\u0435\u0440\u043d Proxy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0443, \u043e\u0442\u043b\u043e\u0436\u0438\u0432 \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f. \u0412 Java \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u043e\u043a\u0441\u0438 \u0438\u043b\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u043a\u0441\u0438-\u043a\u043b\u0430\u0441\u0441\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u043a\u0441\u0438:<\/p>\n<pre><code class=\"java\">import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;  interface Image {     void display(); }  class RealImage implements Image {     private String filename;      public RealImage(String filename) {         this.filename = filename;         loadImageFromDisk();     }      private void loadImageFromDisk() {         System.out.println(\"Loading \" + filename);     }      public void display() {         System.out.println(\"Displaying \" + filename);     } }  class ImageProxyHandler implements InvocationHandler {     private String filename;     private Image realImage;      public ImageProxyHandler(String filename) {         this.filename = filename;     }      @Override     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {         if (realImage == null) {             realImage = new RealImage(filename);         }         return method.invoke(realImage, args);     } }  public class Main {     public static void main(String[] args) {         Image imageProxy = (Image) Proxy.newProxyInstance(                 Image.class.getClassLoader(),                 new Class[]{Image.class},                 new ImageProxyHandler(\"test.jpg\"));          imageProxy.display();  \/\/ \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f     } }<\/code><\/pre>\n<p><code>ImageProxyHandler<\/code> \u043e\u0442\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>RealImage<\/code> \u0434\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <code>display()<\/code>.<\/p>\n<h4>Holder<\/h4>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 Holder \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u0435\u044c\u0441\u043c\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"java\">public class HolderSingleton {     private HolderSingleton() {         \/\/ private constructor     }      private static class Holder {         private static final HolderSingleton INSTANCE = new HolderSingleton();     }      public static HolderSingleton getInstance() {         return Holder.INSTANCE;     }      public void displayMessage() {         System.out.println(\"Holder Singleton instance.\");     } }  public class Main {     public static void main(String[] args) {         HolderSingleton instance = HolderSingleton.getInstance();         instance.displayMessage();     } }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Holder<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u043b\u0435 <code>INSTANCE<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>getInstance()<\/code>.<\/p>\n<h3>Lazy Loading \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445<\/h3>\n<h4>Hibernate<\/h4>\n<p>\u0412 <a href=\"https:\/\/hibernate.org\/\">Hibernate<\/a>, Lazy Loading \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@ManyToOne<\/code>, <code>@OneToMany<\/code>, <code>@OneToOne<\/code>, <code>@ManyToMany<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>fetch = FetchType.LAZY<\/code>:<\/p>\n<pre><code class=\"java\">@Entity public class Company {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;     private String name;      @OneToMany(mappedBy = \"company\", fetch = FetchType.LAZY)     private List&lt;Employee> employees;      \/\/ getters and setters }  @Entity public class Employee {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;     private String name;      @ManyToOne(fetch = FetchType.LAZY)     @JoinColumn(name = \"company_id\")     private Company company;      \/\/ getters and setters }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043d\u0435\u0439 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0441\u0440\u0430\u0437\u0443, \u0430 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043f\u043e\u043b\u044e <code>employees<\/code>.<\/p>\n<p>\u041c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 Lazy \u0432 Hibernate:<\/p>\n<p><strong>LazyInitializationException <\/strong>\u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043b\u0435\u043d\u0438\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u044b\u0442\u0430\u044e\u0442\u0441\u044f \u0431\u044b\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0441\u0435\u0441\u0441\u0438\u0438.<\/p>\n<p><strong>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <a class=\"mention\" href=\"\/users\/Transactional\">@Transactional<\/a>:<\/strong> \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u0435\u0441\u0441\u0438\u044f Hibernate \u0430\u043a\u0442\u0438\u0432\u043d\u0430 \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043b\u0435\u043d\u0438\u0432\u044b\u043c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@Service public class CompanyService {     @Autowired     private CompanyRepository companyRepository;      @Transactional     public Company getCompanyWithEmployees(Long companyId) {         Company company = companyRepository.findById(companyId).orElseThrow();         \/\/ \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438         company.getEmployees().size();         return company;     } }<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p><strong>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/strong> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043b\u0435\u043d\u0438\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@EntityGraph(attributePaths = {\"employees\"}) @Query(\"SELECT c FROM Company c WHERE c.id = :id\") Optional&lt;Company> findByIdWithEmployees(@Param(\"id\") Long id);<\/code><\/pre>\n<h4>@Lazy \u0432 Spring<\/h4>\n<p>Spring \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@Lazy<\/code> \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0438\u043d\u043e\u0432. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">@Configuration public class AppConfig {      @Bean     @Lazy     public ServiceBean serviceBean() {         return new ServiceBean();     } }  @Component public class ClientBean {      private final ServiceBean serviceBean;      @Autowired     public ClientBean(@Lazy ServiceBean serviceBean) {         this.serviceBean = serviceBean;     }      public void doSomething() {         serviceBean.performAction();     } }<\/code><\/pre>\n<p>\u0411\u0438\u043d <code>ServiceBean<\/code> \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u043d\u0435\u043c\u0443 \u0447\u0435\u0440\u0435\u0437 <code>ClientBean<\/code>.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439:<\/strong><\/p>\n<p>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<pre><code class=\"java\">@Lazy @Configuration @ComponentScan(basePackages = \"com.example.lazy\") public class LazyConfig {      @Bean     public MainService mainService() {         return new MainService();     }      @Bean     @Lazy     public SecondaryService secondaryService() {         return new SecondaryService();     } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"java\">@RunWith(SpringRunner.class) @ContextConfiguration(classes = LazyConfig.class) public class LazyInitializationTest {      @Autowired     private ApplicationContext context;      @Test     public void testLazyInitialization() {         assertFalse(context.containsBean(\"secondaryService\"));         MainService mainService = context.getBean(MainService.class);         mainService.callSecondaryService();         assertTrue(context.containsBean(\"secondaryService\"));     } }<\/code><\/pre>\n<p>\u0412 \u0442\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0431\u0438\u043d <code>secondaryService<\/code> \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 <code>callSecondaryService<\/code>.<\/p>\n<hr\/>\n<p>Lazy loading \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c lazy loading, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430\u043c \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, lazy loading \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043d\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0443.<\/p>\n<hr\/>\n<p>\u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e Java-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u0443\u0440\u043e\u043a\u0438 \u043e\u0442 Otus:<\/p>\n<ul>\n<li>\n<p>11 \u0438\u044e\u043d\u044f: \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 batch-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 Jdbc. \u041d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0442\u043d\u0438 \u0441\u0442\u0440\u043e\u043a \u0441\u0440\u0430\u0437\u0443. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/lasq\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<\/li>\n<li>\n<p>25 \u0438\u044e\u043d\u044f: Redis \u0438 Java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432 java \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043c\u043e\u0436\u043d\u043e<br \/>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Redis \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 in-memory \u043a\u0435\u0448\u0430, \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/rmlh\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/819357\/\"> https:\/\/habr.com\/ru\/articles\/819357\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-378475","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=378475"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378475\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}