setting up JDBCTemplate in Spring using java configuration file - spring

I read in Spring in Action that a good way to set up JDBCTemplate is adding this in the Spring config file:
<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/AOICMainDB" expected-type="javax.sql.DataSource" />
<bean id="jdbcTemplateDB2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dbDataSource" />
</bean>
it makes sense, now we can autowire jdbcTemplateDB2 in a DAO and do jdbcTemplate stuff with it.
but how would I set this up using a java config file? Specifically I'm not sure how the jee: namespace translates over to java confg.

The jee:jndi-lookup is just syntactical sugar for the JndiObjectFactoryBean.
The spring documentation provides a before and after example like so:
Before…​
<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MyDataSource"/>
</bean>
After…​
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
</jee:jndi-lookup>
Now to convert this into a java config you will need to do something like this:
#Bean
public JndiObjectFactoryBean simple() {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("jdbc/MyDataSource");
return bean;
}
Then you can just retrieve the jndi object from the bean.
References:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/xsd-configuration.html#xsd-config-body-schemas-jee-jndi-lookup-environment-single
Disclaimer: The public void afterPropertiesSet() must run to populate the jndi object.

Your question should be: Who is running the JNDI service that will give the data source to my app?
If your app doesn't run on a Java EE app server with a JNDI service available, the answer is "No one." You should use a DriverManager data source in that case.
If your app does run on a Java EE app server with a JNDI service available, you have to know how to set up your data source in the pool.

Related

How to integrate Flyway into our spring-batch jdbc application

We have a spring-batch application with DB2 jdbc data source. Want to add Flyway migration capabilities to our app. I've been exploring this article, it makes perfect sense, except the section that mentions how to specify the 'entityManagerFactory' - their example is for JPA with Hibernate,and it looks like this:
<!-- Entity Manager Factory configuration -->
<bean id="entityManagerFactory" class="o.s.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="o.s.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${jpa.database}"/>
</bean>
</property>
</bean>
Our app is simple JDBC datasource for db2. How can I define that <bean id="entityManagerFactory" so that Spring recognizes it as a managed Bean? Do I even need to specify the entityManagerFactory bean configuration?
No, you don't have to specify the entityMangerFactory bean. The flyway migrations don't have to be beans.
This is an example configuration for flyway migrations:
#Configuration
public class FlywayInitializer {
#Autowired
private DataSource dataSource;
#PostConstruct()
private void startMigrations() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("db/migrations");
flyway.setSchemas("public");
flyway.setSqlMigrationPrefix("H");
flyway.migrate();
}
}
We start by creating a new Flyway object. The javax.Sql.DataSource is the only bean that flyway needs. Flyway needs the data from this bean so it can connect to the database.
Then we configure the locations where the migrations are located, the schemas for flyway (the first schema is the one where the schema_version table will be created) and the migration prefix for the migrations (for example, my migrations look like this: H1__init_db.sql).
There are also a lot of other properties that can be set. Once you are done with configuring the flyway object, you call the migrate method in order to execute the migrations.

Unit Testing based on JNDI , ejb and spring

In my application I am injecting some of services based on EJB with use of Spring IOC through JndiObjectFactoryBean like below mentioned so during run the junit I am getting this exception "java.lang.IllegalArgumentException: This JNDI operation is not implemented by the JNDI provider."
Could some please let me know how I'll configure for Junit.
<bean id="xxxMenuItemService" class="xxxMenuItemServiceyyy">
<property name="xxxMenuItemDelegator" ref="xxxMenuItemDelegator" />
</bean>
<bean id="approveMenuItemServiceRemote"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="ejb/XXXXXXXX" />
have a look at the SimpleNamingContextBuilder from org.springframework.mock as it provides a full context builder where you can bind mock or other objects for use by Spring's JNDI lookup.
One thing to do though is to make sure you build the SimpleNamingContextBuilder in the static #BeforeClass of JUnit 4. this means that it is all initialized and waiting before the Spring Application Context is started and you won't have any JNDI lookup failures.

How would I make this bean in JavaConfig, I dont want to use XML with Spring anymore

I am working on a project and I need to set the following bean and property but I dont want to do it in XML.. I want to do it in JavaCofig style.. Can someone please show me how I would do this in javaconfig stlye
<!-- Spring Configuration needed to avoid URI using dots to be truncated -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
Something like this should work:
#Bean public DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping(){
DefaultAnnotationHandlerMapping bean = new DefaultAnnotationHandlerMapping();
bean.setUseDefaultSuffixPattern(false);
return bean;
}
You can see my sample spring MVC app using code config here https://github.com/robhinds/spring-code-configuration-webapp/blob/master/src/main/java/com/tmm/web/configuration/WebMvcConfiguration.java

Spring : Tomcat datasource via JNDI in my Spring configuration Problem?

I want to read Tomcat datasource via JNDI in my Spring configuration i am using oracle toplink
in spring applicationContext.xml i am using like below
<bean id="UserDatabase" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ISM_rep_user"></property>
<property name="lookupOn" value="true"></property></bean>
and in tomcat/conf/context.xml i am using below
Thanks,
Maybe the lookupOn property has no vaid XML syntax (missing ")?

Configuring Datasource in Spring 3.0

Hello guys I have configured a connection pool and JNDI resource in glassfish 2.1. I can get the Datasource via lookup method in my projects and everything works good. However I decided to try Spring framework and to use my existing connection pool.
In the Spring context file I have the following:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="simpleJdbcTemplate" ref="jdbcTemplate"/>
</bean>
When I deploy the project I get:
java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required]
Is there anything else I have to configure in that file or in any other file in order to get the Datasource?
Presumably, com.mycompany.mavenproject3.Dao extends JdbcDaoSupport, but you're setting a property named simpleJdbcTemplate on it, leading me to believe that you've defined your own property to hold the template since that doesn't exist on Spring's implementation. It's therefore complaining at you because you're required to set either the dataSource property or the jdbcTemplate property of the JdbcDaoSupport object before using it, exactly like it's telling you. Change <property name="simpleJdbcTemplate"... to <property name="jdbcTemplate"....
If your DAO doesn't extend JdbcDaoSupport, then find what does and remove it or set its properties appropriately.
You can also call your datasource directly in your dao bean, don't need to do an another bean for jdbcTemplate. So your context file become something like this:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="dataSource" ref="dataSource"/>
</bean>
After you just have to extends JdbcDaoSupport spring class (in which contain the getter and setter of datasource) on your Dao class.

Resources