美文网首页
spring全注解开发

spring全注解开发

作者: simplerandom | 来源:发表于2020-07-23 16:48 被阅读0次

指定该类为配置类

@ComponentScan(value ="org.spring" )
@Configuration
public class MainConfig {
    
}

加载配置

public class Application {
    public static void main(String[] args) {
       ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Hello hello = context.getBean("hello", Hello.class);
        hello.go.go();
    }
}

主配置

@ComponentScan(value ="org.spring" )
@Configuration
@Import(OtherConfig.class)
@PropertySource("classpath:config.properties")
public class MainConfig {
    @Value("${age}")
    int age;
@Bean(name = "hello")
@Scope(value = "prototype")
    public HelloFactory hello(){
        return new HelloFactory(age);
    }
}

其他配置

public class OtherConfig {
    @Bean(name="f")
    public HelloFactory helloFactory()
    {return new HelloFactory();}
}

相关文章

网友评论

      本文标题:spring全注解开发

      本文链接:https://www.haomeiwen.com/subject/fuphlktx.html