Spring – Java Based Configuration

BasicConfig.java


package com.learningtask.init;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan(basePackages ="com.learningtask")
@EnableWebMvc
public class BasicConfig extends WebMvcConfigurerAdapter{

	@Bean
	public UrlBasedViewResolver setupResolver()
	{
		UrlBasedViewResolver resolver = new UrlBasedViewResolver();
		resolver.setViewClass(JstlView.class);
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}
}

MyWebApplicationInitializer.java


package com.learningtask.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;

import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MyWebApplicationInitializer implements WebApplicationInitializer{

	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
		appContext.register(BasicConfig.class);
		servletContext.addListener(new ContextLoaderListener(appContext));
		ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",new DispatcherServlet(appContext));
		dispatcher.addMapping("/");
		dispatcher.setLoadOnStartup(1);

	}

}

HomeController.java


package com.learningtask.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
	@RequestMapping("/")
	public String home()
	{
		System.out.println("Home Controller");
		return "home";
	}
	
	
}

Spring -XML configuration in Maven

Spring Dependencies


<!-- Spring Dependency -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.4.RELEASE</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.4.RELEASE</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.4.RELEASE</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
			<scope>runtime</scope>
		</dependency>

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LearningTask-webapp</display-name>
  
  <welcome-file-list>
  <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springDispatcher-servlet.xml</param-value>
  </context-param>	
  
  
  <servlet>
      <servlet-name>springDispatcher</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>springDispatcher</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   
  
</web-app>

springDispatcher-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans     
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/mvc
			   http://www.springframework.org/schema/mvc/spring-mvc.xsd
			   http://www.springframework.org/schema/data/jpa
     		   http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
     		   http://www.springframework.org/schema/aop 
    		   http://www.springframework.org/schema/aop/spring-aop.xsd" >
	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.sridhar.controller" />

<!-- Enables the Spring MVC @Controller programming model -->
<!-- <mvc:annotation-driven /> -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	      <property name="prefix" value="/WEB-INF/views/" />
	      <property name="suffix" value=".jsp" />
	   </bean>

</beans>

HomeController.java


package com.sridhar.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

	@RequestMapping("/")
	public String home()
	{
		System.out.println("Home Controller");
		return "home";
	}
}

Spring Dependencies

<!-- Shared version number properties -->
<properties>
    <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
</properties>

<!--
    Core utilities used by other modules.
    Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Expression Language (depends on spring-core)
    Define this if you use Spring Expression APIs (org.springframework.expression.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!-- 
    Bean Factory and JavaBeans utilities (depends on spring-core)
    Define this if you use Spring Bean APIs (org.springframework.beans.*) 
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans)
    Define this if you use Spring AOP APIs (org.springframework.aop.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) 
    This is the central artifact for Spring's Dependency Injection Container and is generally always defined
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration
    Define this if you need any of these integrations
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context)
    Define this if you use Spring Transactions or DAO Exception Hierarchy
    (org.springframework.transaction.*/org.springframework.dao.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx)
    Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
    (depends on spring-core, spring-beans, spring-context, spring-tx)
    Define this if you need ORM (org.springframework.orm.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans.
    (depends on spring-core, spring-beans, spring-context)
    Define this if you need OXM (org.springframework.oxm.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Web application development utilities applicable to both Servlet and Portlet Environments
    (depends on spring-core, spring-beans, spring-context)
    Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
    Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
    Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*)
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc-portlet</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<!--
    Support for testing Spring applications with tools such as JUnit and TestNG
    This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs
-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${org.springframework.version}</version>
  <scope>test</scope>
</dependency>

BeanFactory vs ApplicationContext

  • Spring provides two kinds of IOC container, one is BeanFactory and the other is ApplicationContext.

  • Syntactically BeanFactory and ApplicationContext both are Java interfaces and ApplicationContext extends BeanFactory.
  • Both of them are configured using XML configuration file.
  • Both BeanFactory and ApplicationContext provides a way to get a bean from Spring IOC container by calling getBean(“bean name”), but there is some difference in their working and features provided by them.

BeanFactory

ApplicationContext

BeanFactory instantiate bean when you call getBean() method. ApplicationContext instantiate Singleton bean when the container is started
BeanFactory doesn’t provide support for internationalization i.e.(i18n). ApplicationContext provides support for it.
 Implementation of BeanFactory interface is XMLBeanFactory Implementation of ApplicationContext interface is ClassPathXmlApplicationContext.
If you are using auto wiring and using BeanFactory then you need to register AutoWiredBeanPostProcessor using API. Which is configured in XML.
BeanFactory is OK for testing and non production use. ApplicationContext is more feature rich container implementation and should be favored over BeanFactory.

Example:

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));

