我對此完全絕望。這些是我在春季的第一天,我無法正常工作 - XMLProfileService根本沒有被注入,它仍然是空的。如果你能給我一些提示,我將非常感激。我在日誌中沒有錯誤。未注入的服務和存儲庫
彈簧-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'類嗎? – asgs