【发布时间】:2014-10-19 04:15:16
【问题描述】:
我想利用 XML 配置文件中的一些 Spring Boot 自动配置的 bean,但是当我尝试这样做时,我总是遇到异常和错误。
例如,如果我的类路径上有与数据相关的库,Spring Boot 将自动配置一个 DataSource 对象,我可以将其自动装配到我自己的 bean 和类中,如下所示:
@Configuration
@ImportResource("classpath:xmlconfig.xml")
public class Config {
// This works!!
@Autowired
private DataSource dataSource;
@Bean
public ClassThatRequiresADataSource() {
ClassThatRequiresADataSource foo = new ClassThatRequiresADataSource();
foo.setDataSource(dataSource);
return foo;
}
}
但是,如果我尝试在 XML 配置文件中做同样的事情,我会得到一个异常。我一直在通过将@ImportResource("classpath:xmlconfig.xml") 添加到我的主配置类来引导 XML 配置文件。这是我正在谈论的一个例子......内部xmlconfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- THIS DOES NOT WORK! -->
<bean id="anotherClassThatRequiresADataSource" class="my.package.AnotherClassThatRequiresADataSource">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
尽管dataSource 是一个有效的、自动配置的 Bean 名称,但在运行 Spring Boot 应用程序时,上述内容会出现异常。我还尝试使用自动配置的ConnectionFactory(在类路径上使用 ActiveMQ)和在类路径上使用 Hibernate 和 JPA 的EntityManagerFactory,但这些都不起作用。
基本上,我要问的是:什么相当于将 Spring Boot 自动配置的 bean 自动装配到 XML 配置文件中?
这是我的主要 Spring Boot 入口点,只是所有文档中列出的标准类:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
我主要在 Spring Integration 应用程序中使用它,其中 Java 配置尚未得到很好的支持,框架的核心是基于 XML 配置的,但我想使用 Spring Boot 自动配置 @987654331 @ 和ConnectionFactory bean 中的一些集成元素。
编辑:@AdilF 提供的答案适用于 dataSource bean,但类似的配置不适用于 connectionFactory bean。请参阅以下 GitHub 项目的演示代码来说明这一点:
https://github.com/ccampo133/autoconfig-test/tree/master
如果有人能弄清楚如何正确连接connectionFactory bean,我将不胜感激。
以下是说明这一点的大部分代码:
Application.java
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Config.java
@Configuration
@ImportResource("classpath:/resources/config.xml")
public class Config { }
FooService.java
@Service
public class FooService {
final private Logger logger = LoggerFactory.getLogger(FooService.class);
@Autowired
private DataSource dataSource;
@Autowired
private ConnectionFactory connectionFactory;
@Autowired
private EntityManagerFactory entityManagerFactory;
@PostConstruct
public void init() {
Assert.notNull(dataSource, "dataSource is null!");
logger.info("dataSource not null");
Assert.notNull(connectionFactory, "connectionFactory is null!");
logger.info("connectionFactory not null");
Assert.notNull(entityManagerFactory, "entityManagerFactory is null!");
logger.info("entityManagerFactory is not null");
}
}
BarService.java
public class BarService {
final private Logger logger = LoggerFactory.getLogger(BarService.class);
private DataSource dataSource;
private ConnectionFactory connectionFactory;
private EntityManagerFactory entityManagerFactory;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
public void setEntityManagerFactory(final EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@PostConstruct
public void init() {
Assert.notNull(dataSource, "dataSource is null!");
logger.info("dataSource not null");
Assert.notNull(connectionFactory, "connectionFactory is null!");
logger.info("connectionFactory not null");
Assert.notNull(entityManagerFactory, "entityManagerFactory is null!");
logger.info("entityManagerFactory is not null");
}
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="barService" class="app.service.BarService">
<!-- THIS WORKS! -->
<property name="dataSource" ref="dataSource"/>
<!-- THIS DOESN'T WORK! -->
<property name="connectionFactory" ref="connectionFactory"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
build.gradle
buildscript {
ext {
junitVersion = "4.11"
springBootVersion = "1.1.5.RELEASE"
springIntegrationVersion = "4.0.3.RELEASE"
activeMqVersion = "5.7.0"
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "spring-boot"
configurations {
providedRuntime
}
jar {
baseName = "autoconfig-test"
version = "0.0.1-SNAPSHOT"
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone/" }
}
dependencies {
// Spring Boot starters
compile "org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}"
compile "org.springframework.boot:spring-boot-starter-integration:${springBootVersion}"
compile "org.springframework.integration:spring-integration-jms:${springIntegrationVersion}"
// ActiveMQ
compile "org.apache.activemq:activemq-core:${activeMqVersion}"
// Persistence
runtime "com.h2database:h2"
// Test
testCompile "junit:junit:${junitVersion}"
}
【问题讨论】:
-
您在哪里创建数据源?您的 java 配置和 xml 配置都没有创建数据源。
-
Spring Boot 如果你启用了
@EnableAutoConfiguration功能,就会自动创建一个DataSource。然后,您可以将其自动连接到您的 JavaConfig bean。 -
对于初学者,您的
Application类需要在已经存在的内容旁边添加@Configuration注释。基于 Java 的配置和 XML 配置之间的主要区别在于,Java 配置基于类型注入,而 XML 配置基于名称。我建议不要创建Config类,而是将那里的@Configuration和@ImportResource移动到您的应用程序类(或者将Application类上的内容移动到您的Config类)。跨度> -
应用程序类用
@Configuration注解。我一定在帖子里把它漏掉了。无论哪种情况,如果我将所有内容都移至应用程序类,我仍然会遇到同样的问题。@ComponentScan注释的目的是让我不必在主类中包含所有 bean 定义,因此它不应该改变任何东西。 -
您需要做的就是从您的
Application.java文件中删除@Configuration @ComponentScan @EnableAutoConfiguration并将其放在Config.java上。这对我有用。
标签: spring spring-integration spring-boot