草庐IT

Applicationcontext

全部标签

java - 实现 Spring IOC 时如何避免使用 ApplicationContext.getBean()

我刚刚开始了解SpringIOC概念。我经常看到网上找到的大部分例子都是用代码来获取对象的。ApplicationContextappContext=newClassPathXmlApplicationContext("applicationContext.xml");Hellohello=(Hello)appContext.getBean("hello");作为这些问题的引用1和2在堆栈溢出中。我推断,没有必要在代码中使用appContext.getBean("hello"),这被认为是不好的做法。另外,不推荐了。在这里纠正我,如果我的推断是错误的。考虑到这一点,我对我的项目进行了相

java - 我在 Spring 项目目录中的哪里放置 applicationContext.xml 文件

我正在尝试了解Java的Spring框架,但我不确定我应该将applicationContext.xml文件放在哪里来配置Springbean。我的目录结构如下:我要输入.settings吗?还是应该放在springapp的顶层?感谢您的帮助。 最佳答案 放入目录WebContent/WEB-INF。放置在WEB-INF文件夹中的资源无法从Web访问,它们是应用程序内部资源。这很好,因为您的applicationContext.xml不应该可以从网络访问。其他不错的选择是WebContent/WEB-INF/classes或只是sr

xml - 在哪里可以找到示例 applicationContext.xml 文件

在Spring3发行版中,有一个打包在发行版中的项目文件夹。这个项目文件夹有可以使用的示例applicationContext.xml文件。但是,当我从here下载Spring4发行版时它没有附带项目文件夹,我无法找到示例applicationContext.xml。在哪里可以找到示例applicationContext.xml文件。 最佳答案 您可以使用此获取更多信息检查here.以下示例包含如下配置:组件扫描基础com.foo将其视为您的根文件夹。基本数据源定义。属性占位符文件(database.properties)本地化支持

spring - 通过限定符从 ApplicationContext 中获取 bean

鉴于此代码:publicinterfaceService{}@Component@Qualifier("NotWanted")publicclassNotWantedServiceimplementsService{}@Component@Qualifier("Wanted")publicclassWantedServiceimplementsService{}AnnotationConfigApplicationContextctx=newAnnotationConfigApplicationContext();ctx.register(NotWantedService.class)

java - 在所有 SpringBeans 和 ApplicationContext 都被初始化之后调用方法

我在一个复杂的java程序中有一个方法,需要在webApplicationContext和SpringBeans初始化后立即调用。我试过玩弄但是这个方法会调用applicationContext.get().getBean(beanId);方法。我想知道是否有人知道如何做到这一点。谢谢。 最佳答案 从Spring4.2开始,您可以使用注释将事件监听器附加到Springs生命周期事件(和您自己的)。只需将@EventListener添加到方法中,并将事件类型作为第一个(也是唯一一个)参数包含在内,Spring会自动检测它并将其连接起来

java.lang.IllegalStateException : BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

我需要将带有自定义登录页面和数据库连接的SpringSecurity添加到我的SpringMVC项目中。我收到以下错误消息,根据其他问题的答案,我尝试更改代码,例如我将SpringSecuritySchema版本更改为4.0,但代码返回以下错误:将架构更改为4.0http://www.springframework.org/schema/security/spring-security-4.0.xsd错误Cannotinitializecontextbecausethereisalreadyarootapplicationcontextpresent-checkwhetheryouha

java - 如何编写 Spring ApplicationContext.xml 文件?

我正在使用Spring3.2.0版本,所以我试图在applicationContext.xml文件下面写但是这个xml文件在Eclipse编辑器中给出错误Multipleannotationsfoundatthisline:-cvc-elt.1:Cannotfindthedeclarationofelement'beans'.-schema_reference.4:Failedtoreadschemadocument'http://www.springframework.org/schema/beans/spring-beans-3.0.xsd',because1)couldnotfi

java - ApplicationContext 和 ServletContext

当谈到SpringMVC应用程序时,我对两个ApplicationContext和ServletContext感到困惑。我知道每个SpringWeb应用程序只有一个ApplicationContext,每个Web应用程序也只有一个ServletContext。为了初始化ApplicationContext和ServletContext的值,在web.xml中,我们将在context-param标记中添加一些内容。这就是让我感到困惑的地方。这两者有什么区别(我知道ApplicationContext有一些处理bean的方法)?When我们将使用ApplicationContext和Whe

spring - 对 spring applicationContext.xml 中使用的数据源使用编码密码

我想在下面提到的springApplicationContext.xml中保留编码密码有什么方法可以实现吗?目前我已经使用属性占位符配置了所有属性如下所示,但原始密码仍然在我的database.properties中打开springApplicationContext.xml${db.driverClassName}${db.url}${db.username}${db.password}但实际值存在于我的database.propertiesdb.driverClassName=com.mysql.jdbc.Driverdb.url=jdbc:mysql://localhost/my

Java Spring 多个 ApplicationContext

springApplicationContext的定义很模糊,我几乎看完了一整本书的教程,但仍然无法理解ApplicationContext代表什么。根据SpringAPI,ApplicationContext是:Centralinterfacetoprovideconfigurationforanapplication.Thisisread-onlywhiletheapplicationisrunning,butmaybereloadediftheimplementationsupportsthis.TherootinterfaceforaccessingaSpringbeancon