任何人都可以提供一个加载applicationContext.xml文件的SpringApplication示例吗?
我正在尝试使用Spring的示例(基于Gradle)将我的GWT RPC应用程序移动到RESTful Web服务.我有一个applicationContext.xml,但我没有看到如何让SpringApplication加载它.通过手动加载
ApplicationContext context = new ClassPathXmlApplicationContext(args);
导致空的背景.......即使它起作用,也会与返回的那个分开
SpringApplication.run(Application.class, args);
或者有没有办法让外部bean进入SpringApplication.run创建的应用程序上下文?
我有一个带弹簧的传统3层应用程序.我的一个仓库的需要> 3分钟初始化,所以我为了加快整个过程想过一些多线程的方式 - 我想大多数的服务,在我的依赖关系树控制器已经可以开始,所以只有少数必须等待最后一个存储库出现了.
有没有最佳实践方法?
我一直在尝试使用Spring 3.1的bean定义配置文件和嵌套bean.我希望我可以根据活动配置文件定义不同的bean.请考虑以下重点简化示例,以便我的Spring上下文包含类似的内容
<bean id="say" class="test.Say" p:hello-ref="hello"/>
<beans profile="prod">
<bean id="hello" class="test.Hello" p:subject="Production!"/>
</beans>
<beans profile="dev">
<bean id="hello" class="test.Hello" p:subject="Development!"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
线程"main"中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源[applicationContext.xml]中定义名称为'say'的bean时出错:在设置bean属性'hello'时无法解析对bean'hello'的引用; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:在org.springframework.beans.factory的org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)中没有定义名为'hello'的bean. .support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:
我希望根据活动的Maven配置文件(在我的例子中为prod或dev)来定义hello bean .我开始认为Spring活动配置文件(spring.profiles.active)可能与Maven配置文件完全无关.
有人可以解释我哪里出错吗?(甚至可以使用配置文件?).
我创建了一个ApplicationContextInitializer实现来从客户源(ZooKeeper)加载属性并将它们添加到ApplicationContext属性源列表中.
我能找到的所有文档都与Spring web-apps有关,但我想在一个独立的消息应用程序中使用它.
是实例化我的实现,创建上下文,然后将"上下文"传递给我的"手动"实现的正确方法吗?或者我错过了将我的初始化程序应用于我的上下文的框架的一些自动功能?
我遇到了Bean实例化排序很重要的问题.目前,Bean3从下面运行基于数据库的缓存放置操作,Bean 1使用Proxy Bean2查询新创建的缓存.优先级是Bean3和Bean 2在Bean1实例化之前完全实例化,即当Spring容器出现时.这些bean是单独的JARS,Bean2对Bean1的引用不使用Autowired.而是服务定位器给它一个参考.我们使用的是Spring 2.5.2而不是使用XML来实例化bean.任何帮助赞赏!
JAR1(春季项目)
@Service ("bean3")
public class Bean3 implements ApplicationListener {
public void onApplicationEvent() {
//load data from DB and populate cache
}
public void getCache(){
//get data from cache
}
Run Code Online (Sandbox Code Playgroud)
}
@Service ("bean2")
public class Bean2 {
@Autowired
private Bean3 bean3;
private void methodA(){
bean3.getCache();
}
}
Run Code Online (Sandbox Code Playgroud)JAR2(非春季项目)
public class Bean1{
Bean2 bean2 = SpringServiceLocator.getBean("bean2")
public void methodB(){
bean2.methodA();
}
}
Run Code Online (Sandbox Code Playgroud)如果需要,需要哪种配置?这不推荐?
带注释的类:
package com.springbug.beanfactorydependencyissue;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class DependantBean {
@Resource
DependencyBean dependencyBean; // Isn't initialized correctly
public DependencyBean getDependencyBean() {
return dependencyBean;
}
}
Run Code Online (Sandbox Code Playgroud)
失败的依赖bean:
package com.springbug.beanfactorydependencyissue;
import org.springframework.stereotype.Component;
@Component
public class DependencyBean {
}
Run Code Online (Sandbox Code Playgroud)
测试用例:
package com.springbug.beanfactorydependencyissue;
import static org.fest.assertions.Assertions.assertThat;
import javax.annotation.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import com.springbug.beanfactorydependencyissue.DependantBean;
@ContextConfiguration(locations = "/applicationContext.xml")
public class AppTest extends AbstractTestNGSpringContextTests {
@Resource
private DependantBean annotatedBean;
@Test
public void testThatDependencyIsInjected() {
// Fails as dependency injection of annotatedBean.dependencyBean …Run Code Online (Sandbox Code Playgroud) java spring annotations dependency-injection applicationcontext
我正在研究使用 Spring 实现的批处理应用程序。
在这个应用程序中,我发现了以下结构:
BATCH PROJECT
|
|
|------> src/main/java (containing the packages)
|
|------> src/main/resources
|
|----------> META-INF
|
|--------> applicationContext.xml (Spring configuration file)
Run Code Online (Sandbox Code Playgroud)
因此,正如您在前面的模式中看到的,在src/main/resources 中,我找到了META-INF文件夹,其中包含applicationContext.xml文件,该文件包含 Spring 配置(bean 定义)。
这批工作正常,但我有一些疑问:这个地方可以认为是放置applicationContext.xml文件的正确位置吗?
我总是看到META-INF目录到 web 应用程序(而不是批处理应用程序)到我在这个项目中没有的以下文件夹中(因为它不是 web 应用程序):
webapp
|
|_src
|
|_WebContent
|
|__WEB-INF
|
|__META-INF
Run Code Online (Sandbox Code Playgroud)
这是正确的还是我可以做得更好?
我想在 Spring Boot 应用程序的应用程序上下文中存储一个特定的变量(字符串或对象)。但我不想使用 xml 或属性文件来存储它。
将有一个函数将数据存储在应用程序上下文中。我应该能够检索它、修改它、删除它或添加更多数据。
基本上我想在初始化完成后将数据存储在应用程序上下文中。
我有以下设置为我的Spring Application Context.
@Configuration
public class RmiContext {
@Bean
public RmiProxyFactoryBean service() {
RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean();
rmiProxy.setServiceUrl("rmi://127.0.1.1:1099/Service");
rmiProxy.setServiceInterface(Service.class);
return rmiProxy;
}
}
@Configuration
public class LocalContext {
@Bean
public Controller Controller() {
return new ControllerImpl();
}
}
@Configuration
@Import({RmiContext.class, LocalContext.class})
public class MainContext {
}
上面的设置工作正常,但我想启用@ComponentScan注释Controller,@Component因为Controller我的应用程序中有很多s,当逐个声明时使用是很乏味的@Bean.
@Configuration
@ComponentScan(basePackageClasses = {Controller.class})
public class LocalContext {
/* ... */
}
问题是,当我这样做时@ComponentScan(basePackageClasses = {Controller.class}),以前的精细工作RmiProxyFactoryBean无法识别或无法创建.
那么,如何配置my …
我以编程方式遵循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
spring ×8
java ×6
spring-boot ×2
android ×1
annotations ×1
components ×1
java-ee ×1
maven ×1
meta-inf ×1
rmi ×1