1

I am using Spring boot. I have some question regarding the spring boot beans.

But I have doubt

I use bean which are default scope that is singleton. So they will have only one instance per application.

@Configuration
public class ...{

     @Bean
     public void method() {}
}

And

Now i use bean which scope is prototype. So they will have each instance per request.

@Configuration
public class ...{

     @Bean 
     @Scope("prototype")
     public void method() {}
}

But

I want single instance per user..? all request use single instance per user.

2 Answers2

2
@Configuration
class Abc {
 @Bean
 @Scope("session")
 public YourBean getYourBean() {
 return new YourBean();
}
}
Ravenloup
  • 46
  • 6
  • Check this link it has better explanation for session scope https://stackoverflow.com/questions/39488124/how-to-use-session-scoped-component-in-controller – Ravenloup Sep 03 '18 at 11:53
  • what is the error you are getting please post error log if possible – Ravenloup Sep 03 '18 at 12:04
  • Error : org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'attendantServiceImpl' defined in file [C:\kailas\workspace\LAPM010918\lapm\target\classes\com\avs\lapm\service\impl\AttendantServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 5; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consumer': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; – Kailas Biradar Sep 03 '18 at 12:14
  • How to use session scope and implement..? – Kailas Biradar Sep 04 '18 at 06:21
  • I just added one basic Session Scoped project to my git repo, please have a look and let me know if its helpful https://github.com/sonuy221991/SessionScope – Ravenloup Sep 04 '18 at 06:53
  • can use scope annotation for configuration class not component class – Kailas Biradar Sep 05 '18 at 05:55
  • Yes you can use but I don't think that's necessary for configurations – Ravenloup Sep 05 '18 at 07:04
  • if i use scope annotation to configuration class then how to work methods of that configuration class. – Kailas Biradar Sep 05 '18 at 07:23
  • When you define bean scope you need not to do anything else, spring takes care of everything for you, you just Autowire and use as a normal bean.By default spring beans are singleton, mean to say when you are not specifying any scope type you are using singleton and how you use them is by simple autowiring. so just define the type of your bean[session, prototype, request.....] autowire and use like a normal bean. – Ravenloup Sep 05 '18 at 16:10
0

You will need to define one singleton bean with a property using prototype bean:(xml example)

enter image description here

With @bean definition:

@Component
@Scope("singleton")
public class SingletonBean {

   // ..

     @Autowired
     private PrototypeBean prototypeBean;
   //..

}



@Component
@Scope("prototype")
public class PrototypeBean {
 //.......
}

Example: https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

CRISTIAN ROMERO MATESANZ
  • 1,502
  • 3
  • 14
  • 26
  • can i use scope annotation to class as well as methods of that class – Kailas Biradar Sep 05 '18 at 07:24
  • You could use '@Component', '@Controller', '@Service' to delegate into Spring to create your instance. If you do not declare nothings remember that this bean will be create as singleton. If not you could define it: '@Component' @Scope("prototype") Scope prototype means that every time you ask spring (getBean or dependency injection) for an instance it will create a new instance and give a reference to that. – CRISTIAN ROMERO MATESANZ Sep 05 '18 at 11:57
  • @KailasBiradar I have just updated response with bean scope definition – CRISTIAN ROMERO MATESANZ Sep 05 '18 at 12:01