Close

Spring Framework - @Scope Examples

Spring Framework 



    @Bean
@Scope(WebApplicationContext.SCOPE_SESSION)
public Visitor visitor(HttpServletRequest request){
return new Visitor(request.getRemoteAddr());
}
Original Post




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MyPrototypeBean {

@Autowired
@Qualifier("basic-service")
private MyService myService;

public void doSomething(){
System.out.println(myService.getMessage());
}
}
Original Post




@Configuration
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean() {
return new MySingletonBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post




@Configuration
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean() {
return new MySingletonBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post
@Configuration
@ComponentScan(basePackageClasses = MySingletonBean.class)
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);

MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();

Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post




import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean() {
return new MySingletonBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);

MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post




@Configuration
public class AppConfig {

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyPrototypeBean prototypeBean () {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean () {
return new MySingletonBean();
}

public static void main (String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean.showMessage();
}
}
Original Post
@Configuration
public class AppConfig {

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.INTERFACES)
public IPrototype prototypeBean () {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean () {
return new MySingletonBean();
}

public static void main (String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean.showMessage();
}
}
Original Post




import com.logicbig.example.bean.UserRegistrationBean;
import com.logicbig.example.bean.UserRegistrationBeanImpl;
import com.logicbig.example.bean.UserRegistrationValidator;
import com.logicbig.example.service.RegistrationService;
import com.logicbig.example.service.RegistrationServiceImpl;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
public class Config {

@Bean
public UserRegistrationValidator validator () {
return new UserRegistrationValidator();
}

@Bean
public RegistrationService registrationService () {
return new RegistrationServiceImpl();
}

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean
public UserRegistrationBean userRegistrationBean () {
return new UserRegistrationBeanImpl();
}
}
Original Post




Request scope Example:

package com.logicbig.example;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import java.io.Serializable;

@Component
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class EmployeeDetails implements Serializable {
private Employee employee;
private int lastYearSalary;

public void setEmployee(Employee employee) {
this.employee = employee;
}

public Employee getEmployee() {
return employee;
}

public int getLastYearSalary() {
return lastYearSalary;
}

public void setLastYearSalary(int lastYearSalary) {
this.lastYearSalary = lastYearSalary;
}
}
Original Post

Session scope Example:

package com.logicbig.example;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import java.io.Serializable;
import java.time.LocalDateTime;

@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class VisitorInfo implements Serializable {
private String name;
private int visitCounter;
private LocalDateTime firstVisitTime;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getVisitCounter() {
return visitCounter;
}

public void setVisitCounter(int visitCounter) {
this.visitCounter = visitCounter;
}

public LocalDateTime getFirstVisitTime() {
return firstVisitTime;
}

public void setFirstVisitTime(LocalDateTime firstVisitTime) {
this.firstVisitTime = firstVisitTime;
}

public void increaseVisitorCounter() {
visitCounter++;
}
}
Original Post




Application scope Example:

package com.logicbig.example.app1;

import com.logicbig.example.AppLevelPreference;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@EnableWebMvc
@Configuration
public class MyWebConfig {

@Bean
public MyController myController() {
return new MyController();
}

@Bean
@Scope(value = WebApplicationContext.SCOPE_APPLICATION)
public AppLevelPreference pref() {
return new AppLevelPreference();
}

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Original Post




@Configuration
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

@Bean
public MySingletonBean singletonBean() {
return new MySingletonBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post




See Also