当前位置: 首页>>代码示例>>Java>>正文


Java StaticWebApplicationContext.getBean方法代码示例

本文整理汇总了Java中org.springframework.web.context.support.StaticWebApplicationContext.getBean方法的典型用法代码示例。如果您正苦于以下问题:Java StaticWebApplicationContext.getBean方法的具体用法?Java StaticWebApplicationContext.getBean怎么用?Java StaticWebApplicationContext.getBean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.web.context.support.StaticWebApplicationContext的用法示例。


在下文中一共展示了StaticWebApplicationContext.getBean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDelegatingFilterProxyWithTargetBeanName

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:DelegatingFilterProxyTests.java

示例2: testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	// wac.refresh();
	// note that the context is not set as the ROOT attribute in the ServletContext!

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac);
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:DelegatingFilterProxyTests.java

示例3: testDelegatingFilterProxyWithFilterName

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:27,代码来源:DelegatingFilterProxyTests.java

示例4: testDelegatingFilterProxyWithFrameworkServletContext

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:27,代码来源:DelegatingFilterProxyTests.java

示例5: testDelegatingVariableResolver

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
public void testDelegatingVariableResolver() {
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("bean1", TestBean.class, null);
	wac.registerSingleton("var1", TestBean.class, null);
	wac.refresh();
	TestBean bean1 = (TestBean) wac.getBean("bean1");

	// We need to override the getWebApplicationContext method here:
	// FacesContext and ExternalContext are hard to mock.
	DelegatingVariableResolver resolver = new DelegatingVariableResolver(new OriginalVariableResolver()) {
		@Override
		protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
			return wac;
		}
	};
	assertEquals(bean1, resolver.resolveVariable(null, "bean1"));
	assertEquals("val1", resolver.resolveVariable(null, "var1"));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:19,代码来源:DelegatingVariableResolverTests.java

示例6: testSpringBeanVariableResolver

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
public void testSpringBeanVariableResolver() {
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("bean1", TestBean.class, null);
	wac.registerSingleton("var1", TestBean.class, null);
	wac.refresh();
	TestBean bean1 = (TestBean) wac.getBean("bean1");
	TestBean var1 = (TestBean) wac.getBean("var1");

	// We need to override the getWebApplicationContext method here:
	// FacesContext and ExternalContext are hard to mock.
	SpringBeanVariableResolver resolver = new SpringBeanVariableResolver(new OriginalVariableResolver()) {
		@Override
		protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
			return wac;
		}
	};
	assertEquals(bean1, resolver.resolveVariable(null, "bean1"));
	assertEquals(var1, resolver.resolveVariable(null, "var1"));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:20,代码来源:DelegatingVariableResolverTests.java

示例7: testPutBeanInRequest

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
@SuppressWarnings("resource")
public void testPutBeanInRequest() throws Exception {
	String targetBeanName = "target";

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setScope(WebApplicationContext.SCOPE_REQUEST);
	bd.getPropertyValues().add("name", "abc");
	wac.registerBeanDefinition(targetBeanName, bd);
	wac.refresh();

	HttpServletRequest request = new MockHttpServletRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
	TestBean target = (TestBean) wac.getBean(targetBeanName);
	assertEquals("abc", target.getName());
	assertSame(target, request.getAttribute(targetBeanName));

	TestBean target2 = (TestBean) wac.getBean(targetBeanName);
	assertEquals("abc", target2.getName());
	assertSame(target2, target);
	assertSame(target2, request.getAttribute(targetBeanName));

	request = new MockHttpServletRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
	TestBean target3 = (TestBean) wac.getBean(targetBeanName);
	assertEquals("abc", target3.getName());
	assertSame(target3, request.getAttribute(targetBeanName));
	assertNotSame(target3, target);

	RequestContextHolder.setRequestAttributes(null);
	try {
		wac.getBean(targetBeanName);
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:40,代码来源:RequestAndSessionScopedBeanTests.java

示例8: testPutBeanInSession

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
@SuppressWarnings("resource")
public void testPutBeanInSession() throws Exception {
	String targetBeanName = "target";
	HttpServletRequest request = new MockHttpServletRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setScope(WebApplicationContext.SCOPE_SESSION);
	bd.getPropertyValues().add("name", "abc");
	wac.registerBeanDefinition(targetBeanName, bd);
	wac.refresh();

	TestBean target = (TestBean) wac.getBean(targetBeanName);
	assertEquals("abc", target.getName());
	assertSame(target, request.getSession().getAttribute(targetBeanName));

	RequestContextHolder.setRequestAttributes(null);
	try {
		wac.getBean(targetBeanName);
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
	}


}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:30,代码来源:RequestAndSessionScopedBeanTests.java

示例9: testDelegatingFilterProxy

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxy() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:28,代码来源:DelegatingFilterProxyTests.java

示例10: testDelegatingFilterProxyAndCustomContextAttribute

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyAndCustomContextAttribute() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("CUSTOM_ATTR", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("contextAttribute", "CUSTOM_ATTR");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:29,代码来源:DelegatingFilterProxyTests.java

示例11: testDelegatingFilterProxyWithLazyContextStartup

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithLazyContextStartup() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:28,代码来源:DelegatingFilterProxyTests.java

示例12: testDelegatingFilterProxyWithTargetFilterLifecycle

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyWithTargetFilterLifecycle() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("targetFilterLifecycle", "true");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);
	assertEquals(proxyConfig, targetFilter.filterConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertEquals(proxyConfig, targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:30,代码来源:DelegatingFilterProxyTests.java

示例13: testDelegatingFilterProxyInjectedPreferred

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyInjectedPreferred() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	StaticWebApplicationContext injectedWac = new StaticWebApplicationContext();
	injectedWac.setServletContext(sc);
	String beanName = "targetFilter";
	injectedWac.registerSingleton(beanName, MockFilter.class);
	injectedWac.refresh();

	MockFilter targetFilter = (MockFilter) injectedWac.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName, injectedWac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:29,代码来源:DelegatingFilterProxyTests.java

示例14: testDelegatingFilterProxyNotInjectedWacServletAttrPreferred

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyNotInjectedWacServletAttrPreferred() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
	wacToUse.setServletContext(sc);
	String beanName = "targetFilter";
	String attrName = "customAttrName";
	wacToUse.registerSingleton(beanName, MockFilter.class);
	wacToUse.refresh();
	sc.setAttribute(attrName, wacToUse);

	MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
	filterProxy.setContextAttribute(attrName);
	filterProxy.setServletContext(sc);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:34,代码来源:DelegatingFilterProxyTests.java

示例15: testDelegatingFilterProxyNotInjectedWithRootPreferred

import org.springframework.web.context.support.StaticWebApplicationContext; //导入方法依赖的package包/类
@Test
public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
	sc.setAttribute("another", wac);

	StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
	wacToUse.setServletContext(sc);
	String beanName = "targetFilter";
	wacToUse.registerSingleton(beanName, MockFilter.class);
	wacToUse.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse);

	MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
	filterProxy.setServletContext(sc);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:32,代码来源:DelegatingFilterProxyTests.java


注:本文中的org.springframework.web.context.support.StaticWebApplicationContext.getBean方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。