【问题标题】:Spring Autowiring can't see context beans unless component scanning defined on base package除非在基础包上定义组件扫描,否则 Spring Autowiring 无法看到上下文 bean
【发布时间】:2015-09-16 10:57:25
【问题描述】:

我目前使用@SpringBootApplication 设置我的spring 应用程序,但是一切正常,我无法使用自动装配注入在xml 中定义的bean。

如果我通过 xml 配置定义依赖注入,则注入的工作方式类似于

<bean id="dao" class="com.elevations.dao.Dao">
    <property name="dataSource" ref="dataSource"/>
</bean>

但是,如果我用 @ComponentScanning("elevations") 表示我的应用程序,(elevations 是我的基本包)自动装配工作,但是我的控制器端点停止工作。为什么会这样?

我的应用程序 com.elevations.Application 定义为

@SpringBootApplication
public class Application
{
    public static void main( String[] args )
    {
        SpringApplication.run( Application.class, args );
        ApplicationContext context = new ClassPathXmlApplicationContext( "context.xml" );
    }
}

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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.elevations.dao"/>

    <!--postgresql jdbc bean-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byType">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/elevationdb"/>
        <property name="username" value="postgres"/>
        <property name="password" value=""/>
    </bean>


    <bean id="dao" class="com.elevations.dao.Dao"/>

</beans>

我正在尝试自动装配 com.elevations.dao.Dao 的测试类

@Component
public class Dao
{
    private DataSource m_dataSource;

    @Autowired
    public void setDataSource( DataSource dataSource )
    {
        m_dataSource = dataSource;
    }
}

控制器 com.elevations.controllers.ApplicationController

@Controller
public class ApplicationController
{
    @RequestMapping( value = "/elevations", method = RequestMethod.GET )
    public String pageGet()
    {
        return "elevationMaps";
    }

    @RequestMapping( value = "/elevationData", method = RequestMethod.GET )
    @ResponseBody
    public LatLng mapGet( @RequestParam( "bounds" ) String bounds, @RequestParam( "diameter") String diameter )
    {

        JsonParser parser = new JsonParser();
        JsonElement viewBounds = parser.parse( bounds );

        return new LatLng( 1, 0 );
    }
}

【问题讨论】:

  • 没有@Autowire所以没有自动连接...如果你不指定信息什么都不会发生。
  • 对不起,我删除了它,但如果我有 @Autowire,我会收到一条错误消息,说它找不到 DataSource 的 bean 定义
  • 当然不能,因为您的 Spring Boot 应用程序对您的 XML 一无所知。也不应该,使用 Spring Boot 不能对抗/围绕它工作。 (顺便说一句,适用于任何框架)。

标签: java spring spring-mvc dependency-injection autowired


【解决方案1】:

如果您想使用 xml,请将 @ImportResource 添加到您的应用程序类中。

@ImportResource("context.xml")
@SpringBootApplication
public class Application {

    public static void main( String[] args ) {
        SpringApplication.run( Application.class, args );
    }
}

但我强烈建议放弃您的 xml,只需将 application.properties 添加到 src/main/resources 并添加以下属性。

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/elevationdb
spring.datasource.username=postgres
spring.datasource.password=

然后从您的班级中删除@ImportResource 并重新开始。

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多