Step 1 – Create Spring Project:
Step 2 – Add Required Libraries:
- antlr-runtime-3.0.2
- org.springframework.aop-3.2.0.M1
- org.springframework.asm-3.2.0.M1
- org.springframework.aspects-3.2.0.M1
- org.springframework.beans-3.2.0.M1
- org.springframework.context.support-3.2.0.M1
- org.springframework.context-3.2.0.M1
- org.springframework.core-3.2.0.M1
- org.springframework.expression-3.2.0.M1
- commons-logging-1.1.1
Step 3 – Create Source Files:
HelloWorld.java
package com.sdnext.dineshonjava.tutorial; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
SpringTestApp.java
package com.sdnext.dineshonjava.tutorial; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTestApp { public static void main(String[] args) { //ClassPathXmlApplicationContext is load all beans in the application ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //this step is used to get required bean using getBean() method of the created context HelloWorld helloWorld= (HelloWorld) context.getBean("helloWorld"); helloWorld.getMessage(); } }
- First step is to create application context where we used framework API ClassPathXmlApplicationContext(). This API loads beans configuration file and eventually based on the provided API, it takes care of creating and initializing all the objects ie. beans mentioned in the configuration file.
- Second step is used to get required bean using getBean() method of the created context. This method uses bean ID to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.
Step 4 – Create Bean Configuration File:
spring.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.sdnext.dineshonjava.tutorial.HelloWorld" id="helloWorld"> <property name="message" value="Hello World!"> </property></bean> </beans>
After creation of the bean configuration file(Spring.xml), now we put this file into “src” directory of the application(i.e. which is present in classpath). Then configuration file(Spring.xml) is loaded in the ApplicationContext as follows.
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Now suppose we put configuration file(Spring.xml) into the different directory package other than “src” like “com.dineshonjava.sdnext.springConfig” then we have to give classpath of configuration file(Spring.xml) to ApplicationContext as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/dineshonjava/sdnext/springConfig/spring.xml");
We can use wildcard for this as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/dineshonjava/**/springConfig/spring.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/*/**/springConfig/*-spring.xml");
When Spring application gets loaded into the memory, Framework makes use of the above configuration file to create all the beans defined and assign them a unique ID as defined in tag. You can use tag to pass the values of different variables used at the time of object creation.
Now run the program you will get following output on console.
- Spring Interview Questions and Answers
- Spring AOP Interview Questions and Answers
- Spring MVC Interview Questions
- Spring Security Interview Questions and Answers
- Spring REST Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Spring Boot Microservices Interview Questions and Answers
- Dependency Injection (DI) in Spring
- Spring IoC Container
- What is Bean Factory in Spring
- ApplicationContext in Spring
- Bean Autowiring in Spring
- Spring Bean Scopes
- Create Custom Bean Scope in Spring Example
- Using ApplicationContextAware in Spring
- Spring Bean Life Cycle and Callbacks
- BeanPostProcessor in Spring
- BeanFactoryPostProcessor in Spring
- Annotations in Spring and Based Configuration
- Spring JSR-250 Annotations
- JSR 330 Annotations in Spring
- Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
- Method injection with Spring using Lookup method property
- Spring AOP-Introduction to Aspect Oriented Programming
- @Aspect Annotation in Spring
- Spring AOP AspectJ @Before Annotation Advice Example
- Spring AOP Before Advice Example using XML Config
- Spring AOP AspectJ @After Annotation Advice Example
- Spring AOP After Advice Example using XML Config
- Spring AOP AspectJ @AfterReturning Annotation Advice Example
- Spring AOP After-Returning Advice Example using XML Config
- Spring AOP AspectJ @AfterThrowing Annotation Advice Example
- Spring AOP After Throwing Advice Example using XML Config
- Spring AOP AspectJ @Around Annotation Advice Example
- Spring AOP Around Advice Example using XML Config
- Spring AOP Proxies in Spring
- Spring AOP Transaction Management in Hibernate
- Spring Transaction Management
- Spring Declarative Transaction Management Example
- Spring AOP-Ordering of Aspects with Example
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example
good tutorial..must read
There is any way to create spring project without adding lib (jar) files? why we need no add jar in STS? Then what is the diff b/w normal java project and spring project?
yes we can use maven to manage deployment and all required libs for application at deploying time environment.
Spring project is nothing but some pre build like environment set as default behavior which required to setup manually on Java Project. But both are we can use for an non web based application.
In menu no such option like spring project…what to do sir
Which project?? Spring Tutorial has numerous projects here.
There is no Spring project when we download the STS for eclipse. This document requires update. Thanks.