I am running a spring application with xml configuration. I have defined two beans in the application-configuration.xml followed as
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"></property>
</bean>
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
and I am trying to get the bean simply as
public class SQLDbService {
#Autowired
JdbcTemplate jdbcTemplate;
public void someMethod()
{
jdbcTemplate.execute();
}
}
but the jdbcTemplate field is always null.
The application starts from another class
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-configuration.xml");
}
I tried adding
<context:annotation-config/>
to application configuration.
I would like to know if an annotation-based autowiring is possible at this point, if so, what I am missing?
<bean id="sQLDbService" class="path.to.your.package.SQLDbService">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
Related
I'm trying to integrate Mybatis with spring. Here you can see my application context of Spring
<context:annotation-config />
<context:component-scan base-package="com" />
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/DB" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/com/mybatis/mybatis-config.xml" />
<!--<property name="transactionFactory" ref="springManagedTransactionFactory" />-->
</bean>
<!--
<bean id="springManagedTransactionFactory" class="org.mybatis.spring.transaction.SpringManagedTransactionFactory">
<constructor-arg index="0" ref="dataSource" />
</bean> -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="registroClimaMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis.dao.RegistroClimaMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
First of all I have commented springManagedTransactionFactory in XML because it's giving me an exception
Error creating bean with name 'springManagedTransactionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Here is my interface RegistroClimaMapper. I do not have any annotation here because I have defined the context:component scan.
public interface RegistroClimaMapper {
void insertarRegistroClima(RegistroClima registro) throws SQLException;
List<RegistroClima> getRegistrosClima() throws SQLException;
List<RegistroClima> getRegistrosClima(#Param("Validado") boolean Validado) throws SQLException;
}
I try to use this interface in a ManagedBean and when I'm going to use registroClimaPersistence inside a method of TablaRegistroClimaBean I get a NullPointerException
#ManagedBean(name = "tablaRegClimaBean")
#ViewScoped
public class TablaRegistroClimaBean implements Serializable {
#Autowired
private RegistroClimaMapper registroClimaPersistence;
public void setRegistroClimaPersistence(RegistroClimaMapper registroClimaPersistence) {
this.registroClimaPersistence = registroClimaPersistence;
}
}
As seen here: http://javadox.com/org.mybatis/mybatis-spring/1.1.1/org/mybatis/spring/transaction/SpringManagedTransactionFactory.html#SpringManagedTransactionFactory%28%29, SpringManagedTransactionFactory has a default constructor, but none that takes a datasource. You need to pass the datasource to the newTransaction() method, but not to the constructor: http://javadox.com/org.mybatis/mybatis-spring/1.1.1/org/mybatis/spring/transaction/SpringManagedTransactionFactory.html#newTransaction(javax.sql.DataSource,%20org.apache.ibatis.session.TransactionIsolationLevel,%20boolean)
Change the piece of code to
<bean id="springManagedTransactionFactory" class="org.mybatis.spring.transaction.SpringManagedTransactionFactory">
<!--<constructor-arg index="0" ref="dataSource" />-->
</bean>
or completely remove the constructor-arg.
What NullPointerException do you get? It might come from you just defining the RegistroClimaMapper interface, but not implementing this interface in any bean. Please add a Bean that implements this interface and the Autowired annotation should work.
I am trying to learn the Spring framework. I am making a simple test application. I have a Client that calls Service that calls a DAO.
The Client calls the Service like this:
XML
<bean id="myService" class="test.package.service.MyServiceImpl">
<property name="indexFilePath" value="${indexFilePath}" />
<property name="pdfFilePath" value="${pdfFilePath}" />
</bean>
Java
public static void main(String[] args)
{
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = (MyService)context.getBean("myService");
myService.doStuff();
context.close();
}
I want the Service to call the DAO:
XML
<bean id="trackingDAO" class="test.package.dataaccess.TrackingDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${trackingDb.driver}" />
<property name="url" value="${trackingDb.url}" />
<property name="username" value="${trackingDb.username}" />
<property name="password" value="${trackingDb.password}" />
</bean>
My problem is that I have both of these XML pieces in the same file. So I do not know how to access the DAO bean. I do not believe I can open the same context when it is still open in "main". Should I create a second context file? If so, what is a food rule of thumb for when I should create a new context file? I do not like the idea of have many of these. Or perhaps the DAO should be a property of the Service?
<bean id="myService" class="test.package.service.MyServiceImpl">
<property name="indexFilePath" value="${indexFilePath}" />
<property name="pdfFilePath" value="${pdfFilePath}" />
<property name="dao" ref="trackingDAO" />
</bean>
Within your service, add a property of dao, like you mentioned.
public class MyServiceImpl {
....
TrackingDAO dao; //assuming thats the interface , or use Impl.
....
}
Year! U are right "the DAO should be a property of the Service"!
and the piece just need one in U Spring contxt xml file!
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${trackingDb.driver}" />
<property name="url" value="${trackingDb.url}" />
<property name="username" value="${trackingDb.username}" />
<property name="password" value="${trackingDb.password}" />
</bean>
this piece just need one in U Spring contxt xml file!
Try ! it's will work well!
I have the following test:
public class Book
{
public boolean postLoadInvoked;
#PostLoad
private void onPostLoad()
{
postLoadInvoked = true;
}
}
public class MyIntegrationTest extends AbstractIntegrationTest
{
#Autowired
private BookDAO bookDAO;
#Test
public void loadBooks()
{
Book book = bookDAO.findOne(...);
assertTrue(book.postLoadInvoked);
}
}
This test passes as-is, but if I add the #Transactional annotations to the test class, it fails:
#Transactional
#TransactionConfiguration(defaultRollback=true)
public class MyIntegrationTest extends AbstractIntegrationTest
Why does configuring the test with #Transactional affect the JPA callback methods?
EDIT
The DAO is just a Spring Data repository, so has no logic:
public interface BookDAO extends
JpaRepository<Book, Long>,
QueryDslPredicateExecutor<Book> {}
The transaction manager also has a standard configuration:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mangofactory.example</value>
</list>
</property>
<property name="persistenceUnitName" value="spring-test" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
</bean>
I'm not sure what the spring version is that you are using or if AbstractIntegrationTest is extending something, but one thing to check is that in your hierarchy your tests should extend some abstract transactional context aware spring test class.
The classes that I'm thinking about:
org.springframework.test.AbstractTransactionalSpringContextTests
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
or some jUnit flavor (depending on the version that you are using):
org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
org.springframework.test.context.junit38.AbstractTransactionalJUnit38SpringContextTests
I could think of two reasons:
#PostLoad is only executed after the transaction is committed. Unlikely.
How did you enable transactions? Did you specify proxyTargetClass = true? Otherwise, you can get weird behavior.
I'm trying to use TopLink with Spring but I'm having a problem. I'm using it in a webservice (CXF).
When I use getTopLinkTemplate(), the resul is null.
Here is my applicationContext.xml :
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#*************" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.toplink.LocalSessionFactoryBean">
<property name="configLocation" value="toplink-sessions.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="myProductDao" class="ToplinkExecPS.ExecPS">
<property name="sessionFactory">
<ref local="mySessionFactory" />
</property>
</bean>
My code is :
public class ExecPS extends TopLinkDaoSupport{
public Vector Exec(String test)
{
TopLinkTemplate t;
t = getTopLinkTemplate();
if (t == null)
System.out.println("template is null");
}
}
So, when I execute this code, I have "template is null" in the console and a nullPointerException when I try to use the variable "t".
For information, the connection to the database with TopLink is successful.
[TopLink - Infos] : 2012.05.23 03:45:22.113--ServerSession(4812898)--Thread(Thread[main,5,main])--Session - connexion réussie
I have testing so many things and I'm still stuck. I hope you will help me.
[EDIT]
OK I'm really a noob. I just forget to get my bean before call the method...
If anyone get on this thread, there is the code:
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[]{"ToplinkContext.xml"} );
ExecPS exec = (ExecPS)context.getBean("myProductDao");
Not 100% on this one but I would be suspicious of this line:
<property name="configLocation" value="toplink-sessions.xml" />
Unsure toplink-sessions.xml is on your classpath and try the following:
<property name="configLocation" value="classpath:toplink-sessions.xml" />
i have a simple question. Its possible to add dependency injection via #Ressource or #Autowired to the Hibernate Eventlistener?
I will show you my entitymanagerfactory configuration:
<bean id="entityManagerFactory" class="org.hibernate.ejb.EntityManagerFactoryImpl">
<qualifier value="entityManagerFactory" />
<constructor-arg>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager">
<bean
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManagerr">
<property name="defaultDataSource" ref="dataSource" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="mis" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties" ref="jpa.properties" />
<property name="jpaDialect" ref="jpaDialect" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database">
<util:constant
static-field="org.springframework.orm.jpa.vendor.Database.POSTGRESQL" />
</property>
<property name="showSql" value="true" />
</bean>
</property>
</bean>
</constructor-arg>
</bean>
At the moment i register my listener via jpa.properties,
hibernate.ejb.event.load=com.example.hibernate.events.LoadEvent
but in this case i have no spring injection in my listener. I found a solution, but this use the sessionFactory and not the entitymanager oder can i modifiy the sessionfactory in my context? Hopefully someone have a nice idea or solutionhow to deal with this problematic!
Big thanks!
If you used SessionFactory, this would be the configuration:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- Stripped other stuff -->
<property name="eventListeners">
<map>
<entry key="pre-load">
<bean class="com.mycompany.MyCustomHibernateEventListener1" />
</entry>
<entry key="pre-persist">
<bean class="com.mycompany.MyCustomHibernateEventListener2" />
</entry>
</map>
</property>
</bean>
But since you are using JPA, I'm afraid you need to use AOP as outlined in this thread
Or you can
store the ApplicationContext in a ThreadLocal or a custom holder class and expose it through a static method
have a base class for your listeners something like this:
Base class:
public abstract class ListenerBase{
protected void wireMe(){
ApplicationContext ctx = ContextHelper.getCurrentApplicationContext();
ctx.getAutowireCapableBeanFactory().autowireBean(this);
}
}
Now in your lifycycle methods call wireMe() first.
Update:
Here is a sample implementation of ContextHelper:
public final class ContextHelper implements ApplicationContextAware{
private static final ContextHelper INSTANCE = new ContextHelper();
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(final ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public static ApplicationContext getCurrentApplicationContext(){
return INSTANCE.applicationContext;
};
public static ContextHelper getInstance(){
return INSTANCE;
}
private ContextHelper(){
}
}
Wire it in your Spring Bean configuration like this:
<bean class="com.mycompany.ContextHelper" factory-method="getInstance" />