网站建设最流行语言,如何用网站做招聘,承德名城建设集团网站,许昌知名网站建设价格spring 注释介绍#xff1a; 默认情况下#xff0c; Spring框架在应用程序启动时加载并热切初始化所有bean。 在我们的应用程序中#xff0c;我们可能有一些非常消耗资源的bean。 我们宁愿根据需要加载此类bean。 我们可以使用Spring Lazy批注实现此目的 。 在本教程中 默认情况下 Spring框架在应用程序启动时加载并热切初始化所有bean。 在我们的应用程序中我们可能有一些非常消耗资源的bean。 我们宁愿根据需要加载此类bean。 我们可以使用Spring Lazy批注实现此目的 。 在本教程中我们将学习如何使用Lazy注释延迟加载我们的bean。 延迟初始化 如果我们用Lazy注释标记我们的Spring配置类则所有带有Bean注释的已定义bean都会被延迟加载 Configuration ComponentScan (basePackages com.programmergirl.university ) Lazy public class AppConfig { Bean public Student student() { return new Student(); } Bean public Teacher teacher() { return new Teacher(); } } 我们还可以通过在方法级别使用此注释来延迟加载单个bean Bean Lazy public Teacher teacher() { return new Teacher(); } 测试延迟加载 让我们通过运行应用程序来快速测试此功能 public class SampleApp { private static final Logger LOG Logger.getLogger(SampleApp. class ); public static void main(String args[]) { AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(AppConfig. class ); LOG.info( Application Context is already up ); // Beans in our Config class lazily loaded Teacher teacherLazilyLoaded context.getBean(Teacher. class ); Student studentLazilyLoaded context.getBean(Student. class ); } } 在控制台上我们将看到 Bean factory for ...AnnotationConfigApplicationContext: ...DefaultListableBeanFactory: [...] ... Application Context is already up Inside Teacher Constructor Inside Student Constructor 显然 Spring在需要时而不是在设置应用程序上下文时初始化了Student和Teacher Bean。 使用 我们还可以在注入点使用Lazy批注构造函数setter或字段级。 假设我们要延迟加载一个Classroom类 Component Lazy public class Classroom { public Classroom() { System.out.println( Inside Classroom Constructor ); } ... } 然后通过Autowired注释将其连接到University bean Component public class University { Lazy Autowired private Classroom classroom; public University() { System.out.println( Inside University Constructor ); } public void useClassroomBean() { this .classroom.getDetails(); ... } } 在这里我们懒惰地注入了Classroom bean。 因此在实例化University对象时Spring将创建代理Classroom对象并将其映射到该对象。 最后只有当我们调用useClassroomBean时 它才会创建实际的Classroom实例 // in our main() method AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(); LOG.info( Application Context is already up ); University university context.getBean(University. class ); LOG.info( Time to use the actual classroom bean... ); university.useClassroomBean(); 上面的代码将产生以下日志 Bean factory for ...AnnotationConfigApplicationContext: ...DefaultListableBeanFactory: [...] ... Inside University Constructor ... Application Context is already up Time to use the actual classroom bean... Inside Classroom Constructor 如我们所见 Classroom对象的实例化被延迟直到其实际需要为止。 请注意对于延迟注入我们必须在组件类以及注入点上都使用Lazy批注。 结论 在本快速教程中我们学习了如何延迟加载Spring Bean。 我们讨论了延迟初始化和延迟注入。 翻译自: https://www.javacodegeeks.com/2019/09/spring-lazy-annotation.htmlspring 注释