ExamMark mark = (ExamMark)factory.getBean("exammark");

Example:

ApplicationContext ctx = new ClassPathXmlApplicationContext(“spring.xml”);

ExamMark mark = (ExamMark)ctx.getBean(“exammark”);

Insert using JDBCTemplate ( inject Datasource ) – @Repository Annotation

DAO.java


package com.sridhardao;
 
import javax.sql.DataSource;
 
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
@Repository("dao")
public class DAO {      
     
    private DataSource dataSource;
     
    private JdbcTemplate jdbcTemplate ;
     
    @Autowired
        public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
 
    public void save(){
         
               jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?,?)" , 105, "Subash");    
         
    }
}

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
               http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
     
         <context:component-scan base-package="com.sridhar.dao" /> 
         <context:annotation-config/>
  
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/testdb" />
        <property name="username" value="root" />
        <property name="password" value="pwd" />
    </bean>
     
   <!-- <bean id="dao" class="com.sridhar.dao.DAO">
         <property name="dataSource" ref="dataSource" /> 
    </bean> -->
    
</beans> 

Testing:


import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestEmployeeService {
 
    public static void main(String[] args) {
      
        AbstractApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
         
        DAO dao = (DAO)context.getBean("dao");
        dao.save();
        context.registerShutdownHook();
}
}

Constructor Injection

spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="exammark" class="com.sridhar.spin.ExamMarks">
        <!-- Setting values by constructor Injection -->
        <constructor-arg value="CSE1002" />
        <constructor-arg value="89" />
        <constructor-arg value="95" />
 
    </bean>
</beans>

Student.java


package com.sridhar.spin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Student {
 
    public static void main(String[] args) {
 
         
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        ExamMarks mark = (ExamMarks)ctx.getBean("exammark");
 
        mark.displayMarks();
 
    }
}

ExamMarks.java

package com.sridhar.spin;
 
public class ExamMarks
{
    private String studentId;
    private int Sub1;
    private int Sub2;
     
//Constructor
    public ExamMarks(String studentId)
    {
        this.studentId = studentId;
         
    }
     
    public ExamMarks( String studentId, int sub1, int sub2)
    {
        this.studentId = studentId;
        this.Sub1 = sub1;
        this.Sub2 = sub2;
    }
     
    public int getSub1() {
        return Sub1;
    }
 
 
    public void setSub1(int sub1) {
        Sub1 = sub1;
    }
 
 
    public int getSub2() {
        return Sub2;
    }
 
 
    public void setSub2(int sub2) {
        Sub2 = sub2;
    }
 
 
    public String getStudentId() {
        return studentId;
    }
 
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
 
    public void displayMarks()
    {
        System.out.println(getStudentId()+ " Marks are: "+ getSub1() +" "+ getSub2());
    }
 
}

AutoWired by Name – Example

Student.java


package com.sridhar.autowired;

public class Student {

    private ExamMark student1;
    private ExamMark student2;
    private ExamMark student3;

    public ExamMark getStudent1() {
        return student1;
    }
    public void setStudent1(ExamMark student1) {
        this.student1 = student1;
    }
    public ExamMark getStudent2() {
        return student2;
    }
    public void setStudent2(ExamMark student2) {
        this.student2 = student2;
    }
    public ExamMark getStudent3() {
        return student3;
    }
    public void setStudent3(ExamMark student3) {
        this.student3 = student3;
    }

    public void displayMarks()
    {
        System.out.println(getStudent1().getStudentId()+ " Marks are: "+ getStudent1().getSub1() +" "+ getStudent1().getSub2());
        System.out.println(getStudent2().getStudentId()+ " Marks are: "+ getStudent2().getSub1() +" "+ getStudent2().getSub2());
        System.out.println(getStudent3().getStudentId()+ " Marks are: "+ getStudent3().getSub1() +" "+ getStudent3().getSub2());
    }

}

ExamMarks.java


package com.sridhar.autowired;

public class ExamMark {
    private String studentId;
    private int Sub1;
    private int Sub2;

    public ExamMark(String studentId)
    {
        this.studentId = studentId;

    }

