标签: applicationcontext

如何配置Spring以尽可能多地保存内存?

我们在具有内存限制的半嵌入式设备上部署应用程序.我们正在分析应用程序的堆转储并攻击最大的消费者.

我们将Spring 2.5和Spring DM 1.1一起使用,我们注意到我们的一些包含更复杂的Spring上下文的bundle会占用相当多的内存,因为Spring似乎保留了包含从XML解析的所有BeanDefinition的整个对象图. .我认为,一旦应用程序初始化并注入所有内容,大部分内容都是不必要的.

是否有Spring的配置选项允许控制此行为?在某种低内存模式下运行?扔掉所有不必要的东西?交易计算时间的大小?

java memory heap spring applicationcontext

6
推荐指数
1
解决办法
4046
查看次数

Spring分层上下文

我有以下问题:我正在尝试在现有的耳朵应用程序中实现Spring (使用Jboss作为App Server和Hibernate作为ORM).ear应用程序包括war(基本上很少有简单的servlet),har(hibernate archive只有数据模型 - 作为类和相应的hibernate映射文件 - 作为xmls),sar(只有一个解析类作为计划任务).所以,我很感兴趣如何在战争中共享har存档的最终appContext(因为servlet正在从数据库中检索数据)和sar(因为解析器正在"填充"数据库).关于这一点,(战争和SAR)应使用单SessionFactory的豆这将在har的背景下定义.代码示例或代码段将受到高度赞赏.

jboss spring hibernate hierarchical applicationcontext

6
推荐指数
0
解决办法
508
查看次数

组件扫描未在Tomcat webapp中的JAR中找到@Component

