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


spring 中常用的組件標簽有:

@Controller:控制層

@Service:業務層

@Repository:數據層

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

 

組件注冊方式:

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

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的銷毀方法

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   :

  • 注入一個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 FactoryBean

  • 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(); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM