spring factoryBean

Monkey

1
2
3
package com.melon.app.bean;
public class Monkey {
}

MelonFactoryBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.melon.app.config;

import com.melon.app.bean.Monkey;
import org.springframework.beans.factory.FactoryBean;

public class MelonFactoryBean implements FactoryBean<Monkey> {


public Monkey getObject() throws Exception {
return new Monkey();
}

public Class<?> getObjectType() {
return Monkey.class;
}

public boolean isSingleton() {
return false;
}
}

@Configuration
@Import(value = {MelonImportSelector.class,MelonImportBeanDefinitionRegistrar.class})
public class MyImportConfigureation {

@Bean
public MelonFactoryBean melonFactoryBean(){
    return  new MelonFactoryBean();
}

}

1
2
3
4
5
````
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyImportConfigureation.class);
String[] str= applicationContext.getBeanDefinitionNames();
Object bean1 = applicationContext.getBean("melonFactoryBean");
System.out.println(bean1.getClass());

结果如下

class com.melon.app.bean.Monkey
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
myImportConfigureation
com.melon.app.bean.Cat
com.melon.app.bean.Dog
melonFactoryBean
pig