Spring DI – Autowiring ”; Previous Next You have learnt how to declare beans using the <bean> element and inject <bean> using <constructor-arg> and <property> elements in XML configuration file. The Spring container can autowire relationships between collaborating beans without using <constructor-arg> and <property> elements, which helps cut down on the amount of XML configuration you write for a big Spring-based application. Autowiring Modes Following are the autowiring modes, which can be used to instruct the Spring container to use autowiring for dependency injection. You use the autowire attribute of the <bean/> element to specify autowire mode for a bean definition. Sr.No Mode & Description 1 no This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter. 2 byName Autowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. 3 byType Autowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown. 4 constructor Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. 5 autodetect Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType. You can use byType or constructor autowiring mode to wire arrays and other typed-collections. Limitations with autowiring Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing for developers to use it to wire only one or two bean definitions. Though, autowiring can significantly reduce the need to specify properties or constructor arguments but you should consider the limitations and disadvantages of autowiring before using them. Sr.No. Limitations & Description 1 Overriding possibility You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring. 2 Primitive data types You cannot autowire so-called simple properties such as primitives, Strings, and Classes. 3 Confusing nature Autowiring is less exact than explicit wiring, so if possible prefer using explict wiring. Print Page Previous Next Advertisements ”;
Category: spring Dependency Injection
Spring DI – Useful Resources
Spring DI – Useful Resources ”; Previous Next The following resources contain additional information on Spring DI. Please use them to get more in-depth knowledge on this topic. Useful Links on Spring DI Spring Source − Find latest news about Spring Framework, download section and all about Spring. Oracle”s Site on JDBC − Sun Developer Network giving link on JDBC material. MySQL Connector/J − MySQL Connector/J is the official JDBC driver for MySQL. The JavaTM Tutorials − The Java Tutorials are practical guides for programmers who want to use the Java programming language to create applications. JavaTM 2 SDK, Standard Edition − Official site for JavaTM 2 SDK, Standard Edition Free Java Download − Download Java for your desktop computer now! Java Technology Reference − Sun Microsystem”s official website listing down all the API documentation, latest Java Technologies, Books and other resource. To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
Spring DI – Map Ref Setter
Spring DI – Map Ref Setter ”; Previous Next You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean. Now what if you want to pass Map. In this example, we”re showcasing passing direct values of the Map using setter injection. Example The following example shows a class JavaCollection that is using collections as dependency injected using setter method. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − Address.java − A class to be used as dependency. JavaCollection.java − A class containing a collections of dependencies. MainApp.java − Main application to run and test. Here is the content of Address.java file − package com.tutorialspoint; public class Address { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } } Here is the content of JavaCollection.java file − package com.tutorialspoint; import java.util.*; public class JavaCollection { Map<String, Address> addressMap; public JavaCollection() {} public JavaCollection(Map<String, Address> addressMap) { this.addressMap = addressMap; } // a setter method to set Map public void setAddressMap(Map<String, Address> addressMap) { this.addressMap = addressMap; } // prints and returns all the elements of the Map. public Map<String, Address> getAddressMap() { System.out.println(“Map Elements :” + addressMap); return addressMap; } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); JavaCollection jc=(JavaCollection)context.getBean(“javaCollection”); jc.getAddressMap(); } } Following is the configuration file applicationcontext.xml which has configuration for all the type of collections − <?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-3.0.xsd”> <bean id = “address1” class = “com.tutorialspoint.Address”> <property name=”name” value=”INDIA”></property> </bean> <bean id = “address2” class = “com.tutorialspoint.Address”> <property name=”name” value=”JAPAN”></property> </bean> <bean id = “address3” class = “com.tutorialspoint.Address”> <property name=”name” value=”USA”></property> </bean> <bean id = “address4” class = “com.tutorialspoint.Address”> <property name=”name” value=”UK”></property> </bean> <bean id = “javaCollection” class = “com.tutorialspoint.JavaCollection”> <property name = “addressMap”> <map> <entry key = “1” value-ref = “address1″/> <entry key = “2” value-ref = “address2″/> <entry key = “3” value-ref = “address3″/> <entry key = “4” value-ref = “address4″/> </map> </property> </bean> </beans> Output Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Map Elements :{1=INDIA, 2=JAPAN, 3=USA, 4=UK} Print Page Previous Next Advertisements ”;
Spring DI – Non-Static Factory ”; Previous Next Spring provides an option to inject dependency using factory-method along with factory-bean attributes in case of non-static factory methods. Example The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − TextEditor.java − A class containing a SpellChecker as dependency. SpellChecker.java − A dependency class. MainApp.java − Main application to run and test. Here is the content of TextEditor.java file − package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } } Following is the content of another dependent class file SpellChecker.java − This class constructor is private. So its object can not be created directly using new operator by other object. It has a non-static factory method to get an instance. package com.tutorialspoint; public class SpellChecker { private SpellChecker(){ System.out.println(“Inside SpellChecker constructor.” ); } public SpellChecker getInstance() { System.out.println(“Inside SpellChecker getInstance.” ); return new SpellChecker(); } public void checkSpelling(){ System.out.println(“Inside checkSpelling.” ); } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); TextEditor te = (TextEditor) context.getBean(“textEditor”); te.spellCheck(); } } Following is the configuration file applicationcontext.xml which has configuration for autowiring byName − <?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-3.0.xsd”> <!– Definition for textEditor bean –> <bean id = “textEditor” class = “com.tutorialspoint.TextEditor” autowire = “byName”> <property name = “name” value = “Generic Text Editor” /> </bean> <bean id = “spellCheckFactory” class = “com.tutorialspoint.SpellChecker”></bean> <!– Definition for spellChecker bean –> <bean id = “spellChecker” class = “com.tutorialspoint.SpellChecker” factory-method=”getInstance”>< factory-bean=”spellCheckFactory”/bean> </beans> Output Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Inside SpellChecker constructor. Inside SpellChecker getInstance. Inside SpellChecker constructor. Inside checkSpelling. Print Page Previous Next Advertisements ”;
Spring DI – Collections Setter ”; Previous Next You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean. Now what if you want to pass plural values like Java Collection types such as List, Set, Map, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows − Sr.No Element & Description 1 <list> This helps in wiring ie injecting a list of values, allowing duplicates. 2 <set> This helps in wiring a set of values but without any duplicates. 3 <props> This can be used to inject a collection of name-value pairs where the name and value are both Strings. You can use either <list> or <set> to wire any implementation of java.util.Collection or an array. In this example, we”re showcasing passing direct values of the collection elements. Example The following example shows a class JavaCollection that is using collections as dependency injected using setters. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − JavaCollection.java − A class containing a collections as dependency. MainApp.java − Main application to run and test. Here is the content of JavaCollection.java file − package com.tutorialspoint; import java.util.*; public class JavaCollection { List<String> addressList; Set<String> addressSet; Properties addressProp; // a setter method to set List public void setAddressList(List<String> addressList) { this.addressList = addressList; } // prints and returns all the elements of the list. public List<String> getAddressList() { System.out.println(“List Elements :” + addressList); return addressList; } // a setter method to set Set public void setAddressSet(Set<String> addressSet) { this.addressSet = addressSet; } // prints and returns all the elements of the Set. public Set<String> getAddressSet() { System.out.println(“Set Elements :” + addressSet); return addressSet; } // a setter method to set Property public void setAddressProp(Properties addressProp) { this.addressProp = addressProp; } // prints and returns all the elements of the Property. public Properties getAddressProp() { System.out.println(“Property Elements :” + addressProp); return addressProp; } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); JavaCollection jc=(JavaCollection)context.getBean(“javaCollection”); jc.getAddressList(); jc.getAddressSet(); jc.getAddressProp(); } } Following is the configuration file applicationcontext.xml which has configuration for all the type of collections − <?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-3.0.xsd”> <!– Definition for javaCollection –> <bean id = “javaCollection” class = “com.tutorialspoint.JavaCollection”> <!– results in a setAddressList(java.util.List) call –> <property name = “addressList”> <list> <value>INDIA</value> <value>JAPAN</value> <value>USA</value> <value>UK</value> </list> </property> <!– results in a setAddressSet(java.util.Set) call –> <property name = “addressSet”> <set> <value>INDIA</value> <value>JAPAN</value> <value>USA</value> <value>UK</value> </set> </property> <!– results in a setAddressProp(java.util.Properties) call –> <property name = “addressProp”> <props> <prop key = “one”>INDIA</prop> <prop key = “two”>JAPAN</prop> <prop key = “three”>USA</prop> <prop key = “four”>UK</prop> </props> </property> </bean> </beans> Output Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − List Elements :[INDIA, JAPAN, USA, UK] Set Elements :[INDIA, JAPAN, USA, UK] Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA} Print Page Previous Next Advertisements ”;
Spring DI – Constructor-Based ”; Previous Next Constructor-Based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on the other class. Example The following example shows a class TextEditor that can only be dependency-injected with constructor injection. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − TextEditor.java − A class containing a SpellChecker as dependency. SpellChecker.java − A dependency class. MainApp.java − Main application to run and test. Here is the content of TextEditor.java file − package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; public TextEditor(SpellChecker spellChecker) { System.out.println(“Inside TextEditor constructor.” ); this.spellChecker = spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); } } Following is the content of another dependent class file SpellChecker.java package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println(“Inside SpellChecker constructor.” ); } public void checkSpelling() { System.out.println(“Inside checkSpelling.” ); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); TextEditor te = (TextEditor) context.getBean(“textEditor”); te.spellCheck(); } } Following is the configuration file applicationcontext.xml which has configuration for the constructor-based injection − <?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-3.0.xsd”> <!– Definition for textEditor bean –> <bean id = “textEditor” class = “com.tutorialspoint.TextEditor”> <constructor-arg ref = “spellChecker”/> </bean> <!– Definition for spellChecker bean –> <bean id = “spellChecker” class = “com.tutorialspoint.SpellChecker”></bean> </beans> Output Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Inside SpellChecker constructor. Inside TextEditor constructor. Inside checkSpelling. Print Page Previous Next Advertisements ”;
Spring DI – Map Ref Constructor ”; Previous Next You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean. Now what if you want to pass Map. In this example, we”re showcasing passing direct values of the Map using constructor injection. Example The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − Address.java − A class to be used as dependency. JavaCollection.java − A class containing a collections of dependencies. MainApp.java − Main application to run and test. Here is the content of Address.java file − package com.tutorialspoint; public class Address { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } } Here is the content of JavaCollection.java file − package com.tutorialspoint; import java.util.*; public class JavaCollection { Map<String, Address> addressMap; public JavaCollection() {} public JavaCollection(Map<String, Address> addressMap) { this.addressMap = addressMap; } // a setter method to set Map public void setAddressMap(Map<String, Address> addressMap) { this.addressMap = addressMap; } // prints and returns all the elements of the Map. public Map<String, Address> getAddressMap() { System.out.println(“Map Elements :” + addressMap); return addressMap; } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); JavaCollection jc=(JavaCollection)context.getBean(“javaCollection”); jc.getAddressMap(); } } Following is the configuration file applicationcontext.xml which has configuration for all the type of collections − <?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-3.0.xsd”> <bean id = “address1” class = “com.tutorialspoint.Address”> <property name=”name” value=”INDIA”></property> </bean> <bean id = “address2” class = “com.tutorialspoint.Address”> <property name=”name” value=”JAPAN”></property> </bean> <bean id = “address3” class = “com.tutorialspoint.Address”> <property name=”name” value=”USA”></property> </bean> <bean id = “address4” class = “com.tutorialspoint.Address”> <property name=”name” value=”UK”></property> </bean> <bean id = “javaCollection” class = “com.tutorialspoint.JavaCollection”> <constructor-arg name = “addressMap”> <map> <entry key = “1” value-ref = “address1″/> <entry key = “2” value-ref = “address2″/> <entry key = “3” value-ref = “address3″/> <entry key = “4” value-ref = “address4″/> </map> </constructor-arg> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Map Elements :{1=INDIA, 2=JAPAN, 3=USA, 4=UK} Print Page Previous Next Advertisements ”;
Spring DI – Injecting Collections Constructor ”; Previous Next You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean. Now what if you want to pass plural values like Java Collection types such as List, Set, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows − Sr.No Element & Description 1 <list> This helps in wiring ie injecting a list of values, allowing duplicates. 2 <set> This helps in wiring a set of values but without any duplicates. 3 <props> This can be used to inject a collection of name-value pairs where the name and value are both Strings. You can use either <list> or <set> to wire any implementation of java.util.Collection or an array. In this example, we”re showcasing passing direct values of the collection elements. Example The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − JavaCollection.java − A class containing a collections as dependency. MainApp.java − Main application to run and test. Here is the content of JavaCollection.java file − package com.tutorialspoint; import java.util.*; public class JavaCollection { List<String> addressList; Set<String> addressSet; Properties addressProp; public JavaCollection() {} public JavaCollection(List<String> addressList, Set<String> addressSet, Properties addressProp) { this.addressList = addressList; this.addressSet = addressSet; this.addressProp = addressProp; } // a setter method to set List public void setAddressList(List<String> addressList) { this.addressList = addressList; } // prints and returns all the elements of the list. public List<String> getAddressList() { System.out.println(“List Elements :” + addressList); return addressList; } // a setter method to set Set public void setAddressSet(Set<String> addressSet) { this.addressSet = addressSet; } // prints and returns all the elements of the Set. public Set<String> getAddressSet() { System.out.println(“Set Elements :” + addressSet); return addressSet; } // a setter method to set Property public void setAddressProp(Properties addressProp) { this.addressProp = addressProp; } // prints and returns all the elements of the Property. public Properties getAddressProp() { System.out.println(“Property Elements :” + addressProp); return addressProp; } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); JavaCollection jc=(JavaCollection)context.getBean(“javaCollection”); jc.getAddressList(); jc.getAddressSet(); jc.getAddressProp(); } } Following is the configuration file applicationcontext.xml which has configuration for all the type of collections − <?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-3.0.xsd”> <bean id = “javaCollection” class = “com.tutorialspoint.JavaCollection”> <constructor-arg name = “addressList”> <list> <value>INDIA</value> <value>JAPAN</value> <value>USA</value> <value>UK</value> </list> </constructor-arg> <constructor-arg name = “addressSet”> <set> <value>INDIA</value> <value>JAPAN</value> <value>USA</value> <value>UK</value> </set> </constructor-arg> <constructor-arg name = “addressProp”> <props> <prop key = “one”>INDIA</prop> <prop key = “two”>JAPAN</prop> <prop key = “three”>USA</prop> <prop key = “four”>UK</prop> </props> </constructor-arg> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − List Elements :[INDIA, JAPAN, USA, UK] Set Elements :[INDIA, JAPAN, USA, UK] Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA} Print Page Previous Next Advertisements ”;
Spring DI – Static Factory
Spring DI – Static Factory ”; Previous Next Spring provides an option to inject dependency using factory-method attribute. Example The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − TextEditor.java − A class containing a SpellChecker as dependency. SpellChecker.java − A dependency class. MainApp.java − Main application to run and test. Here is the content of TextEditor.java file − package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } } Following is the content of another dependent class file SpellChecker.java − This class constructor is private. So its object can not be created directly using new operator by other object. It has a static factory method to get an instance. package com.tutorialspoint; public class SpellChecker { private SpellChecker(){ System.out.println(“Inside SpellChecker constructor.” ); } public static SpellChecker getInstance() { System.out.println(“Inside SpellChecker getInstance.” ); return new SpellChecker(); } public void checkSpelling(){ System.out.println(“Inside checkSpelling.” ); } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); TextEditor te = (TextEditor) context.getBean(“textEditor”); te.spellCheck(); } } Following is the configuration file applicationcontext.xml which has configuration for autowiring byName − <?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-3.0.xsd”> <!– Definition for textEditor bean –> <bean id = “textEditor” class = “com.tutorialspoint.TextEditor” autowire = “byName”> <property name = “name” value = “Generic Text Editor” /> </bean> <!– Definition for spellChecker bean –> <bean id = “spellChecker” class = “com.tutorialspoint.SpellChecker” factory-method=”getInstance”></bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Inside SpellChecker getInstance. Inside SpellChecker constructor. Inside checkSpelling. Print Page Previous Next Advertisements ”;
Spring DI – Environment Setup ”; Previous Next This chapter will guide you on how to prepare a development environment to start your work with Spring Framework. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework − Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle”s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively. If you are running Windows and have installed the JDK in C:jdk-11.0.11, you would have to put the following line in your C:autoexec.bat file. set PATH=C:jdk-11.0.11;%PATH% set JAVA_HOME=C:jdk-11.0.11 Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button. On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file. setenv PATH /usr/local/jdk-11.0.11/bin:$PATH setenv JAVA_HOME /usr/local/jdk-11.0.11 Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE. Setup Eclipse IDE All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine then it should display the following result − Set Maven In this tutorial, we are using maven to run and build the spring based examples. Follow the Maven – Environment Setup to install maven. Print Page Previous Next Advertisements ”;