I am trying to check for bean scopes, a prototype injected within singleton bean. When i use @Lookup annotation it is throwing null pointer exception.
thows null pointer at singletonBean class...Actually the return null should be overrided by cglib and should return the prototype scoped bean right...
AppConfiguration.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import Bean.ProtoBean;
import Bean.SingletonBean;
@Configuration
public class AppConfiguration {
@Bean
@Scope("prototype")
public ProtoBean protoBean() {
return new ProtoBean();
}
@Bean
@Scope("singleton")
public SingletonBean singletonBean() {
return new SingletonBean();
}
}
SingletonBean.java
@Component
public class SingletonBean {
public SingletonBean() {
System.out.println("SingletonBean");
}
public ProtoBean fromProto() {
ProtoBean protoBean = getProtoBean();
//throws nullpointer here
System.out.println("beans is here: "+protoBean.hashCode());
return protoBean;
}
@Lookup
public ProtoBean getProtoBean() {
return null;
}
}
App.java
public class App
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfiguration.class);
SingletonBean sb = ctx.getBean(SingletonBean.class);
ProtoBean pb = sb.fromProto();
SingletonBean sb1 = ctx.getBean(SingletonBean.class);
ProtoBean pb1 = sb1.fromProto();
if(pb.equals(pb1)) {
System.out.println("Equal Proto");
} else {
System.out.println("Unequal Proto");
}
ctx.close();
}
}