First steps in Spring Framework - java

I have downloaded Spring Framework and using SpringSource dm Server. I just need to write a simple Hello world using Spring Framework. I don't know about the configuration and where to write the JAVA code.
I did a google, but could not find anything that can help me out.

This is a very simple tutorial that outlines a basic setup (although using tomcat).
Edit: Another good example from a different question.

Ummm... "Hello world" programs typically demonstrate languages which Spring is not. What problem are you trying to solve?

It's easy! in 5 steps you can run the hello world using Spring
1. Set up a Maven Project
2. Add Spring Dependency
3. Create the Spring Bean Configuration File
4. Create a Spring Bean
5. Run the Maven Project
Create a Hello class under /src/main/java directory to test the project.
public class Hello {
#SuppressWarnings("resource")
public static void main(String[] args) {
// loading the definitions from the given XML file
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorldService service = (HelloWorldService) context
.getBean("helloWorldService");
String message = service.sayHello();
System.out.println(message);
//set a new name
service.setName("Spring");
message = service.sayHello();
System.out.println(message);
}}
Right click Hello.java and run it as Java Application. The result is:
Hello! Program Creek Readers
Hello! Spring
More details see this tutorial
here others tutorials the Hello World with Spring
Spring 3 hello world example
Spring MVC hello world example
If you wish to learn more,you can see, this tutorials the MKYONG
In this series of tutorials, it’s provides many step by step examples and explanations on using the Spring framework.
Spring Core Core support for dependency injection, transaction management, web applications, data access, messaging, testing and more.
Spring MVC
, a Java Model-View-Contraller (MVC) web framework, which builds on top of the Spring Inversion of control(IoC) framework.
Spring Security, is a flexible and powerful authentication and access control framework to secure Spring-based Java web application.
Spring Data Mongo DB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities.
Spring Batch, is an open source framework for batch processing – execution of a series of jobs. Spring Batch provides classes and APIs to read/write resources, transaction management, job processing statistics, job restart and partitioning techniques to process high-volume of data.
And here, Introduction to the Spring Framework

Get hold of Spring Manning in Action book (google may give it to you or its worth buying if you plan to work on spring more). It will explain to you properly how to start

Related

How to integrate Olingo(Odata) in Java SpringBoot Project

Can anyone please help me on how to integrate Olingo (Odata) in a Springboot Java Appln.
I'm pretty new to Spring boot and have implemented one project and wanted it to convert to Oling (Odata).
I have gone through various resources but with a bunch of different approaches not sure how to do it the correct way.
Please let me know if some has worked on it and can guide me.
link to the project on which I applied spring-boot.
If you are trying to integrate OlingoJPA there are a couple of things you will have to do
Implement JPAServiceFactory and initializeODataJPAContext, basically this is about defining the persistence unit and entity manager
Then you can create a Spring Boot Configuration to mount your OData endpoint and initialize the EntityManagerFactory
Then you can point to your database here
An finally you can define the JPA entities you want your service to expose
The full Spring Boot + JPA project sample is located Github. Feel free to go though it, raise an issue or submit a Pull request for impalements

Apache Shiro in Spring Boot Desktop Standalone Application

I once successfully implemented Apache Shiro in a desktop standalone application (without Spring framework or any framework for that matter).
I used INI file to get SecurityManager like so:
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:resources/shiro.ini");
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
I read in one of stackoverflow post that it is not advisable to use shiro INI when using spring.
In my case now I use Spring Boot with Spring Data JPA with Hibernate in this application. What is the best way to configure Apache Shiro so that I can maximise the use of this security framework.
Edit
All examples i have come across, shows examples for web. I need specifically for standalone application. Examples will highly be appreciated.
Solution
After alot of learning and answers from this post, i managed to come up with this solution and created github repo. If anyone wishes to make it better, be my guest.
As I understand doesn't mutter if you are developing a web-based application or a standalone, unless for the webfilters official Apache shiro doc as you can see here this example shows how to properly configure apache shiro for both, web-based and standalone.
You can still use xml-based configurations,as is described here with spring boot and it's easy migrate configuration from INI files to XML using tags like or constructor-args as shown here spring xsd doc
In my opinion use INI file or XML-based configurations has smiilar advanteges and disadvantages, in both methods you can "hot-swap" beans configurations.
For instance if it's easy for you use Java or configurate Shiro on code you can check this github link where you can see an example Class configuration for apache Shiro spring-boot application.
Forget about the web filters and configure the others beans as on this links it should work fine.
As far as i know there is no a good or a worng way talking about use xml or class configurations with spring boot, it just depend on what your needs are.
To finish I code a simple example Spring-boot-standalone with shiro. for you to demostrate an XML-based configuration, this prints the realm bean to show that the context is up and has commented code to fulfill with your needs the rest must be compleate beans with the shiro webpage tutorial.
Greatings.
You can use a yml file to add shiro configurations along with you the application properties.
As an example, take a look at https://github.com/johntostring/spring-boot-shiro

