spring learning (two)-advanced assembly

1. Annotation

  1. @Profile("") : Specify the environment through the profile annotation, and only beans with the same operating environment will be registered in the spring context
  2. @Primary : a plurality of spring context BeanID same time, the use of automatic assembly procedure causes abnormal injection method, using the specified class primary spring in a multi-injection injection which priority when the bean
  3. @Qualifier : Custom BeanId can be used with @Component and @Bean, and can be used with @Autowired to specify the name of the injected bean
  4. @Scope : A scope in the spring container, which has the following scopes in the Spring container: common scope singleton (singleton) , prototype (multiple instances)

Two, assemble Bean

Continue to the previous test demo and create test2

  •  Create interface
package com.test.spring.test2;

public interface Dog {
    String wangWangWang();
}
  • Create two implementation classes and register them in the spring container
package com.test.spring.test2;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Primary
@Component
public class DaLangGou implements Dog {
    @Override
    public String wangWangWang() {
        String str = "我是大狼狗";
        System.out.println(str);
        return str;
    }
}
package com.test.spring.test2;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;


@Component
public class XiaoLangGou implements Dog {

    @Override
    public String wangWangWang() {
        String str = "我是小狼狗~";
        System.out.println(str);
        return str;
    }
}
  • Create javaConfig
package com.test.spring.test2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class JavaConfig {
}
  • test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JavaConfig.class)
public class Test2 {

    @Autowired
    private Dog dog;

    @Test
    public void contextLoads() {
        dog.wangWangWang();
    }
}

 Since the DaLangGou class uses @Primary, it will be assembled first

Three, use custom BeanID method

  • Modify the implementation class
//@Primary
@Component
@Qualifier("daGou")
public class DaLangGou implements Dog {
    @Override
    public String wangWangWang() {
        String str = "我是大狼狗";
        System.out.println(str);
        return str;
    }
}
@Component
@Qualifier("xiaoGou")
public class XiaoLangGou implements Dog {

    @Override
    public String wangWangWang() {
        String str = "我是小狼狗~";
        System.out.println(str);
        return str;
    }
}
  • test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JavaConfig.class)
public class Test2 {

    @Autowired
    @Qualifier("xiaoGou")
    private Dog dog;

    @Test
    public void contextLoads() {
        dog.wangWangWang();
    }
}

Due to the use of a custom beanID method, the name of the BeanID needs to be specified during assembly

Fourth, set up the operating environment

  •  Modify the implementation class and set the operating environment
//@Primary
@Component
@Profile("pro")
//@Qualifier("daGou")
public class DaLangGou implements Dog {
    @Override
    public String wangWangWang() {
        String str = "我是大狼狗";
        System.out.println(str);
        return str;
    }
}
@Component
@Profile("dev")
//@Qualifier("xiaoGou")
public class XiaoLangGou implements Dog {

    @Override
    public String wangWangWang() {
        String str = "我是小狼狗~";
        System.out.println(str);
        return str;
    }
}
  • test
/**
 * @ActiveProfiles
 * 指定使用的那个环境来创建,只有pro或没有指定环境的bean会被创建
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JavaConfig.class)
@ActiveProfiles("pro")
public class Test2 {

    @Autowired
    //@Qualifier("daGou")
    private Dog dog;

    @Test
    public void contextLoads() {
        dog.wangWangWang();
    }
}

Five, read the configuration file

  • application.yml

  • test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JavaConfig.class)
@PropertySource("classpath:application.yml")
public class Test2 {

    /**
     * 读取配置文件注解方式和使用Environment方式
     */
    @Autowired
    private Environment env;

    @Value("${kkkk}")
    private String str;

    @Test
    public void testEnv(){
        System.out.println(env.getProperty("kkkk"));
        System.out.println(str);
    }
}

Guess you like

Origin blog.csdn.net/qq_42407917/article/details/94428032