background preloader

Spring

Facebook Twitter

Spring Rest API with Swagger – Exposing documentation. Once you create API documentation it is important to make it available to the stakeholders.

Spring Rest API with Swagger – Exposing documentation

In ideal case, this published documentation would be flexible enough to account for any last-minute changes and also be easy to distribute (in terms of costs as well as time needed to accomplish this). To make this possible we will make use of what was accomplished in my previous post detailing the process of creation of API documentation. Using Swagger UI module in combination with published API documentation in json allows us to create simple HTML documentation that may be used to interact with the APIs as well. Integration with Swagger UI Makers of Swagger UI describe it as a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation and sandbox from a Swagger-compliant API. Spring Dependency Checking Example. This is an example of how to check dependencies in a Spring Bean.

Spring Dependency Checking Example

A Spring Bean can have dependencies of any type, such as primitive types, Collections or even Objects that are references to other beans. Sometimes it is necessary that specific dependencies have to be set, so that a Spring Bean is instantiated correctly. Spring provides developers the ability to check if all mandatory dependencies of a Spring Bean have been set, either by using the @Required annotation or by defininig a custom @Required-style annotation by themselves. Furthermore, older Spring releases have offered developers the ability to check dependencies in different modes, via XML-configuration. This way is though deprecated now. Captain Debug's Blog: Using Spring Profiles in XML Config. My last blog was very simple as it covered my painless upgrade from Spring 3.0.x to Spring 3.1.x and I finished by mentioning that you can upgrade your Spring schemas to 3.1 to allow you to take advantage of Spring’s newest features.

Captain Debug's Blog: Using Spring Profiles in XML Config

In today’s blog, I'm going to cover one of the coolest of these features: Spring profiles. But, before talking about how you implement Spring profiles, I thought that it would be a good idea to explore the problem that they’re solving, which is need to create different Spring configurations for different environments. This usually arises because your app needs to connect to several similar external resources during its development lifecycle and more often and not these ‘external resources’ are usually databases, although they could be JMS queues, web services, remote EJBs etc. Secondly, there are environment specific bean classes such as data sources, which usually differ depending upon how you’re connecting to a database. Spring Profiles and Java Configuration. My last blog introduced Spring 3.1’s profiles and explained both the business case for using them and demonstrated their use with Spring XML configuration files.

Spring Profiles and Java Configuration

It seems, however, that a good number of developers prefer using Spring’s Java based application configuration and so Spring have designed a way of using profiles with their existing @Configuration annotation. I’m going to demonstrate profiles and the @Configuration annotation using the Person class from my previous blog. Asynchronous Spring Service. How to create dynamic proxy with Spring and Java. Java - How can I create proxy without using spring AOP. Spring Auto Proxy Bean – BeanNameAutoProxyCreator. The BeanNameAutoProxyCreator automatically creates AOP proxies for beans with names matching literal values or wildcards.

Spring Auto Proxy Bean – BeanNameAutoProxyCreator

As with ProxyFactoryBean, there is an interceptorNames property rather than a list of interceptor, to allow correct behavior for prototype advisors. Named “interceptors” can be advisors or any advice type. As with auto proxying in general, the main point of using BeanNameAutoProxyCreator is to apply the same configuration consistently to multiple objects, and with minimal volume of configuration. Spring Scoped Proxy Beans – An Alternative to Method Injection. Few months back, I wrote on JavaLobby about how you can use Method Injection in scenarios where bean lifecycles are different i.e. you want to inject a non-singleton bean inside a singleton bean.

Spring Scoped Proxy Beans – An Alternative to Method Injection

For those of you who are not aware of Method Injection, it allows you to inject methods instead of objects in your class. Method Injection is useful in scenarios where you need to inject a smaller scope bean in a larger scope bean. For example, you have to inject a prototype bean inside an singleton bean , on each method invocation of Singleton bean. Just defining your bean prototype, does not create new instance each time a singleton bean is called because container creates a singleton bean only once, and thus only sets a prototype bean once. So, it is completely wrong to think that if you make your bean prototype you will get new instance each time prototype bean is called. RequestProcessor class. Secrets of the Spring AOP Proxy - Intertech Blog.

By Jim White (Director of Training and instructor) Spring Aspect Oriented Programming (AOP) is a powerful mechanism to weave cross cutting concerns like security, transactions, exception handling, logging, etc. into business code ("core concerns") without explicitly adding calls to the cross cutting concern.

