本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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);
}
示例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"));
}
示例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"));
}
示例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
}
}
示例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
}
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}