spring 组件基于注解的注册方式

2019-06-13 09:02:28来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

 


原文链接:https://www.cnblogs.com/chenzhubing/p/11010745.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:浅谈volatile关键字

下一篇:结合案例深入解析策略模式