Secrets of the Spring AOP Proxy - Intertech Blog

This allows the cross cutting concern to be updated, removed, swapped out, and generally maintained in a much simpler fashion. AOP Under the Covers – the Proxy Having said that, because of the way AOP is implemented, there can seem to be a certain amount of mystery and magic at work. Your code never invokes the advice methods of an aspect class. 3.13 BeanPostProcessors & BeanFactoryPostProcessors. This example shows a very simple BeanPostProcessor that takes a value from an int field and doubles it if the getter or setter is marked with the @Doubler annotation. chapter03-post-processor/src/main/java/org/springbyexample/springindepth/chapter03/postProcessor/DoublerBean.java /** * Doubler bean. */ public class DoublerBean { protected int value = 0; /** * Gets value. */ @Doubler public int getValue() { return value; } /** * Sets value. */ public void setValue(int value) { this.value = value; } } The DoublerBeanPostProcessor implements the Ordered interface configured with the lowest order of precedence.

3.13 BeanPostProcessors & BeanFactoryPostProcessors

The postProcessBeforeInitialization method loops through all methods using Springs ReflectionUtils and checks for the presence of the @Doubler annotation on the method. If it finds a method with the annotation, it assumes it is an int with a getter and setter. Chapter03-post-processor/src/main/java/org/springbyexample/springindepth/chapter03/postProcessor/DoublerBeanPostProcessor.java. Spring BeanPostProcessor. Fecha de publicación del tutorial: 2014-01-07 Índice de contenidos 1.

Spring BeanPostProcessor

Introducción 2. Spring Remoting with Security and SSL. One of my favorite features of the Spring Framework is the Spring Remoting part, which enables you to expose any bean in a Spring Application Context as a remote service over HTTP.

Spring Remoting with Security and SSL

It’s fast, it’s easy, and it’s really, really simple. Basic Spring Remoting Configuration In the general situation all you need to do is create a DispatcherServlet (just as you would with any Spring MVC application), add an Exporter on the server side and reference a ProxyFactoryBean on the client. On the server side: demo-servlet.xml - exposes bean 'helloService' as remote service <bean name="/hello" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"><property name="service" ref="helloService" /><property name="serviceInterface" value="se.jayway.demo.server.HelloService" /></bean>

Customize Spring Social Connect Framework For MongoDB. In my previous post, I talked about the first challenge I had was to change the data model and add the connection framework. Here I want to give more details about how I did it. Spring Social project already provides a jdbc based connection repository implementation to persist user connection data into a relational database. However, I’m using MongoDB, so I need to customize the code, and I found it is relatively easy to do it. The user connection data will be saved as the object of UserSocialConnection, it is a MongoDB document: BaseEntity just has ‘id’. After we have our database UserSocialConnectionRepository, we will implement Spring Social required ConnectionRepository and UsersConnectionRepository.

MongoUsersConnectionRepository is pretty much exactly like JdbcUsersConnectionRepository. First, I replaced JdbcTemplate with UserSocialConnectionRepository to retrieve UserSocialConnection objects from the database. Spring Integration - Application from scratch, Part 1. Before we start In this tutorial you will learn what is Spring Integration, how to use it and what kind of problems does it help to solve. We will build a sample application from the scratch and demonstrate some of the core components of Spring Integration. If you’re new to Spring check out another tutorial on Spring written by me – Shall we do some Spring together?

Also note that you don’t need any special tooling, however you can get the best experience for building Spring Integration applications either with IntelliJ IDEA or Spring Tool Suite (you can get some fancy-looking diagrams with STS). You can either follow this tutorial step-by-step and create application from the scratch yourself, or you can go ahead and get the code from github: DOWNLOAD SOURCES HERE: JAX-WS Spring Integration Example. This is an example that demonstrates how you to use JAX-WS to create a simple Web Service and use Spring Framework to perform Dependency Injection with a bean. There are three ways of exposing Spring POJO services as a JAX–WS web services : Exposing Servlet – based web services (suitable for Java EE 5 environments)Exporting standalone web services (suitable when utilizing Sun’s JDK 1.6 build–inJAX–WS provider)Exporting web services using the JAX–WS RI’s Spring support (similar to the standalone approach, but this time in a Servlet environment.

Suitable for non – Java EE environments such as Tomcat, embedding the JAX–WS RI as part of the web application) In this example we are going to follow the third approach. Sun’s JAX–WS RI, developed as part of the GlassFish project, ships Spring support as part of its JAX–WS Commons project. MongoDB: Add A CounterWithSpring Data. In my blog app, you can view any user’s profile, for example, my profile page will be and ‘user1′ is my user Id in the system. In MongoDB, every document object will have a unique identifier, and often we store it as String, so I have a BaseEntity class for that: But the system generated id usually is very long, and I want to generate my own userId in my UserAccount class: The generated userId is very simple, just ‘user’ with a sequence number, for example, I’m the first user, so my userId is ‘User1′, and the next signed up user will be ‘User2′, etc. I want a sequence number generator from MongoDB to give me unique sequence numbers.

