SpringBoot装载bean的几种方式

一、直接装配

package com.lh.bean;

/** 
* @ClassName: MyBean 
* @Description: 简单直接方式注入bean
* @author Liu 
* @date 2018年5月26日 下午6:21:44 
*/
public class MyBean {

}


    /***
    * @Title: createMyBean 
    * @Description: 1、返回bean名称默认为方法名
    *                 2、同时也可以手动指定bean名称
    * @param @return  
    * @return MyBean
    * @throws
     */
    @Bean(name = "myBean")
    @Scope("prototype")//指定为原型方式,即多例
    public MyBean createMyBean() {
        return new MyBean();
    }

二、FactoryBean工厂方式

package com.lh.bean2;

import org.springframework.beans.factory.FactoryBean;

/** 
* @ClassName: RunnableFactoryBean 
* @Description: TODO
* @author Liu 
* @date 2018年5月26日 下午6:44:07 
*/
public class JeepFactoryBean implements FactoryBean<Jeep> {

    @Override
    public Jeep getObject() throws Exception {
        return new Jeep();
    }

    @Override
    public Class<?> getObjectType() {
        return Jeep.class;
    }

    @Override
    public boolean isSingleton() {
//        return true;
        return false;
    }

}


    @Bean
    public JeepFactoryBean createJeepFactoryBean() {
        return new JeepFactoryBean();
    }

注:获取FactoryBean本身的方式需要在bean名称前加"&"或直接以类型获取:

        //获取FactoryBean本身
        System.out.println(ctx.getBean(JeepFactoryBean.class));//class方式
        System.out.println(ctx.getBean("&createJeepFactoryBean"));//name方式

三、普通工厂模式方式

package com.lh.bean3;

/** 
* @ClassName: JeepFactory 
* @Description: TODO
* @author Liu 
* @date 2018年5月26日 下午9:59:06 
*/
public class CarFactory {
    public Car create() {
        return new Car();
    }
}


package com.lh.bean3;

/** 
* @ClassName: Car 
* @Description: 自定义工厂类创建实体,实体创建依赖之前定义的工厂类
* @author Liu 
* @date 2018年5月26日 下午10:04:06 
*/
public class Car {

}


    @Bean
    public CarFactory createCarFactory() {
        return new CarFactory();
    }

    @Bean
    public Car createCar(CarFactory carFactory) {
        return carFactory.create();
    }

四、注解方式

@Controller/@Service/@Repository/@Component

五、@Import方式

package com.lh;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.lh.bean.EnableLog;
import com.lh.bean.Role;
import com.lh.bean.User;
import com.lh.exercise.EnableEcho;

/***
 * 
* @ClassName: App 
* @Description: @Import用来导入一个或多个普通类(bean会被spring容器托管),或者配置类(配置类中的bean都会被spring容器托管)
* @author Liu 
* @date 2018年6月10日 下午6:21:28 
*
 */

@ComponentScan
@Import({User.class,Role.class,MyConfiguration.class})
public class App2 
{
    public static void main( String[] args )
    {
        ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
        
        System.out.println(context.getBean(User.class));
        System.out.println(context.getBean(Role.class));
        System.out.println(context.getBeansOfType(Runnable.class));
        
        context.close();
    }
}


package com.lh.bean;

/** 
* @ClassName: Role 
* @Description: TODO
* @author Liu 
* @date 2018年6月10日 下午6:58:55 
*/
public class Role {

}


package com.lh.bean;

/** 
* @ClassName: User 
* @Description: TODO
* @author Liu 
* @date 2018年6月10日 下午6:58:48 
*/
public class User {

}


package com.lh.bean;

import org.springframework.context.annotation.Bean;

/** 
* @ClassName: MyConfiguration 
* @Description: TODO
* @author Liu 
* @date 2018年6月10日 下午7:05:38 
*/
public class MyConfiguration {
    @Bean
    public Runnable createRunnable() {
        return () -> {};
    }
    
    @Bean
    public Runnable createRunnable2() {
        return () -> {};
    }
}

注:

1、@Import用来导入一个或多个普通类(bean会被spring容器托管),或者配置类(配置类中的bean都会被spring容器托管

2、不要在类上加任何注解,否则@Import注解失去意义!

六、通过扫描指定包装载bean

package com.lh;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.lh.bean3.Car;
import com.lh.bean4.Cat;
import com.lh.bean5.Dog;
import com.lh.bean6.Animal;
import com.lh.bean7.User;

/**
 * @ClassName: AnnotationClient
 * @Description: TODO
 * @author Liu
 * @date 2018年5月28日 下午10:42:55
 */
public class AnnotationClient {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.lh");
        System.out.println(ctx.getBean("&createJeepFactoryBean"));// name方式

        System.out.println(ctx.getBean(Car.class));

        System.out.println(ctx.getBean(Cat.class));

        System.out.println(ctx.getBean(Dog.class));

        System.out.println(ctx.getBean(Animal.class));
        
        System.out.println(ctx.getBean("myUser"));
        ctx.close();
    }

}

七、指定哪些bean可以装配

package com.lh;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.lh.bean10.UserController;
import com.lh.bean3.Car;
import com.lh.bean4.Cat;
import com.lh.bean5.Dog;
import com.lh.bean6.Animal;

/**
 * @ClassName: AnnotationClient
 * @Description: TODO
 * @author Liu
 * @date 2018年5月28日 下午10:42:55
 */
public class AnnotationClient2 {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationScan.class);
        System.out.println(ctx.getBean("&createJeepFactoryBean"));// name方式

        System.out.println(ctx.getBean(Car.class));

        System.out.println(ctx.getBean(Cat.class));

        System.out.println(ctx.getBean(UserController.class));
        
        System.out.println(ctx.getBean(Dog.class));
        

        System.out.println(ctx.getBean(Animal.class));
        ctx.close();
    }

}


package com.lh;

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

import com.lh.bean10.UserController;

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

/** 
* @ClassName: AnnotationScan 
* @Description: TODO
* @author Liu 
* @date 2018年5月28日 下午10:47:35 
*/
@ComponentScan(basePackages = "com.lh", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {DogConfig.class,UserController.class}))
@Configuration
public class AnnotationScan {

}


package com.lh;

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

import com.lh.bean5.Dog;

/** 
* @ClassName: DogConfig 
* @Description: TODO
* @author Liu 
* @date 2018年5月28日 下午10:53:48 
*/
@Configuration
public class DogConfig {
    @Bean(initMethod = "init",destroyMethod="destroy")
    public Dog createDog() {
        return new Dog();
    }
}


package com.lh.bean10;

import org.springframework.stereotype.Controller;

/** 
* @ClassName: UserController 
* @Description: TODO
* @author Liu 
* @date 2018年5月28日 下午9:46:28 
*/
@Controller//一般用于展现层
public class UserController {

}

评论

暂无评论