spring 組件基於註解的註冊方式

spring 中經常使用的組件標籤有:spring

@Controller:控制層ide

@Service:業務層spa

@Repository:數據層code

@Component:普通的pojo注入到spring容器component

 

組件註冊方式:對象

 @ComponentScan  掃描那些要注入到spring容器的組件的包路徑blog

package com.annotation.component; import org.springframework.stereotype.Controller;
import org.springframework.context.annotation.Configuration; @Controller
public class PersonController { } package com.annotation.register; import org.springframework.context.annotation.ComponentScan; //代表這是個註解配置類 @Configuration @ComponentScan(value={"com.annotation.component"}) public class Config { } package com.annotation.register; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.annotation.component.controller.PersonController; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class); PersonController personController = annotationConfigApplicationContext.getBean(PersonController.class); } }

 @bean :主要做用在方法上,name能夠指定bean在容器中的名稱,不指定的話默認是方法名;initMethod指定bean的初始化方法;destroyMethod指定bean的銷燬方法get

package com.annotation.component; public class Blue { } package com.annotation.register; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.annotation.component.bean.Blue; @Configuration public class MainConfig { @Bean(name="blue") public Blue getColor(){ return new Blue(); } } package com.annotation.register; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.annotation.component.bean.Blue; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Blue blue= annotationConfigApplicationContext.getBean(Blue.class); System.out.println(blue); } }

 @Import   :it

  • 注入一個bean
  • 導入ImportSelector的實現類,selectImports方法裏面配置要注入的bean
  • 導入ImportBeanDefinitionRegistrar的實現類,registerBeanDefinitions方法裏面配置要注入的bean

            

package com.annotation.component.bean; public class Blue { } package com.annotation.component.bean; public class Yellow { } package com.annotation.component.bean; public class Green{ } package com.annotation.importbean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import com.annotation.component.bean.White; @Configuration @Import(value = { White.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class }) public class MainConfig { } package com.annotation.importbean; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; public class MyImportSelector implements ImportSelector{ @Override public String[] selectImports(AnnotationMetadata annotationmetadata) { return new String[]{"com.annotation.component.bean.Yellow"}; } } package com.annotation.importbean; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; import com.annotation.component.bean.Green; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{ @Override public void registerBeanDefinitions(AnnotationMetadata annotationmetadata, BeanDefinitionRegistry beandefinitionregistry) { RootBeanDefinition beanDefinition = new RootBeanDefinition(Green.class); beandefinitionregistry.registerBeanDefinition("com.annotation.component.bean.Green", beanDefinition); } } package com.annotation.importbean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class); String[] names = annotationConfigApplicationContext.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } annotationConfigApplicationContext.close(); } }

 implements FactoryBeanio

  • getObject 方法表示要注入的對象,getObjectType表示對象類型,isSingleton表示是否單例
  • annotationConfigApplicationContext.getBean("colorFactoryBean") 表示獲取的是工程裏面注入的對象,即Color
  • annotationConfigApplicationContext.getBean("&colorFactoryBean") 表示獲取的是工程對象自己,即ColorFactoryBean
package com.annotation.component.bean; public class Color { } package com.annotation.factorybean; import org.springframework.beans.factory.FactoryBean; import com.annotation.component.bean.Color; public class ColorFactoryBean implements FactoryBean<Color>{ @Override public Color getObject() throws Exception { return new Color(); } @Override public Class<?> getObjectType() { return Color.class; } @Override public boolean isSingleton() { return false; } } package com.annotation.factorybean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MainConfig { @Bean public ColorFactoryBean colorFactoryBean(){ return new ColorFactoryBean(); } } package com.annotation.factorybean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Object obj1 = annotationConfigApplicationContext.getBean("colorFactoryBean"); Object obj2 = annotationConfigApplicationContext.getBean("colorFactoryBean");
System.out.println(obj1
==obj2); //獲取factorybean 自己 Object obj3 = annotationConfigApplicationContext.getBean("&colorFactoryBean"); System.out.println(obj3.getClass()); annotationConfigApplicationContext.close(); } }
相關文章
相關標籤/搜索