Spring Boot学习笔记(二)

javaConfig

删除ApplicationContext.xml,配置Config.java类
package com.fan.springboot;

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

@Configuration
public class Config {

    @Bean
    public SomeBean someBean(){
        return new SomeBean();
    }
}
修改测试类代码,测试:
import com.fan.springboot.Config;
import com.fan.springboot.SomeBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SomeBeanTest {
    @Test
    public void test(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
        SomeBean sb = ctx.getBean(SomeBean.class);//根据Bean的类型(SomeBean)找
        System.out.println(sb);
    }
}
输出:

com.fan.springboot.SomeBean@491b9b8

你可能感兴趣的:(Spring,Boot,java)