java - 服务和存储库未注入(inject)

标签 java spring

我对此彻底绝望了。这是我使用 spring 的第一天,我无法让它工作 - XMLProfileService 根本没有注入(inject),它仍然是空的。如果您能给我一些提示,我将非常感激。我的日志中没有错误。

spring-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'

最佳答案

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

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

然后找到它。 applicationContext 上有一些方法允许通过 id 查找 bean。您需要将结果转换为 Main2 类。并在之后调用您的 run 方法。

关于java - 服务和存储库未注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206249/

相关文章:

java - 无法摆脱: javax. persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

java - 使用 tomcat 数据源 - 如何通过 spring jndi 访问数据源以获取当前数据库池状态

java - 在 header 或 POST 中返回 OAuth 访问 token

java - 在 Java 中放置 SQL 语句的最佳位置

谷歌容器引擎中的java/Kubernetes - 未知主机异常

java - 在 Android 上的 java 中检测内存泄漏

java - 从用户处获取密码值,我想在 url 路径中使用此密码,我使用 thymeleaf 作为模板引擎

java - 将xml解析为java对象

java - 如何在 Java Android 的 onLocationChanged 中播放一次 MediaPlayer?

spring - 在maven中找不到org.springframework.web.servlet