Spring自动装配无法查看上下文bean,除非在基础包上定义了组件扫描

时间:2015-09-16 10:57:26

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

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

如果我通过xml配置为注入工作定义依赖注入,如

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

但是,如果我用@ComponentScanning(“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;
    }
}

Controller 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 );
    }
}

1 个答案:

答案 0 :(得分: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并重新启动。