XmlBeanFactory factory = new XmlBeanFactory(
new FileSystemResource("spring.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("dev.properties"));
cfg.postProcessBeanFactory(factory);
public class DemoBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("PostBeanFactory");
}
}
public class DemoBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("PostBeforeInit: " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("PostAfterInit: " + beanName);
return bean;
}
}
<context:component-scan base-package="edu"/>
new AnnotationConfigApplicationContext("com");
@Configuration
@ComponentScan("edu")
public class JavaConfig {
@Bean
public AnnotatedMessage annotatedMessage(){
AnnotatedMessage result = new AnnotatedMessage();
result.setText("Demo");
return result;
}
@Bean(initMethod = "method")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public Message message(){
Message result = new Message();
result.setText("Demo1");
result.setAnnotatedMessage(annotatedMessage());
return result;
}
}
new AnnotationConfigApplicationContext(AppConfig.class)
import my.company.MyBeanImpl
beans = {
myBean(MyBeanImpl) {
someProperty = 42
otherProperty = "blue"
}
}
new GenericGroovyApplicationContext("context.groovy");
public interface Scope {
Object get(String name, ObjectFactory objectFactory);
Object remove(String name);
/** Register a callback to be executed on destruction of the specified
* object in the scope (or at destruction of the entire scope, if the
* scope does not destroy individual objects but rather only terminates
* in its entirety).**/
void registerDestructionCallback(String name, Runnable callback);
Object resolveContextualObject(String key);
String getConversationId();
}
public class SimpleThreadScope implements Scope {
private final ThreadLocal<Map<String, Object>> threadScope = new NamedThreadLocal<Map<String, Object>>("SimpleThreadScope");
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> scope = this.threadScope.get();
Object object = scope.get(name);
if (object == null) {
object = objectFactory.getObject();
scope.put(name, object);
}
return object;
}
@Override
public Object remove(String name) {
Map<String, Object> scope = this.threadScope.get();
return scope.remove(name);
}
}
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
Message message = null;
long before = System.nanoTime();
for(int i = 0; i < 100*100*100; i++){
message= (Message)applicationContext.getBean("message");
System.out.println( message);
}
long after = System.nanoTime();
System.out.println((after-before)/1000000);