스프링 ApplicationScope 빈주입

728x90

  • Bean을 정의 할 때 application scope 로 정의하면 서버가 가동될 때 자동으로 주입된다.
  • 주입된 Bean은 주입만 이루어지는 것이므로 application 영역에 저장되지는 않는다.
  • 서버가 가동 될 때 자동 주입 되는 것이므로 @Lazy를 설정하지 않아도 된다.

Application Scope

  • java 방식은 @ApplicationScope 를 사용
  • xml 방식은 bean을 정의 할 때 scope="application"으로 설정
@Configuration
public class RootAppContext{
	
	@Bean //@Bean("application2")
	@ApplicationScope 
	
	public MemberDTO member() {
		return new MemberDTO();
	}	
}
@Controller
public class TestController{
	
	@Autowired //정의된 Bean중에서 MemberDTO로 정의가 된 것이 있는지 확인.
	MemberDTO applicationMember;
	@Resource(name = "application2")
	MemberDTO applicationMember2; 
	
	@GetMapping("/test")
	public String test(){
		applicationMember.setName("홍길동");
		applicationMember.setId("hong");

		return "forward:test1"; //forward 사용에는 현재 주입되는 시기가 새로운 요청이 발생했을 경우여서
		//forward는 새로운 요청이 아니기에 객체가 유지된다.
	}
}

서버가 가동 될 때 만 주입되기 때문에 이름을 지정해도 model 를 사용해야만 한다.

	@GetMapping("/test1")
	public String test1(Model model){
		model.addAttribute("applicationMember",applicationMember);
		//모델로 받아서 request 영역에 저장해야만 출력이가능
		
		return ""; 
	}

xml 방식은

root-context.xml 에 주입한다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean class="com.tests.test.members.MemberDTO" scope='application'/> <!--패키지명-->
	<bean class="com.tests.test.members.MemberDTO" scope='application' id="applcation2"/>
</beans>

xml 방식으로 하면 id를 사용한 것만 유일하게 application에 자동으로 저장이 된다.

@Component //@Component(value = "applicationMember") 이름으로 주입된다. 
@ApplicationScope
public class MemberDTO{
	private String name;
	private String id;
	//getter, setter 생략
}

config 패키지에 ServletAppContext 클래스

//Spring MVC 프로젝트에 관련된 설정하는 클래스
@Configuration
//Controller 어노테이션이 셋팅되어 있는 클래스를 Controller로 등록한다.
@EnableWebMvc
//스캔할 패키지를 정한다.
@ComponentScan("com.tests.test.controller")
@ComponentScan("com.test.test.members") // 빈 캐피지를 스캔한다.
public class ServletAppContext implements WebMvcConfigurer{
	//Controller의 메서드가 반환하는 .jsp의 이름앞뒤에 경로와 확장자를 붙여주도록 설정한다.
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		WebMvcConfigurer.super.configureViewResolvers(registry);
		registry.jsp("WEB-INF/views/", ".jsp");
	}
} 

단순 주입만 이루어지기 때문에 request 영역에 저장되지 않는다.

 

xml 방법으론

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- 스캔할 Bean들이 모여있는 패키지를 지정한다 -->
	<context:component-scan base-package="com.tests.test.controller"/>
	<context:component-scan base-package="com.tests.test.members"/>

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.tests.test" />
	
	
	
</beans:beans>
반응형

'Back-End > Spring(Boot)' 카테고리의 다른 글

스프링의 의존성 주입 - 기초  (0) 2021.05.30
스프링 Cookie  (0) 2021.03.06
스프링 Application Scope  (0) 2021.03.06
스프링 Session 빈 주입  (0) 2021.03.06
스프링 Session  (0) 2021.03.06