I opened a question some hrs ago, which got marked as a duplicate, however it was not a duplicate of the marked question, whatever.
Since then, I managed to get some things done, and work stuff out, so here is my question:
I was trying to @Autowire a spring bean into another one, however my problem was that the @Autowired field was always null, depsite of the fact that they were both managed beans and worked correctly by themselves.
I found out, that you can reach the ApplicationContext in a bean by implementing the ApplicationContextAware interface, which I did. It was called and the right context was given to it.
This way, I can call getBean() on the context, which returns the bean I wanted to @Autowire in the first place, which is a yaay, but this seems like a workaround for a bigger problem.
Can you help me what might be wrong? I tried @Autowiring both as field, method and constructor parameter, none worked. These beans are both singleton beans and are used as basis for HesianServiceExporter, in order to reach them from another servlet.
I guess is I'm missing some crucial config information here, but I can't see why a field injection would not work, while at the same time an interface implementation does. Is it a case, where the bean which I want to @Autowire is not ready yet, so it can't be injected?
As some of you requested, here is my code. Don't know how it helps, but:
@Service(Persistence.NAME)
public class PersistenceBean implements Persistence, ApplicationContextAware {
ApplicationContext context;
@Override
public User getUser() {
User user = new User();
user.setEmail(context.getBean(HelloWorld.class).getText());
return user;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
And my context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="hu.bme.sch.qpa" annotation-config="true"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="packagesToScan"
value="hu.bme.sch.qpa.global.entities"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQL95Dialect
</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="username" value="qpapp_server_user"/>
<property name="password" value="root"/>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="persistenceExceptionTranslationPostProcessor" class=
"org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
And lastly, my ApplicationInitializer is annotated like this:
@ImportResource("/WEB-INF/app-core-servlet.xml")
@SpringBootApplication(scanBasePackages = "hu.bme.sch.qpa")
@EnableWebMvc
@Configuration
@EnableAutoConfiguration
public class CoreStarter extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(CoreStarter.class, args);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
}
}
And here is HelloWorld bean, which is (what the name would suggest) a really simple bean. It's in the same package as PersistenceBean:
@Service(HelloWorld.NAME)
public class HelloWorldBean implements HelloWorld {
@Override
public String getText() {
return "Hello World!!!! I'm remoted";
}
}
Thanks in advance.
HelloWorldwith package to which that class belongs? - Ivan