zoukankan      html  css  js  c++  java
  • 【spring源码之Lookup注解的简单应用】

    1. 代码示例1: 如果是抽象类那么抽象类上得是Lookup注解,此时才能将resource注册成一个

      Appconfig.java

    @Configuration
    @ComponentScan(basePackages= "com.luban")
    public class AppConfig {
    
    
    }
    
    

    UserService.java

    @Component
    public abstract class UserService{
    	public void test(){
    		System.out.println("test");
    	}
    }
    
    

    Test.java

    public class Test {
     public static 	void main(String[] args) {	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
     	UserService userService = (UserService) applicationContext.getBean("userService");
     	userService.test();
    }    
    /* 打印Connected to the target VM, address: '127.0.0.1:53158', transport: 'socket'
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' available
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:833)
     at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1334)
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:306)
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1153) 
    */
    

    如果此时修改UserService类时:

    @Component
    public abstract class UserService{
       public void test(){
          System.out.println("test");
       }
       
       @Lookup
       public void a(){
          System.out.println("a");
       }
    }
    // 成功输出test
    

    2.代码示例

    UserServie.java

    @Component
    public  class UserService{
    	@Autowired
    	private OrderService orderService;
    
    	public void test(){
    		System.out.println(orderService);
    	}
    
    }
    

    OrderService.java

    @Component
    @Scope("prototype")
    public class OrderService {
    }
    

    Tset.java

    public class Test {
    	public static 	void main(String[] args) {	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    		UserService userService = (UserService) applicationContext.getBean("userService");
    		userService.test();
    }    
    /* 打印
    com.luban.service.OrderService@3bb9a3ff
    com.luban.service.OrderService@3bb9a3ff
    com.luban.service.OrderService@3bb9a3ff
    */
    

    有没有很奇怪这边打印三次的OrderService 是同一个,我们不再在OrderService类上面加了@@Scope("prototype")

    其实这个问题很简单,只要我们想一下就能知道了,因为UserService 在spring容器中的对象是非懒加载的单例bean,所以对应的OrderService属性注入只能够注入一次,这一点而都不奇怪。

    修改代码

    UserService.java

    @Component
    public  class UserService{
    	@Autowired
    	private OrderService orderService;
    
    
    	public void test(){
    		System.out.println(a());
    	}
    
    	@Lookup("orderService")
    	public OrderService a(){
    		return null;
    	}
    
    }
    

    OrderService.java

    @Component
    @Scope("prototype")
    public  class OrderService {
    }
    
    

    Test.java

    public class Test {
    	public static 	void main(String[] args) {	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    		UserService userService = (UserService) applicationContext.getBean("userService");
    		userService.test();
    }    
    /* 打印
    com.luban.service.OrderService@123ef382
    com.luban.service.OrderService@dbf57b3
    com.luban.service.OrderService@384ad17b
    */
    
  • 相关阅读:
    【Delphi】VCL 使用TCoolBar控件,在系统触发UAC界面时,引发CMSysFontChanged事件导致界面卡死或弹出System Error 1400错误
    【Delphi】 FMX 下 TImageList的使用方法:获取其中一张图片
    如何在电脑睡眠状态下保持程序运行
    【Delphi】使用TIdHTTPServer开发HTTP服务端在Windows2008部署后,外网无法访问
    fedora 25 virtualbox 增强功能安装
    在Win8系统中如何将一般类型的文件放在开始菜单中
    mac上的替代软件
    spring boot 1.4.1 with jsp file sample
    macbook pro 重装系统
    找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher
  • 原文地址:https://www.cnblogs.com/tangliMeiMei/p/15255601.html
Copyright © 2011-2022 走看看