2015-07-03 56 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