spring - Spring 的切入点

标签 spring

我正在开发一个使用 Spring 依赖注入(inject)框架的代码库。我正在检查 spring,但我无法找到 Spring XML 文件是如何作为代码中的第一件事执行的。谁定义了这个入口点?就像在代码中一样,我知道 main() 方法是起点。

最佳答案

Application Context是Spring的高级容器。

spring 它是容器。入口点是 ApplicationContext 。 ApplicationContext 包含BeanFactory 的所有功能,一般推荐使用BeanFactory。 BeanFactory 仍然可以用于轻量级应用程序,例如移动设备或基于小程序的应用程序。

**最常用的 ApplicationContext 实现是:**

FileSystemXmlApplicationContext - 此容器从 XML 文件加载 bean 的定义。此处需要向构造函数提供 XML bean 配置文件的完整路径。

ClassPathXmlApplicationContext - 此容器从 XML 文件加载 bean 的定义。这里你不需要提供 XML 文件的完整路径,但你需要正确设置 CLASSPATH,因为这个容器在 CLASSPATH 中看起来像 bean 配置 XML 文件。

您可以使用您的 xml 配置运行容器:

 public static void main(String[] args) {
      ApplicationContext context = new FileSystemXmlApplicationContext
         ("FILE PATH TO application-context.xml");
      --application-context.xml - is your cofing file
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }

 public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext 
         ("classthat:/application-context.xml");
      --application-context.xml - is your cofing file
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }

默认情况下,ApplicationContext 实现急切地创建并配置所有单例 bean 作为初始化过程的一部分。 eagerly 意味着所有未标记为惰性的 bean 将在 spring 容器启动时创建。

关于spring - Spring 的切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442181/

相关文章:

spring - @Service 中的 @Autowired ServletContext

java - 独立的 Spring Hibernate 中的空 Autowiring 字段

java - Spring Data Neo4J 存储库 findAll() 导致 nullpointerexception

java - 无法解析 @Value ("${xxx:}")(Spring 4),但可以解析 @Value ("${xxx}")

java - Spring Data REST - 当 PATCH 在请求正文中使用空数组发送时出现异常

java - lambda 表达式阻止类进行 spring 组件扫描

javascript - 提交后复选框值不变

使用接口(interface)定义spring bean

spring - 了解基本的Spring框架和总流程

java - ComponentScan 忽略排除过滤器(测试中)