Bean对象创建的三种方式:1.通过构造方法进行<bean id: class>定义; 2.通过工厂对象调用普通方法的创建对象;3.通过工厂对象调用静态方法的创建对象;
手动创建Bean对象: 前提是该类已注入Spring容器,简单代码如下: @Component public class BeanTest{ ? ? @Autowired ? ? private ApplicationContext applicationContext ; ? ? public void test1() throws Exception{ ? ? ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext ; ? ? ConfigurableListableBeanFactory beanFactory = configurableApplicationContext.getBeanFactory(); ? ? ConcurrentHashMap<String,Channel> channelsMap = new ConCurrentHashMap(); ? ? beanFactory.registerSingleton("channelsMap",channelsMap) ; ? ? } ? ? public void test2() throws Exception{ ? ? ConcurrentHashMap channelsMap = applicationContext.getBean("ChannelsMap",ConcurrentHashMap.class); ? ? } }
|