2015-07-03 88 views
2

我對此完全絕望。這些是我在春季的第一天,我無法正常工作 - 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' 
+0

是你管理的'Main2'類嗎? – asgs

回答

2

創建Main2爲五言春天的new Main2()對它一無所知。 在春天的環境中創建它。將此添加到spring-config.xml中

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

然後找到它。在applicationContext上有一些方法可以通過id找到bean。你需要將結果投到你的課程Main2。並在之後調用您的run方法。

+0

我很抱歉有一些愚蠢的問題,在Spring的上下文中如何創建類? (之後立即購買一本關於春天的書)。我用@Component註釋了Main2類,並確保它通過組件掃描進行掃描。它不起作用.. – Ladiko

+0

我編輯了我的答案,以澄清如何將bean添加到上下文, – talex

+0

好吧,如果應該使用自動裝配的類是JAX-WS類?它不是由Spring管理的,而是直接從J2EE服務器調用的。在applicationContext中添加這個Webservice類沒有幫助。 – Ladiko