@Import()

使用@Import()注解,向IOC容器中注入Bean

1.创建Bean类 MyBean

public class MyBean {
	//Bean类
}

2.这里在Application类中,SpringBoot项目的入口类中向IOC容器中注入Bean

@SpringBootApplication
@Import({Mybean.class})
public class SpringbootTestApplication implements CommandLineRunner {

    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        Mybean mybean = application.getBean(Mybean.class);
        System.out.println("mybean--"+mybean);
    }
    @Override
    public void run(String... args) throws Exception {

    }
}

@Bean

1.在Application类中,使用@Bean注解

@SpringBootApplication
public class SpringbootTestApplication implements CommandLineRunner {

	@Bean
    public Mybean mybean(){
        return new Mybean();
    }

    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        Mybean mybean = application.getBean(Mybean.class);
        System.out.println("mybean--"+mybean);
    }
    @Override
    public void run(String... args) throws Exception {

    }
}

@Configuration

1.通过使用配置类的形式,向IOC中注入,是以动态代理的形式,生成Bean对象

@Configuration
public class Mybean {

}

2.在application类中,直接从容器中获取bean对象

@SpringBootApplication
public class SpringbootTestApplication implements CommandLineRunner {
    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        Mybean mybean = application.getBean(Mybean.class);
        System.out.println("mybean--"+mybean);  
    }
    @Override
    public void run(String... args) throws Exception {

    }
}

3.通过查看运行结果 mybean--xjr.example.bean.Mybean$$EnhancerBySpringCGLIB$$aee90cd7@79c3f01f

@Configuration + @Bean

1.通过配置类中使用@Bean 向IOC容器中注入Bean

@Configuration
public class Mybean {
    @Bean
    public YouBean youBean(){
        return new YouBean();
    }
}

2.同样在Application类中,直接从容器中拿取Bean对象

@SpringBootApplication
public class SpringbootTestApplication implements CommandLineRunner {

    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        Mybean mybean = application.getBean(Mybean.class);
        YouBean youBean = application.getBean(YouBean.class);
        System.out.println("youbean--"+youBean);
        System.out.println("mybean--"+mybean);
    }
    @Override
    public void run(String... args) throws Exception {

    }
}

@ComponentScan()

1.通过扫描的方式,向IOC中注入bean对象

@Component
public class Mybean {

}
@Component
public class YouBean {

}

2.在Application类中,从容器中拿取Bean对象

@SpringBootApplication
@ComponentScan("xjr.example.bean")
public class SpringbootTestApplication implements CommandLineRunner {
    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        Mybean mybean = application.getBean(Mybean.class);
        YouBean youBean = application.getBean(YouBean.class);
        System.out.println("youbean--"+youBean);
        System.out.println("mybean--"+mybean);
    }
    @Override
    public void run(String... args) throws Exception {

    }
}

@ImportResource()

1.由于SpringBoot项目完全取消了ApplicationContext.xml配置文件,对于Spring项目,若由SpringBoot整合,可以使用@ImportResource()注解向SpringBoot项目中导入ApplicationContext.xml配置文件拿取Bean对象。
applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="hebean" class="he.xx.HeBean">
        <property name="name" value="xjr"/>
    </bean>
</beans>

2.在Application类中直接从IOC容器中拿取Bean对象,此处获取对应的属性值
Bean类

public class HeBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
@SpringBootApplication
@ImportResource(locations = {"classpath:applicationContext.xml"})
public class SpringbootTestApplication implements CommandLineRunner {
    public static void main(String[] args) {
        ApplicationContext application = SpringApplication.run(SpringbootTestApplication.class, args);
        HeBean heBean =  application.getBean(HeBean.class);
        System.out.println(heBean.getName());
    }
    @Override
    public void run(String... args) throws Exception {

    }
}