Spring中の@Scropeコメント


Spring  @Scope  :

  :
	@Target({ElementType.TYPE, ElementType.METHOD})
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	public @interface Scope {

		/**
		 * Specifies the scope to use for the annotated component/bean.
		 * @see ConfigurableBeanFactory#SCOPE_SINGLETON
		 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
		 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
		 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
		 */
		String value() default ConfigurableBeanFactory.SCOPE_SINGLETON;

		/**
		 * Specifies whether a component should be configured as a scoped proxy
		 * and if so, whether the proxy should be interface-based or subclass-based.
		 * Defaults to ScopedProxyMode#NO, indicating that no scoped proxy should be created.
		 * Analogous to  support in Spring XML.
		 */
		ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;

	}
	
	
  :
	value
		singleton	   bean    。(  )
		prototype	   bean    ,      bean         。
		request		   http   ,  bean      。
		session		   httpSession ,  bean      。
		
	proxyMode
		DEFAULT			     。(  )
		NO				     ,   DEFAULT。
		INTERFACES		         (jdk dynamic proxy)。
		TARGET_CLASS	        (cglib)。
		
  :
	  :
		ExcelParseServiceImpl    (singleton)
		BaiduExcelParseServiceImpl    (prototype)
		ExcelParseServiceImpl   BaiduExcelParseServiceImpl
	  :
		  BaiduExcelParseServiceImpl            。
	
	  :
	
		   bean:
		
			@Component //  :     
			public class ExcelParseServiceImpl {

				@Autowired
				private BaiduExcelParseService baiduExcelParseService;

				public BaiduExcelParseService getBaiduExcelParseService() {
					return baiduExcelParseService;
				}

				public void setBaiduExcelParseService(BaiduExcelParseService baiduExcelParseService) {
					this.baiduExcelParseService = baiduExcelParseService;
				}
			}
			
		   bean:
		
			case1:        bean
				@Service()	
				@Scope(value="prototype")
				public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {
					// ...
				}
			
			case2:       bean
				@Service()
				@Scope(value="prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
				public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {
					// ...
				}

			case3:       bean
				@Service()
				@Scope(value="singleton",proxyMode = ScopedProxyMode.TARGET_CLASS)
				public class BaiduExcelParseServiceImpl implements BaiduExcelParseService {
					// ...
				}
	
	   :
		@RunWith(SpringJUnit4ClassRunner.class)
		@ContextConfiguration(locations = "classpath:spring-test.xml")
		public class BaiduExcelParseServiceImplTest extends AbstractJUnit4SpringContextTests{
			
			@Autowired
			private ExcelParseServiceImpl excelParseService;

			@Test
			public void test(){

				BaiduExcelParseService bean1 = MyApplicationContextUtil.getContext().getBean(ExcelParseServiceImpl.class).getBaiduExcelParseService();
				BaiduExcelParseService bean2 = MyApplicationContextUtil.getContext().getBean(ExcelParseServiceImpl.class).getBaiduExcelParseService();

				BaiduExcelParseService bean3 = MyApplicationContextUtil.getContext().getBean(BaiduExcelParseService.class);
				BaiduExcelParseService bean4 = MyApplicationContextUtil.getContext().getBean(BaiduExcelParseService.class);
		//
				System.out.println("---" + bean1);
				System.out.println("---" + bean2);
				System.out.println();
				System.out.println("---" + bean3);
				System.out.println("---" + bean4);
			}
			
		}

	    :
	
		case1:@Scope(value="prototype")         bean

			---com.jxn.service.BaiduExcelParseServiceImpl@16029e2f
			---com.jxn.service.BaiduExcelParseServiceImpl@16029e2f

			---com.jxn.service.BaiduExcelParseServiceImpl@3b2db389
			---com.jxn.service.BaiduExcelParseServiceImpl@45f1413c
			
			  :
				ExcelParseServiceImpl    ,ExcelParseServiceImpl                 baiduExcelParseService    ,
				 bean1、bean2           baiduExcelParseService。
	
		case2:@Scope(value="prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)        bean
		
			---com.jxn.service.BaiduExcelParseServiceImpl@16b7e04a
			---com.jxn.service.BaiduExcelParseServiceImpl@661db63e
			
			---com.jxn.service.BaiduExcelParseServiceImpl@5cf2f5d6
			---com.jxn.service.BaiduExcelParseServiceImpl@429f0ca8
			
	
		case3:@Scope(value="singleton",proxyMode = ScopedProxyMode.TARGET_CLASS)        bean

			---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b
			---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b
			
			---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b
			---com.jxn.service.BaiduExcelParseServiceImpl@4dac40b