Annotation-based Spring

First, the comment form is injected Bean IOC container

  1.1, you must have the configuration class, such as:

@Configuration
public class MyConfig {

    @Bean    //即id="myPig"
    public Pig myPig() {
        Pig pig = new Pig ( "Page", 4 "Husky");
        return pig;
    }
}

  1.2, in the form of a non-divided into three components and three components

  Annotations three components: the control layer: @Controller

          Service Layer: @Service

          Dao layer: @Respository three are available: @Component

    Where scanning annotation packet to the IOC container: a mode profile:. <Context: component-scan base-package = "XXX">

                    . B Notes ways:

@Configuration
@ComponentScan(value="service")
public class MyConfig {

    @Bean    //即id="myPig"
    public Pig myPig() {
        Pig Pig = new new Pig ( "Page", 4 "Husky" );
         return Pig;
    }
}

    Notes ways various parameters, see the source code, in this column a demo

// scan controller of this package, but ruled out this package comment about @Service of 
@ComponentScan (value = "controller", excludeFilters = {@ ComponentScan.Filter (of the type = FilterType.ANNOTATION, value = Service. Class )}) 
// scan only StudentService this class, attention must turn off the default scan to take effect
@ComponentScan (@ ComponentScan.Filter (type = FilterType.ASSIGNABLE_TYPE , value = {StudentService.class})}, useDefaultFilters = false)

   Non-Layer assembly (e.g. Converter Converter):

    1, may also be used @ Bean + return value, id defaults to the method name ;

      2, @ Import mode

    3、FactoryBean

  The first approach has been demonstrated at the beginning, and therefore start from @Import:

@Configuration
@Import ({Course,. Class , CollectionType. Class })       // ID is fully qualified class name
public class MyConfig {
     ...         
}

  @Import source also mentioned two other implementations: {@ link ImportSelector}, {@ link ImportBeanDefinitionRegistrar}

  FactoryBean:

public class MyFactoryBean implements FactoryBean<Teacher>{

    @Override
    public Teacher getObject() throws Exception {
        // TODO Auto-generated method stub
        return new Teacher();   //放入对象
    }

    @Override
    public Class <Teacher> getObjectType () {
         // the TODO Auto-Generated Method Stub 
        return . Teacher class ;     // Add Object Type 
    }

    @Override
    public  Boolean isSingleton () {
         // the TODO Auto-Generated Method Stub 
        return  to true ;    // determined singleton 
    }

}



@Configuration
public class MyConfig {
    @Bean    //id=方法名
    public FactoryBean<Teacher> myFactoryBean(){
        return new MyFactoryBean();
    }
}

  Get Bean by this method:  

    @Test
    public void factory() {
        Object bean1 = context.getBean("&myFactoryBean");      //得到MyFactoryBean
        Object bean2 = context.getBean("myFactoryBean");    //得到Teacher
        System.out.println(bean1+"\n"+bean2);
    }

config.MyFactoryBean@1a052a00
entity.Teacher@4d826d77

 

Second, the notes provided in the form of injection conditions Bean

  2.1, Creating Condition class

public class CourseCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment Environment = Context.getEnvironment ();     // Get Environment 
        String pigType = environment.getProperty ( "pig.type" );
         IF (pigType.contains ( "Old")) {      // the proviso that the implementation class pig 
            return  to true ;
        }
        return  to false ;                // to true Bean added to the IOC container, whereas the container without the addition of IOC
    }

}

  2.2, create pig interface and implementation class

public interface Pig {

    void syso();
}

public class OldPig implements Pig{...}

  2.3, an increase in the configuration class annotation @Conditional

@Configuration
public class MyConfig {
    @Bean("oldPig")    //即id="myPig"
    @Conditional(CourseCondition.class)
    public OldPig myPig() {
        Pig OldPig = new new OldPig ( "Page", 4 "Husky" );
         return Pig;
    }
}

  Finally, different injection can be achieved by changing the environment configuration Bean

Guess you like

Origin www.cnblogs.com/709539062rao/p/12616844.html
Recommended