    public ExamMark( String studentId, int sub1, int sub2)
    {
        this.studentId = studentId;
        this.Sub1 = sub1;
        this.Sub2 = sub2;
    }

    public int getSub1() {
        return Sub1;
    }

    public void setSub1(int sub1) {
        Sub1 = sub1;
    }

    public int getSub2() {
        return Sub2;
    }

    public void setSub2(int sub2) {
        Sub2 = sub2;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

}

springAutoWire.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.sridhar.autowired.Student" autowire="byName"></bean>

    <bean id="student1" class="com.sridhar.autowired.ExamMark">
        <!-- <property name="studentId" value="CSE1001"></property> -->
        <constructor-arg value="CSE1002" />
        <constructor-arg value="89" />
        <constructor-arg value="95" />

    </bean>
    <bean id="student2" class="com.sridhar.autowired.ExamMark">
        <!-- <property name="studentId" value="CSE1001"></property> -->
        <constructor-arg value="CSE1003" />
        <constructor-arg value="50" />
        <constructor-arg value="80" />

    </bean>
    <bean id="student3" class="com.sridhar.autowired.ExamMark">
        <!-- <property name="studentId" value="CSE1001"></property> -->
        <constructor-arg value="CSE1004" />
        <constructor-arg value="90" />
        <constructor-arg value="98" />

    </bean>

</beans>

TestStudentAutoWire.java


package com.sridhar.autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudentAutoWire {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext ctx = new ClassPathXmlApplicationContext("springAutoWire.xml");
        Student student = (Student)ctx.getBean("student");
        student.displayMarks();

    }

}

@Autowire

We provide bean configuration details in the spring bean configuration file and we also specify the beans that will be injected into other beans using ref attribute. But the Spring framework provides autowiring features too, where we don’t need to provide bean injection details explicitly.

 

Types of Modes:

No.

Mode

Description

1) no It is the default autowiring mode. It means no autowiring by default.
2) byName The byName mode injects the object dependency according to the name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
3) byType The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.
4) constructor The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.

 

Limitations of Autowiring:

Limitations Description
Overriding possibility You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring.
Primitive data types You cannot autowire so-called simple properties such as primitives, Strings, and Classes.
Confusing nature Autowiring is less exact than explicit wiring, so if possible prefer using explict wiring.

What is Spring Framework

What is Spring Framework

  • Spring is a lightweight and open source framework
  • The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications
  • Unlike struts and hibernate, Spring framework can be used for all layer implementations for areal time
  • Spring framework is said to be anon-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API
  • Spring Framework made J2EE application development little easier, by introducing a POJO model
  • Spring is a popular and demanded framework because of Simplicity, Testability and Loose Coupling
  • Spring can be used to develop any kind ofJava application, means we can develop starting from console application to enterprise level application.

Some of the benefits of spring platform

  • Make a Java method execute in a database transaction without having to deal with transaction APIs.
  • Make a local Java method a remote procedure without having to deal with remote APIs.
  • Make a local Java method a management operation without having to deal with JMX APIs.
  • Make a local Java method a message handler without having to deal with JMS APIs.

Spring Modules

The Spring Framework consists of features organized into about 20 modules. These modules are grouped into the Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, and Test, as shown in the following diagram.

Spring Modules

Core Container

The Core Container consists of the spring-core, spring-beans, spring-context, spring-context-support, and spring-expression (Spring Expression Language) modules.

The spring-core and spring-beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.

The Context (spring-context) module builds on the solid base provided by the Core and Beans modules: it is a means to access objects in a framework-style manner that is similar to a JNDI registry. The Context module inherits its features from the Beans module and adds support for internationalization (using, for example, resource bundles), event propagation, resource loading, and the transparent creation of contexts by, for example, a Servlet container. The Context module also supports Java EE features such as EJB, JMX, and basic remoting. The ApplicationContext interface is the focal point of the Context module. spring-context-support provides support for integrating common third-party libraries into a Spring application context for caching (EhCache, Guava, JCache), mailing (JavaMail), scheduling (CommonJ, Quartz) and template engines (FreeMarker, JasperReports, Velocity).

The spring-expression module provides a powerful Expression Language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification. The language supports setting and getting property values, property assignment, method invocation, accessing the content of arrays, collections and indexers, logical and arithmetic operators, named variables, and retrieval of objects by name from Spring’s IoC container. It also supports list projection and selection as well as common list aggregations.

AOP and Instrumentation