The operations are to return current sequence number and also increase the sequence number in the database. Solr, as a Spring Data Module. At the end of October, I attended a 3-days Solr training. It was very interesting, in light of the former Elastic Search talk I attended mid-year. As an old Spring Data fan, when I found out Spring Data offered a Solr module, I jumped at the chance to try it. In this case, I’m well aware that an abstraction layer over Solr doesn’t mean we can easily change the underlying datastore: Solr is designed as an inverted index, while other Spring Data modules are much more specialized (JPA, MongoDB, etc.). Email filtering using Aspect and Spring Profile. During web application development, often the need for sending emails arise.

However, sometimes the database is populated by data from production and there is a risk of sending emails to real customers during email test execution. This post will explain how to avoid it without explicitly write code in the send email function. We would use 2 techniques: Spring Profiles – a mechanism to indicate what the running environment is (i.e. development, production,..)AOP – in simplified words its a mechanism to write additional logic on methods in decoupled way. I would assume you already have Profiles set on your projects and focus on the Aspect side. In that example the class which sends emails is EmailSender with the method send, as specified below: Now, we will add the logic which prevent sending email to customers where code is not running on production.

Java - how to read System environment variable in Spring applicationContext. 6 Tips for Managing Property Files with Spring. What could be simpler than property files? In an enterprise application, it turns out, many things! In this post I’ll take a look at a few subtle complexities to managing properties in an enterprise Java Spring application, and hopefully demonstrate how a little fore-thought in design can yield big savings in terms of time, confusion, bugs, and gray hair down the road. Types of Properties Before designing an approach to managing property files, it's critical first to understand the types of properties are to be managed.

How to access Environment Variables in the Spring IoC Configuration. UPDATE: For a more modern version, please check here. So for the past few hours, I have spent some time attempting to figure out how my Spring applicationContext file can resolve environment variables. First off, let me start by saying that I HATE using environment variables, especially when Java provides you with a simple method to define your own variables. However, I had no control over this requirement, so off to work… THE PROBLEM: We have a simple properties file used to dynamically define the Data Source. Erwin Vervaet: Spring bean definition profiles and web.xml. Spring 3.1 was released a few weeks ago. One of the interesting new features is the first-class support for bean definition profiles: you can define beans as part of a profile and than activate that profile if required. Using Spring Profiles and Java Configuration. Example of PropertyOverrideConfigurer in Spring (context:property-override) Reading properties files in Spring with PropertyPlaceholderConfigurer bean.

Details Last Updated on 08 April 2014 Table of content: Spring Test DBUnit - Spring DBUnit provides integration between the Spring testing framework and the popular DBUnit project. It allows you to setup and teardown database tables using simple annotations as well as checking expected table contents once a test completes. RESTful API Documentation Using Swagger and Spring MVC. With the advancement of web technologies and different ways to implement them, there have been many different schools of thought about how to make it easy for end users and to address different challenges into the space of web. With this the inception of web-services proliferated majorily into two prime categories REST and SOAP. Annotation scanning via Spring BeanPostProcessor. Simple Spring HTTP Remoting Example. Zen in the art of IT: Creating your own custom Spring configuration.

Custom components with Spring 2.5. Generate xsd from xml with trang. Creating a Custom Spring 3 XML Namespace.

Cache

Integration. Java Dev world. How to programmatically define Spring beans. Boot-reference. AOP. Spring In-depth, In Context. Automatically Inject Mocks into Spring Context. Spring MVC Handler Interceptor. Creating Spring FactoryBeans for Mock Objects. Spring Framework Reference Documentation. Access the concrete class from a spring proxy.