The spring beans are define in spring bean XML configuration file, so that spring container can detect and register spring beans for dependency injection. The are created using bean tag. Once the xml configuration loads the define beans are ready to be injected and use by the dependent classes.
Steps
1. | Create classes |
2. | Create xml configuration file |
3. | Add spring bean |
4. | Read configuration file |
Create classes
The class which instance is require at runtime to achieve the functionality of dependent (who needs instance of others classes) class. The spring bean will be created using <bean> tag.
1 package com.ogyan.service;
2
3 public class ExampleService {
4
5 public String getValue() {
6 return "Example service getValue method called...";
7 }
8 }
9
Create xml configuration file
Once the classes are created, it needs to create xml configuration file which is responsible to create spring container bean by specifying the namespace. An xml namespace is just a token which provides a better description, identifies whose "version" a particular tag or attribute belongs and it prevents the conflicts among the modules.
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context.xsd">
9
10
11
12 </beans>
App spring bean
The spring configuration allows to create bean using <bean> tag by specifying the bean id and class path.
The class path includes the name of class for which bean needs to be created. Once the configuration file loads in the memory it will create requested bean in the spring container.
Add spring bean definition
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context.xsd">
9
10
11
12 <bean id="exampleService" class="com.ogyan.service.ExampleService"></bean>
13 </beans>
Read configuration file
Once the specified package scanned successfully, all the annotated class bean will be created by spring container and it is ready to inject in the dependent classes whenever requested. To read it manually for testing purpose, lets use ClassPathXmlApplicationContext. There are many application context implementation which can be used based on requirement.
1. | ClassPathXmlApplicationContext, |
2. | AnnotationConfigWebApplicationContext |
3. | XmlWebApplicationContext |
4. | FileSystemXmlApplicationContext |
5. | AnnotationConfigWebApplicationContext |
1 package example;
2
3 import org.springframework.context.support.ClassPathXmlApplicationContext;
4 import com.ogyan.service.ExampleService;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
11 new String[] { "applicationContext.xml" });
12
13 try {
14 ExampleService service = (ExampleService) context.getBean("exampleService");
15 String str = service.getValue();
16 System.out.println(str);
17 } catch (Exception e) {
18 context.close();
19 }
20 }
21 }
22
In the above example, a ClassPathXmlAppilcationContext object is created by passing a string that includes name of spring bean configuration file. A context method getBean() called by specifying a string that represents an ID of spring bean. It returns ExampleService bean instance. A getValue() method called that returns string and print on console.
1 Example service getValue method called...
Related options for your search