Where to define the Datasource for a web application? - spring-mvc

I am developing a web application based on Spring MVC framework. In this application I need to persist some data to DB.
I intend to use Spring data JPA as well. Now where is the best place to have the Datasource configured? I intend to deploy this in Apache tomcat.
I guess we have two places:
1) Define in the spring configuration file, like below:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:tcp://localhost:9092/~/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
OR
2) Define in the Tomcat.
Is my understanding correct? Are there any difference in approaches in #1 and #2?
If we use #2, can Spring do dependency injection to the Datasource when needed by the application? OR can we reference the Datasource by JNDI lookup in this scenario?
I am learning this of my own, to understand how real life applications work; so any deeper insight would be of great help.

I have personally gone back and forwards between setting the datasource as JNDI and defining it in the configuration.
I came to the conclusion that I prefer defining the datasource in the configuration for a couple of reasons:
Using JNDI I would need to add the mysql-connector jar to the tomcat lib. And I don't like to have to change the running environment to cater for a specific app.
Doing the configuration in the web application, I can use annotation to configure, and no XML
I think is correct to say that is the application that needs to know how to connect to the database, not necessarily the container

Related

ASP.NET Core How to read config properties as I do in a Spring?

I'm very newbie with ASP.NET, but not with Spring (Java).
I was wondering how the ASP.NET Core applications are normally configured ? The place I used before for my config in a web applications was application.properties (or others under /src/main/resources cause I used allways maven) and later I just had to add #Configuration Bean referencing the attributes in the POJO.
So is there a similiar way to do config stuff onLoad time on ASP.NET ? (easily)
How do you normally configure a ASP.NET or Where do you put the configuration files ?
How is the right way to do it ? Do you normally have a schema of files and directories to follow ?
Try this:
Class
using System.Configuration;
private string varName = ConfigurationManager.AppSettings["key"];
Web.config
<appSettings>
<add key="key" value="vaue" />
</appSettings>
Bit of a late answer here, but the approach in ASP.NET Core is to use .NET Configuration abstractions, including IOptions which is detailed on the same page. There are providers for application.json, environment variables and a variety of other options.
Coming from Spring, you may also be interested in config providers offered by Steeltoe, probably most notably options for using Spring Cloud Config Server

XML-based Spring MVC argument resolver

I'm trying to migrate a big web app from Spring 2.5 to Spring 4. The whole context and MVCs are being configured using XML files, not annotated. Does anybody know if it's possible to declare argument resolvers to MVC beans injected using XML files? When using annotated controllers, the solution is to inject argument resolvers:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.company.CustomResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
But I can't implement this solution because, due to size and legacy reasons, I can't migrate the app whole set of MVCs to annotated controllers.

Does Mule supports Spring MVC?

I have web app which is already developed based on Spring MVC. I need to re-implement that web app in Mule.
Can I develop the mule application based on Spring MVC?
Can I declare the Spring MVC dispatcher servlet inside a mule's servlet endpoint and take things further from there?
The web app has web.xml where it defines the DispatcherServlet, the contextparams, the listener classes and so on. How can we remodel that in a mule application?
Any examples where a mule application is developed based on SpringMVC would be great.
Thanks to its embedded Jetty container, you can deploy any JavaEE web application in Mule. So there's no need to remodel anything.
The "Boosktore" example application demonstrates running web-apps within Mule: https://github.com/mulesoft/mule/tree/mule-3.x/examples/bookstore
Mule ESB is not an MVC Framework. It is developed using enterprise integration patterns in mind.
Please go through this blog, to know when to use ESB.
spring mvc can be integrated with mule.
Define all your spring related configuration in separate xml file and include it in mule configuration file.
You can write your custom transformers ,in the custom transformations you can inject or do an autowire of your service classes and from service object you can interact with dao layers.

Spring 3 component scan project scope

I have a web app that depends on multiple internal maven projects, can one wire components from other projects via component scan ?
For example :
in the application context xml of the web app project
<context:component-scan base-package="com.mycompany.app" />
<context:component-scan base-package="com.mycompany.projectX.service.impl"/>
<context:component-scan base-package="com.mycompany.projectY.service.impl" />
Yes, and that is how you do it. However, if there is an ambiguity, you might get into trouble. For example, there are several implementations of the same interface that you want to wire.

Asp.net sql cache dependency programmatically configuration

I've a question regarding SqlCacheDependency configuration. Every resource I've read requires the developer to define the polled databases into web.config.
This is not a good scenario for our application that connects to different databases, because we don't want to edit web.config each time we add / remove a database.
We tried to configure it using SqlCacheDependency and SqlCacheDependencyAdmin in application startup but it seems that you must anyway configure everything in web.config.
Do you know a possible solution?
Thanks,
Marco

Resources