Java 未注入的服务和存储库
我对此感到绝望。这是我使用spring的第一天,我无法让它工作——XMLProfileService根本没有注入,它仍然是空的。如果你能给我一些提示,我将不胜感激。我在日志中没有错误 spring config.xmlJava 未注入的服务和存储库,java,spring,Java,Spring,我对此感到绝望。这是我使用spring的第一天,我无法让它工作——XMLProfileService根本没有注入,它仍然是空的。如果你能给我一些提示,我将不胜感激。我在日志中没有错误 spring config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.o
<?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>
Main.java
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
new Main2().run();
}
}
public class Main2 {
@Autowired
private XMLProfileService profileService;
public void run() {
profileService.findByNodeName("z", "b");
}
}
Main2.java
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
new Main2().run();
}
}
public class Main2 {
@Autowired
private XMLProfileService profileService;
public void run() {
profileService.findByNodeName("z", "b");
}
}
pom.xml(依赖项)
您创建的
Main2
是因为cource Spring的new Main2()
对此一无所知。
在spring上下文中创建它。将其添加到spring-config.xml
<bean id="main2id" class="Main2">
</bean>
然后找到它。
applicationContext
上有一些方法允许按id查找bean。您需要将结果强制转换到Main2
类。然后调用您的run
方法。您的Main2
类也是由Spring管理的吗?我很抱歉提出了一些愚蠢的问题,如何在Spring上下文中创建类?(紧接着,我买了一本关于春天的书)。我已经用@Component注释了Main2类,并确保它被Component扫描。我编辑了我的答案以澄清如何将bean添加到上下文中,好吧,以及应该使用自动连接的类是否是JAX-WS类?它不是由Spring管理的,而是直接从J2EE服务器调用的。。在applicationContext中添加此Webservice类没有帮助。我不理解这个问题。把它贴成新的,并详细解释。
<bean id="main2id" class="Main2">
</bean>