Skip to content

Instantly share code, notes, and snippets.

@joshlong
Created April 24, 2012 11:18
Show Gist options
  • Save joshlong/2478860 to your computer and use it in GitHub Desktop.
Save joshlong/2478860 to your computer and use it in GitHub Desktop.
An Example demonstrating prototype injection. Each time the 'BeanThatDependsOnAnother' is used, it invokes a method on the 'Dependency' class, triggering the creation of a new bean, thanks to Spring's smart use of proxies
package org.springsource.examples.sawt.proxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
public class Main {
@Configuration
@EnableAspectJAutoProxy
static class MyConfigurationClass {
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
@Bean
public Dependency client() {
return new Dependency();
}
@Bean
public BeanThatDependsOnAnother service() {
return new BeanThatDependsOnAnother();
}
}
static class Dependency {
public Dependency() {
System.out.println(getClass().getName());
}
public void sayHello() {
System.out.println("Hello");
}
}
static class BeanThatDependsOnAnother {
@Autowired
private Dependency dependency;
void doSomething() {
dependency.sayHello();
}
}
public static void main(String[] args) throws Throwable {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfigurationClass.class);
BeanThatDependsOnAnother s = ac.getBean(BeanThatDependsOnAnother.class);
s.doSomething();
s.doSomething();
}
}
/**
// OUTPUT
2012-04-24 14:13:10 ClassPathBeanDefinitionScanner [INFO] JSR-330 'javax.inject.Named' annotation found and supported for component scanning
2012-04-24 14:13:10 AnnotationConfigApplicationContext [INFO] Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@48f0c0d3: startup date [Tue Apr 24 14:13:10 EEST 2012]; root of context hierarchy
2012-04-24 14:13:10 AutowiredAnnotationBeanPostProcessor [INFO] JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2012-04-24 14:13:10 DefaultListableBeanFactory [INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@48ff2413: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,myBean.MyConfigurationClass,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,org.springframework.aop.config.internalAutoProxyCreator,scopedTarget.client,client,service]; root of factory hierarchy
org.springsource.examples.sawt.proxy.MyBean$Dependency$$EnhancerByCGLIB$$d2d7bbf6
org.springsource.examples.sawt.proxy.MyBean$Dependency
Hello
org.springsource.examples.sawt.proxy.MyBean$Dependency
Hello
Process finished with exit code 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment