服务和存储库未注入

时间:2015-07-03 11:54:06

标签: java spring

我完全绝望了。这是我春天的第一天,我无法使它工作 - XMLProfileService根本没有注入,它仍然是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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:repository="http://www.springframework.org/schema/data/repository"
       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-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/data/repository
        http://www.springframework.org/schema/data/repository/spring-repository.xsd
        ">

    <jpa:repositories base-package="test.repository"></jpa:repositories>
    <context:annotation-config />
    <context:component-scan base-package="test" />
    <tx:annotation-driven />

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

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="myPersistenceUnit"/>
        <property name="packagesToScan" value="test.domain" />
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false"/>
                <property name="generateDdl" value="true"/>
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
            </bean>
        </property>
    </bean>
</beans>

XMLProfileService

@Service
public class XMLProfileService {

    @Inject
    private PropertiesRepository profileDb;


    public void findByNodeName(String nodeName, String password) {
    }
}

Main.java

public class Main {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        new Main2().run();
    }

}

Main2.java

public class Main2 {

    @Autowired
    private XMLProfileService profileService;

    public void run() {
        profileService.findByNodeName("z", "b");
    }

}

pom.xml (依赖项)

<dependencies>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.8.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.9</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.7.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
        <!--<exclusions>-->
        <!--<exclusion>-->
        <!--<groupId>org.hibernate.javax.persistence</groupId>-->
        <!--<artifactId>hibernate-jpa-2.1-api</artifactId>-->
        <!--</exclusion>-->
        <!--</exclusions>-->
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.4.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
</dependencies>

登录

13:53:26.337 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'XMLProfileService'
13:53:26.337 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'XMLProfileService'
13:53:26.338 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'XMLProfileService' to allow for resolving potential circular references
13:53:26.339 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
13:53:26.340 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'XMLProfileService'

1 个答案:

答案 0 :(得分:2)

你创建Main2new Main2() cource Spring对此一无所知。 在spring上下文中创建它。将其添加到spring-config.xml

<bean id="main2id" class="Main2">
</bean>

然后找到它。 applicationContext上有一些允许通过id查找bean的方法。您需要将结果投射到Main2课程。并在之后调用你的run方法。