- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在学习this这本书(我强烈推荐),我对作者如何解释 Spring 框架的配置方式感到困惑。
您可以看到本书here中使用的一些代码示例. (任何人都可以使用它们。)我所指的代码将是第 2 章中的代码,如果您想看一下。
书中指出有3种配置Spring Container的方法。
基于 XML 的配置
这将需要一个类似于以下内容的 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl">
</bean>
</beans>
然后为了引导 Spring,将使用的代码将是:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/wiley/beginningspring/ch2/ch2-beans.xml");
此刻我没有任何困惑。
基于 Java 的配置
在这个Configuration方法中,会有一个配置类如下:
@Configuration
public class Ch2BeanConfiguration {
@Bean
public AccountService accountService() {
AccountServiceImpl bean = new AccountServiceImpl();
bean.setAccountDao(accountDao());
return bean;
}
@Bean
public AccountDao accountDao() {
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
return bean;
}
}
负责引导 Spring 的代码如下所示:
ApplicationContext applicationContext
= new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);
所以到这里为止,对我来说一切都清楚了。 (有点..)我还想指出,这里我们实际上有一个名为 @Configuration 的注解......
基于注解的配置
最后一种可用的配置方法,书中解释的是基于注释的配置。
有一个 xml 文件,就像我们在基于 XML 的配置中一样,但是要小得多:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<context:component-scan base-package="com.wiley.beginningspring.ch2"/>
</beans>
所有的bean都有Annotations,例如:
@Component, @Service
等等。
并且所有的依赖都有注解:
@Autowired
这样可以注入(inject)bean。
该配置方式中Spring框架的自举方式如下:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/ch2-beans.xml");
这是我的问题:
为什么(所谓的)Annotation Based Configuration实际上是使用ClassPathXmlApplicationContext而不是上面的AnnotationConfigApplicationContext?后者似乎更适合用于其中包含“基于注释”字样的配置,不是吗?
书中解释的Java Based Configuration似乎应该叫做Annotation Based Configuration。?
书中解释的基于注释的配置方式在我看来实际上类似于:使用 Autowiring bean 的基于 XML 的配置。它甚至没有“基于 Java 的配置”所具有的 @Configuration 注释。
Spring框架有几种配置方式?
最佳答案
为避免混淆,我们应该理解,配置定义和bean定义是两个不同的东西。 Spring 4默认提供三种定义配置的方式:
并且有两种方法可以将 bean 定义添加到应用程序中:
内部配置 bean 定义,当您在配置中通过声明手动添加 bean 时。
在这种情况下,定义将基于配置类型。对于 xml-config,它将是 <bean/>
标记,用于基于 java 的配置 - 带有 @Bean
的方法注释和 beans {...}
Groovy 的构造。
基于注释 bean 定义,当您使用特定注释标记 bean 类时(如 @Component
、@Service
、@Controller
等)。这种类型的配置使用 classpath scanning .
在这种情况下,您必须指定扫描类路径的指令。对于 xml-config,它将是 <context:component-scan base-package="..."/>
, 对于 java-config - @ComponentScan
注释,用于 Groovy ctx.'component-scan'(...)
调用。
如您所见,您可以在不同的组合中使用配置和 bean 定义。
注意,如果您使用基于 xml 的配置,您可以选择驱动依赖注入(inject)的方法:手动在 xml 中,或使用注释(@Autowire
,@Required
等)。在后期情况下,您必须定义 <context:annotation-config/>
.但不要混淆 bean 定义和依赖注入(inject)控制。
现在基于这个观点让我们试着回答你的问题:
Why is the (so-called) Annotation Based Configuration actually usingClassPathXmlApplicationContext but notAnnotationConfigApplicationContext above?
本书的作者混淆了概念。实际上,这是一个基于 xml 的配置,带有基于注解的 bean 定义。
The Java Based Configuration explained in the book seems like whatshould be called Annotation Based Configuration.?
你说得对——基于 Java 的配置确实积极地使用注解,并且可以称为基于注解。但是注解是 Java 的一部分。此外,这是一个传统术语,specified in documentation .
How many ways are there to configure Spring framework?
因此,默认情况下,我们有三种方式来描述配置,以及两种方式来定义 bean。这变成了配置 Spring 框架的六种方法(默认情况下)。但是,当然,所有这些方式都可以相互结合使用。
关于java - Spring框架有几种配置方式?它们在技术上有何区别? (不是优点或缺点..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807056/
我目前正在尝试编写一个函数,它将一个新元素添加到列表顶部,并将列表的其余部分推回...任何人都可以帮助我吗?当我尝试编译和运行我的程序时,它不起作用。它无限循环。有什么帮助吗? #include #
我有时在程序中使用表情符号来突出显示代码的某些部分(在开源库中)。每个脚本我很少使用超过 5-6 个,而且我发现它们在文本编辑器中的颜色确实很突出。 通常,它们是 transient 标记,并且会在与
假设我有一个像这样的(有点人为的组件) const ExampleComponent = (props) => { const renderList = () => {
谁能告诉我在 WPF 中使用命令与事件的优点是什么?命令或事件是否会遇到内存泄漏?什么是最快的方法。它们的缺点是什么? 最佳答案 与事件处理程序相比,命令具有两个主要优点: 命令未链接到调用者,所以相
我应该使用什么?或者是否有特殊场合我应该使用其中一种而不是另一种? 最佳答案 对于更高维度,K 图不一定失败。问题在于,对五个以上的变量进行可视化非常困难。 4 变量 K 图是二维的并且易于可视化。第
在使用kubernetes部署我的应用程序和数据库容器时,我试图了解以下体系结构的优缺点。 背景知识:该应用程序位于Nginx代理后面。所有请求都从代理流向Web服务器。 Web服务器是唯一有权访问(
有多种脚本环境可用于 .NET 应用程序(例如 this post)。 我的问题是,使用它们各自的优缺点是什么? 示例包括(但不限于) PowerShell IronPython 路亚 JavaScr
以下是我在访问资源文件时看到的 3 个选项。由于可能存在异常(exception)情况,选项 1 可能是最不推荐的,因此问题实际上与选项 2 或选项 3 相关,即哪个是首选或推荐的实现。 选项 1 -
Effective Java - Item-2 states, a JavaBean may be in an inconsistent state partway through its const
我不希望每次旋转设备时都重新创建我的 Activity,所以我将 android:configChanges="keyboardHidden|orientation"标签放在我的 list 文件中。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我有一个工作分配方案,其中每个单元都进行一些簿记和管理,并将任务传递给订单链中的下一个单元,例如;假设有 3 个类:老板、经理、 worker class Boss { void do_asy
创建一个包含字段 A、B、C 的唯一键然后创建另一个包含字段 C、B、A 的唯一键是否有缺点,因此 MySQL 将在仅使用 A 和仅使用 A 进行搜索的情况下利用索引C? 最佳答案 您不想创建额外的复
我对 JavaScript 中的原型(prototype)设计和构造函数很陌生。我看到有人这样声明构造函数: var Person = (function(){ function Person
假设我有一个 json 数据对象,我想将其解析为我自己的对象。我遇到过两种方法。第一种是像这样使用初始化程序: class DataModelOne { let firstProperty: S
在c++中我们可以调用一个类的方法而不用实例化它。比如; MyClass mc; mc.method(); 在不实例化类的情况下使用类方法的优点和缺点是什么?我们什么时候应该使用这种类型? 最佳答案
我需要更好地了解 web 开发,尤其是 CSS 的工作原理。我从事 Web 开发已有大约 2 年了,我认为现在是时候了解如何优化我网站的性能了。我想问的事情不多。 首先: 每当我的页面上有标签内容时,
代码如下: .thumbnail { float:left; width:110px; height:90px; margin:5px; }
上下文:数据库规范化,我正在创建一个表:UserInvolvement。User 可以通过多种方式(基于角色和其他方式)参与另一个实体:Foo。在当前情况下,每种类型的参与都作为单独的列添加到 Foo
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一名优秀的程序员,十分优秀!