The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated. Using source-level metadata functionality, you can also incorporate behavioral information into your code, in a manner similar to that of .NET attributes.

The separate spring-aspects module provides integration with AspectJ.

The spring-instrument module provides class instrumentation support and classloader implementations to be used in certain application servers. Thespring-instrument-tomcat module contains Spring’s instrumentation agent for Tomcat.

Messaging

Spring Framework 4 includes a spring-messaging module with key abstractions from the Spring Integration project such as Message, MessageChannel, MessageHandler, and others to serve as a foundation for messaging-based applications. The module also includes a set of annotations for mapping messages to methods, similar to the Spring MVC annotation based programming model.

Data Access/Integration

The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules.

The spring-jdbc module provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes.

The spring-tx module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs (Plain Old Java Objects).

The spring-orm module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, and Hibernate. Using the spring-orm module you can use all of these O/R-mapping frameworks in combination with all of the other features Spring offers, such as the simple declarative transaction management feature mentioned previously.

The spring-oxm module provides an abstraction layer that supports Object/XML mapping implementations such as JAXB, Castor, XMLBeans, JiBX and XStream.

The spring-jms module (Java Messaging Service) contains features for producing and consuming messages. Since Spring Framework 4.1, it provides integration with the spring-messaging module.

Web

The Web layer consists of the spring-web, spring-webmvc, spring-websocket, and spring-webmvc-portlet modules.

The spring-web module provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context. It also contains an HTTP client and the web-related parts of Spring’s remoting support.

The spring-webmvc module (also known as the Web-Servlet module) contains Spring’s model-view-controller (MVC) and REST Web Services implementation for web applications. Spring’s MVC framework provides a clean separation between domain model code and web forms and integrates with all of the other features of the Spring Framework.

The spring-webmvc-portlet module (also known as the Web-Portlet module) provides the MVC implementation to be used in a Portlet environment and mirrors the functionality of the spring-webmvc module.

Test

The spring-test module supports the unit testing and integration testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.

Resource from: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.html

Autowire by Name

Student.java


package com.sridhar.autowired;

public class Student {

private ExamMark student1;
private ExamMark student2;
private ExamMark student3;

public ExamMark getStudent1() {
return student1;
}
public void setStudent1(ExamMark student1) {
this.student1 = student1;
}
public ExamMark getStudent2() {
return student2;
}
public void setStudent2(ExamMark student2) {
this.student2 = student2;
}
public ExamMark getStudent3() {
return student3;
}
public void setStudent3(ExamMark student3) {
this.student3 = student3;
}

public void displayMarks()
{
System.out.println(getStudent1().getStudentId()+ " Marks are: "+ getStudent1().getSub1() +" "+ getStudent1().getSub2());
System.out.println(getStudent2().getStudentId()+ " Marks are: "+ getStudent2().getSub1() +" "+ getStudent2().getSub2());
System.out.println(getStudent3().getStudentId()+ " Marks are: "+ getStudent3().getSub1() +" "+ getStudent3().getSub2());
}

}

ExamMark.java

package com.sridhar.autowired;

public class ExamMark {
	private String studentId;
	private int Sub1;
	private int Sub2;

	public ExamMark(String studentId)
	{
		this.studentId = studentId;

	}

	public ExamMark( String studentId, int sub1, int sub2)
	{
		this.studentId = studentId;
		this.Sub1 = sub1;
		this.Sub2 = sub2;
	}

	public int getSub1() {
		return Sub1;
	}

	public void setSub1(int sub1) {
		Sub1 = sub1;
	}

	public int getSub2() {
		return Sub2;
	}

	public void setSub2(int sub2) {
		Sub2 = sub2;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

}

springAutoWire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="student" class="com.sridhar.autowired.Student" autowire="byName"></bean>

	<bean id="student1" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1002" />
		<constructor-arg value="89" />
		<constructor-arg value="95" />

	</bean>
	<bean id="student2" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1003" />
		<constructor-arg value="50" />
		<constructor-arg value="80" />

	</bean>
	<bean id="student3" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1004" />
		<constructor-arg value="90" />
		<constructor-arg value="98" />

	</bean>

</beans>

TestStudentAutoWire.java

package com.sridhar.autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudentAutoWire {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext ctx = new ClassPathXmlApplicationContext("springAutoWire.xml");
		Student student = (Student)ctx.getBean("student");
		student.displayMarks();
	}
}