Spring Boot: web-app + webservice

Need to create a webapp in java using spring boot which has UI and also it functions as a webservice. (web-app + webservice using spring boot). How to do the configurations ?
I think that this question is a bit wide. You should start by reading some tutorial like :
http://projects.spring.io/spring-boot/
I don't really know a good option to do UI with spring boot, but maybe look at :
https://spring.io/guides/gs/crud-with-vaadin/
You will need to learn and read a bit but I hope it helped you :)
EmCode
Select the SpringBoot example that is most close to your problem space and extend. You can find the samples Here.
Alternatively you can use the Spring Initializr
This is an example of Web app from the original spring page:
https://spring.io/guides/gs/spring-boot/
And this for webservices
https://spring.io/guides/gs/actuator-service/
You only need to start from the first example and add configuration in the 2nd

Spring Boot and Spring Data application with multiple DataSources created in runtime

I am developing a Spring Boot application that uses Spring Data JPA and will need to connect to many different databases e.g. PostreSQL, MySQL, MS-SQL, MongoDB.
I need to create all datasources in runtime i.e. user choose these data by GUI in started application:
-driver(one of the list),
-source,
-port,
-username,
-password.
And after all he writes native sql to choosen database and get results.
I read a lot of things about it in stack and spring forums(e.g. AbstractRoutingDataSource) but all of these tutorials show how to create datasources from xml configuration or static definition in java bean. It is possible to create many datsources in runtime? How to manage transactions and how to create many sessionFactories? It is possible to use #Transactional annotation? What is the best method to do this? Can someone explain me how to do this 'step by step'?
Hope it's not too late for an answer ;)
I developed a module which can be easily integrated in any spring project. It uses a meta-datasource to hold the tenant-datasource connection details.
For the tenant-datasource an AbstractRoutingDataSource is used.
Here you find my core implementation using the AbstractRoutingDataSource.
https://github.com/Dactabird/multitenancy
Here is an example to show how to integrate it. https://github.com/Dactabird/multitenancy-sample
In this example I'm using H2 embedded db. But of course you can use whatever you want.
Feel free to modify it for your purposes or to ask if questions are left!

Grails app using services from spring-mvc backend

We have a fairly involved web application written using spring-mvc with a maven build system and would like to harness all the power of Grails for the front end.
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data.
I need some guidance with my architectural approach to this integration at a high level.
From my understanding, I will need to;
- add my spring-mvc app as a compile dependency in my BuildConfig.groovy.
- Expose the service layer objects as service beans in my conf/spring/resources.groovy and inject them into my controllers
Questions:
My spring-mvc app has lots of dependencies of its own (which it obviously has to have) which are causing lots of dependency errors. Should I be setting "transitive=false" in my config and calling all of these in my Grails app?
How should the datasource get configured? I guess I have to integrate the applicationContext of my spring-mvc app by calling it from my Grails applicationContext and hope it all bootstraps nicely?
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data
Can you be a bit more specific about which components of the Spring MVC you want to use from Grails, is it just the services and datasource?
I will need to add my spring-mvc app as a compile dependency in my BuildConfig.groovy
yes
Expose the service layer objects as service beans in my conf/spring/resources.groovy
Although you could make the Spring beans known to your Grails app by defining them individually in resources.groovy, this is unnecessary because you've already defined them in an Spring XML file (presumably) in the Spring MVC project.
Instead you can use the importBeans method of the BeanBuilder to import the Spring beans defined in this XML file into the Grails app. Once you've added the Spring MVC project as a dependency of your Grails app, the Spring XML file should be on your classpath, so all you need to do is add the following to resources.groovy
beans = {
importBeans('classpath:/path/to/file/applicationContext-services.xml')
}
How should the datasource get configured?
A Spring bean named dataSource defines the datasource that a Grails app uses. In a standard Grails app, this bean is created based on the configuration in DataSource.groovy. If your Spring MVC app defines a bean with this name, then this should be used instead after making the changes above. To be sure that Grails is using the datasource from your Spring MVC app rather than whatever is in DataSource.groovy, I guess you could delete the contents of the latter.

Categories