Spring @Autowired不使用跨多个项目的配置文件

时间:2015-08-13 23:12:26

标签: java spring

我有databusinessweb图层的不同Java项目。我在web层初始化Spring上下文,并且只在web层中包含所有Spring bean XML文件。

@Autowired功能仅适用于web层中的Spring bean(并不适用于其他两个项目中的Spring bean)。

我是否还需要在其他两个项目(databusiness)中定义Spring bean XML配置?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Service -->
    <context:component-scan base-package="com.jai.mdm" />
    <context:annotation-config />

    <jaxrs:server id="mdmCxfServer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="airlineService" />
        </jaxrs:serviceBeans>

        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
        </jaxrs:extensionMappings>

        <jaxrs:providers>
            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
        </jaxrs:providers>

        <jaxrs:features>
            <cxf:logging />
        </jaxrs:features>
    </jaxrs:server>

    <!-- Business -@Autowired not working hence manual getter/setter old style used -->
    <bean id="airlineBusiness" class="com.jai.mdm.business.impl.AirlineBusinessImpl">
        <property name="airlineRepository" ref="airlineRepository" />
    </bean>

    <!-- Data -@Autowired not working hence manual getter/setter old style used -->
    <bean id="airlineRepository" class="com.jai.mdm.data.repository.impl.AirlineRepositoryImpl">
    </bean>

    <jpa:repositories base-package="com.jai.mdm.data.repository"  />

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@localhost:1521/jaydb" />
        <property name="username" value="jay" />
        <property name="password" value="jay" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.jai.mdm.data" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />
</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>mdm</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

2 个答案:

答案 0 :(得分:1)

我建议为每个项目使用不同的Spring XML配置文件(或更多,具体取决于您的需要)。例如,JPA存储库用于在data项目中声明,并在需要时导入;其他人一样。

查看Spring的import

我认为您可以在每个项目中包含许多配置文件,并且可以使用(web路径/META-INF/*<import resource="classpath:/META-INF/spring-context-data.xml" /> <import resource="classpath:/META-INF/spring-context-business.xml" /> 导入它们):

alert

答案 1 :(得分:0)

这是我的错误......我在界面中使用了注释(比如@Repository,@ Service)而不是实现类。将它移到课堂后,它可以正常工作。