我想WebApplicationContextUtils.getRequiredWebApplicationContext(context)在其init()方法中对依赖于Spring的servlet代码进行单元测试.
以下是代码的一部分:
@Override
public void init() throws ServletException {
super.init();
WebApplicationContext applicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.injectedServiceBean = (SomeService) applicationContext.getBean("someBean");
}
Run Code Online (Sandbox Code Playgroud)
将适当的applicationContext.xml(测试版本)注入此文本的最佳方法是什么?
我知道Spring的@ContextConfiguration,但是我不确定将${testClass}Test-context.xml注释加载到servlet上下文中的上下文注入的最佳方法,以便getRequiredWebApplicationContext(...)可以返回它.
可能重复:
活动上下文和应用程序上下文之间的差异
我想知道之间的实际差异Application Context,并Activity Context在Android..?
这两个让我很困惑.请找我这两个人的区别.
我需要使用当前Maven构建中的"main"applicationContext -a.xml创建一个ApplicationContext.另一个来自另一个maven构建的连接类,并在Maven依赖项包含的jar中预设.
这个想法:
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
"classpath*:applicationContext-*.xml"});
Run Code Online (Sandbox Code Playgroud)
这应该从Classpath加载applicationContext-a.xml,因为它在同一个Project中.这有效.
然后应该从dependency-jar加载applicationContext-b.xml. 这不起作用.
注意
"classpath*:applicationContext-*.xml"
Run Code Online (Sandbox Code Playgroud)
只匹配直接类路径中的XML,jar中没有任何内容.
我发现了什么:
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
"classpath*:applicationContext-*.xml", "classpath*:applicationContext-b.xml"});
Run Code Online (Sandbox Code Playgroud)
这是有效的,但前提是我可以在jar:applicationContext-b.xml中明确告诉xml的文件名
我还需要它来进行集成测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"**/applicationContext*.xml"})
public class TestClass { ... }
Run Code Online (Sandbox Code Playgroud)
最好的想法可能是自定义装载机?必须有一种方法来使这个模式工作......
前段时间有一个解决方案,反之亦然:它只从jar获取applicationContext.xml.如果类路径中有另一个,它只匹配此文件.
我正在使用Spring 3.0.5.RELEASE 和今天使用maven构建项目时,我在appicationContext.xml文件中遇到以下错误:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
'context:component-scan'.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-
config'.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-
driven'.
Run Code Online (Sandbox Code Playgroud)
我试图打开架构链接:
但我总是得到禁止的页面!
这是我如何配置applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans …Run Code Online (Sandbox Code Playgroud) 我在构建应用程序时遇到问题,我收到此错误:
VII 08, 2012 10:03:46 DOP. org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
VII 08, 2012 10:03:46 DOP. org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
VII 08, 2012 10:03:47 DOP. org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Jul 08 10:03:47 CEST 2012]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/D:/work/projekty/java/evrem/target/classes/META-INF/spring/root-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 …Run Code Online (Sandbox Code Playgroud) 我正在创建一个基本的spring-maven项目,它应该作为java应用程序运行(在进程中,而不是在Web服务器上).
我的应用程序上下文位于我的类路径中的resources文件夹下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ox="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- Context -->
<context:component-scan base-package="me.co.nutrition" />
<!-- Properties -->
<context:property-placeholder
location="classpath:/nutrition.accumulation.properties" />
<!-- DB -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultPersistenceUnitName" value="nutrition-pu"/>
<property name="persistenceXmlLocation" …Run Code Online (Sandbox Code Playgroud) 我有一个项目A(Spring Web),并希望导入到另一个项目,如
怎么做 ?请帮忙.谢谢.
我正在开发Spring mvc应用程序.
我有一个控制器,我注入了超过10个服务.
我从这个控制器中暴露了10个以上的url,并且我one or two service在每个方法中都使用了对象.
我在考虑两种方法.
请建议我哪种方法更好,或者两种方法在内存使用和时间方面都相同
谢谢
我们知道,@Autowired只能在spring容器管理的实例中使用,如果你新建一个实例,其中的@Autowired成员不会生效。
但我认为在某些情况下,新实例无法避免。
比如一个RunnableTask。其中包含由 spring 管理的 DAOService。因为任务是手动新建的。所以我不能在ThreadTask中使用DAOService。
所以我想知道如何在Spring Boot 中获取 ApplicationContext ,这样我就可以通过context.getBean().
我知道在 main() 中我可以自动装配 ApplicationContext。但是我不能在任何地方都将上下文作为参数传递!
我想在任何地方获取 ApplicationContext。
任何帮助将不胜感激。
我需要一些澄清; 我有一个我建立的jar,在jar里面它有一个application-context.xml我需要加载的自定义文件.我在jar内的类中加载文件.当我设置这个application-context.xml的url时,是我的jar的类路径特有的目录,还是使用jar-classpath的项目?
例如 -
Jar的类路径:
src > main > META-INF > application-custom-context.xml
Run Code Online (Sandbox Code Playgroud)
项目的类路径:
src > Libraries > myjar.jar > src > main > META-INF > application-custom-context.xml
Run Code Online (Sandbox Code Playgroud)
我知道这些网址不准确:P - 但是,在我的上下文创建中,我会遵循哪个目录结构.这都是理论上的.今天有人问我这个,我不知道怎么回答.我认为如果你从一个jar调用一个类,类路径应该相对于那个jar的内容..但是,我认为远非总是正确的:)
spring ×8
java ×4
autowired ×2
classpath ×2
android ×1
classloader ×1
import ×1
jar ×1
properties ×1
servlets ×1
spring-boot ×1
spring-mvc ×1
unit-testing ×1