我刚刚在Spring bug系统中提交了一个错误(https://jira.springsource.org/browse/SPR-8551),但我仍然不确定我是否遗漏了某些东西

我跟踪了<context:component-scan/>这个陈述的问题.给定以下两个类在Web应用程序的WEB-INF/lib中的相同JAR中(JAR文件具有目录结构):

测试/ TheBean.java:

package test;
@Component
public class TheBean{
}
Run Code Online (Sandbox Code Playgroud)

测试/ BeanSearcher.java:

package test;
public class BeanSearcher{

  public void init(){ 
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
    ctx.scan("test"); 
    ctx.refresh(); 
    TheBean b=  ctx.getBean(TheBean.class); 
    // What is the value of b? 
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我new BeanSearcher().init()在jUnit测试用例或其他类型的独立应用程序中运行,则会为b分配一个TheBean实例,但是如果我在JSP中运行它,ctx.getBean()则返回null.

所以,我做错了什么或没有考虑到一些事情,这只是一个错误......?

编辑8/8/2011:当我试图简化问题时似乎工作正常,但是,当我尝试使其工作时,在OpenCms的初始化中,它失败了.现在我试图寻找工作版本和不工作版本之间的差异.(类加载器,不同JAR中或直接在WEB-INF /类中的相关类的使用,通过反射调用等)

spring web-applications opencms applicationcontext

6
推荐指数
1
解决办法
1万
查看次数

在 ApplicationContext 被销毁之前立即触发的 Spring 关闭事件?

我正在寻找一个拦截器或触发器来知道,所有的上下文 bean 都被销毁了,applicationcontext 实例即将销毁自己。所以我可以在应用程序生命周期结束时执行一个过程。

有这个事件类型 ContextClosedEvent,它与我想做的事情很接近,但是它销毁 bean抛出事件。我认为它带有 applicationcontext 的 close() 方法。所以它不适合我的需要

有任何想法吗?

问候

阿里

java spring destroy applicationcontext

6
推荐指数
1
解决办法
8672
查看次数

解析applicationContext.xml会呈现一个`java.lang.NoSuchMethodError`.这可能是由依赖问题引起的吗?

我正在使用带注释的应用程序上下文(@ContextConfiguration(locations={"classpath:applicationContext.xml"}))调用测试方法(Junit ).测试失败,出现以下堆栈跟踪:

11:20:39.793 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@28419cd] to prepare test instance [com.atrioom.TestDynamo@7fbdc7db]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:91) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:74) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:116) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) ~[spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:251) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67) [spring-test-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at …
Run Code Online (Sandbox Code Playgroud)

java spring applicationcontext

6
推荐指数
1
解决办法
1万
查看次数

Android中的活动之间的对象共享

嗨,我有一个关于在整个应用程序中传递对象的问题.假设我想在整个应用程序中拥有一个大的自定义对象.多个活动和服务将使用此对象.我最初做的是这个.

首先,我创建了一个Application类并定义了一个singleton对象.

public class FooApplication extends Application {
    public static SharedObj obj;
    ...
}
Run Code Online (Sandbox Code Playgroud)

其次,在一个活动中我设置了这个对象的值

public class FooActivity extends Activity {
    OnCreate() {
        FooApplication.obj = SharedObj.getInstance();
    }
}
Run Code Online (Sandbox Code Playgroud)

第三,在另一个活动中,我访问了这个对象

public class BarActivity extends Activity {
    OnCreate() {
        SharedObj useThis = FooApplication.obj;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,这有什么问题?它似乎工作正常,但我发现有时这个单例对象的值由于某种原因被设置为null.我这样分享这个对象而不是让它变得可分的主要原因是这样做对我来说有点贵,我做的事情看起来很容易.有缺点吗?

经过一些研究,我发现还有另一种方法可以在整个应用程序中共享一个对象.

首先,在app类中定义

public class FooApplication extends Application {
    public SharedObj obj = new SharedObj();
    ...
}
Run Code Online (Sandbox Code Playgroud)

其次,在这样的活动中初始化

public class FooActivity extends Activity {
    OnCreate() {
        FooApplication fooApp = (FooApplication)getApplicationContext();
        fooApp.obj = new SharedObj();
    }
}
Run Code Online (Sandbox Code Playgroud)

第三,在另一个活动中,我访问了这个对象 …

singleton android sharing applicationcontext

6
推荐指数
1
解决办法
4374
查看次数

spock测试中的上下文配置

我有这样的Application课:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:applicationContext.xml")
@EnableJpaRepositories("ibd.jpa")
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)

我也有UserService课(它被发现 @EnableJpaRepositories("ibd.jpa")):

@RestController
@RequestMapping("/user")
public class UserService {

@Autowired
private UserRepository userRepository;

@RequestMapping(method = RequestMethod.POST)
public User createUser(@RequestParam String login, @RequestParam String password){
    return userRepository.save(new User(login,password));
} 
Run Code Online (Sandbox Code Playgroud)

我试着测试UserService:

@ContextConfiguration
class UserServiceTest extends Specification {

@Autowired
def UserService userService


def "if User not exists 404 status in response sent and corresponding message shown"() { …
Run Code Online (Sandbox Code Playgroud)

java spring spock applicationcontext

6
推荐指数
2
解决办法
6660
查看次数

Android:更改语言Android N,应用程序实例未更新

我以编程方式遵循Android N更改语言,从而在android N及更高版本中更改了我的应用程序的语言。但是,我仍然对应用程序上下文实例有疑问。

在我的应用程序类中:

private static Application mInstance;

public static Context getApplication() {
    return mInstance;
}

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
}
Run Code Online (Sandbox Code Playgroud)

语言已更改,但是从应用程序上下文获取的资源未更改。例如:

MyApplication.getApplication().getResources().getString(stringId);  
Run Code Online (Sandbox Code Playgroud)

用返回错误的语言字符串。

在这种情况下可以更新应用程序实例吗?我把这个问题坚持了几个小时。因为MyApplication.getApplication()已在我的应用程序的许多地方使用。因此,我无法转换为Activity上下文。

非常感谢。

android multiple-languages applicationcontext android-7.0-nougat

6
推荐指数
1
解决办法
809
查看次数

如何在 Spring Framework 4 中初始化应用程序上下文

我有一个基于Spring Framework 4及其子项目 - Spring Data Solr 的项目。

我有机会看到的所有示例都解释了如何组织您的项目 - 从基本实体( pojo )类到 Spring 特定类(例如存储库和服务)。当谈到测试功能时,所有示例都显示带有私有字段( spring bean )的测试,该字段通常在注释的帮助下进行初始化

@ContextConfiguration(classes = some-spring-data-main-class.class, loader = SpringApplicationContextLoader.class)
Run Code Online (Sandbox Code Playgroud)

然后就可以在方法中调用它的bean的方法了@Test

但是,当涉及到项目中的 init bean 时 - 如何使用 Spring 4 来实现它,它是完全无 XML 的(我的意思是,我没有文件applicationContext xml)。

PS 在 Spring 3 中我们通常这样写:

ApplicationContext context = new ClasspathApplicationContext("applicationContext.xml") 
Run Code Online (Sandbox Code Playgroud)

期望与 Spring 4 类似的东西引入应用程序初始化的全新概念是否合理?现在我们应该写什么来初始化应用程序的第一个 bean?

spring launch javabeans applicationcontext

5
推荐指数
1
解决办法
3万
查看次数

ApplicationContext.getBean 与 new 关键字

我正在使用 JSF + Spring+ Hibernate

protected @Inject ChartOfAccount chartOfAccount;
Run Code Online (Sandbox Code Playgroud)

chartOfAccount我基本上想从列表中填充

for (DistributionEntry de : getDistributionEntries()) {

     chartOfAccount.setAccount(de.getAccount());
     chartOfAccountList.add(chartOfAccount);
}
Run Code Online (Sandbox Code Playgroud)

对于每次迭代,我想要新的对象,chartOfAccount否则你知道列表包含具有最新值的相同对象。

解决方案一:使用 new 关键字 :-p

for (DistributionEntry de : getDistributionEntries()) {
     ChartOfAccount coa= new ChartOfAccount();
     coa.setAccount(de.getAccount());
     chartOfAccountList.add(coa);
}
Run Code Online (Sandbox Code Playgroud)

解决方案二:applicationContext.getBean

for (DistributionEntry de : getDistributionEntries()) {
     chartOfAccount= applicationContext.getBean(ChartOfAccount.class);
     chartOfAccount.setAccount(de.getAccount());
     chartOfAccountList.add(chartOfAccount);
}
Run Code Online (Sandbox Code Playgroud)

但我读过某些文章,以避免使用applicationContext.getBean

如果我避免使用applicationContext.getBean处理这种情况的最佳方法是什么?两种行为都会相同吗?(ApplicationContext.getBean 与 new 关键字)

注意:我的托管bean是@Scope(“session”),模型是@Scope(BeanDefinition.SCOPE_PROTOTYPE),所以我们都知道,对于一个会话,它是单例,而不同会话的原型。

java jsf spring scope applicationcontext

5
推荐指数
1
解决办法
3956
查看次数