欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-11 12:59:41
spring 中常用的组件标签有: @Controller:控制层 @Service:业务层 @Repository:数据层 @Component:普通的pojo注入到spring容器 组件注册方式: @ComponentScan 扫描那些要注入到spring容器的组件的包路径 @bean :主要作 ......

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