Spring container and configuration knowledge
Through the first two articles, I have a preliminary understanding of spring, and should realize that the core of spring is to control reverse IOC and dependency injection DI.
How does spring achieve this goal?
We all know that the spring framework is used to inject Dependencies by spring containers, so some configuration information is required to tell spring how to inject. This is the configuration of spring metadata (bean configuration ).
In summary, spring implements basic functions through container and metadata configuration.
Achieved through this article, two goals:
1. A preliminary understanding of spring containers.
2. Get a preliminary understanding of spring metadata Configuration
ApplicationContext
Org. springframework. context. applicationContext represents the Spring IoC container, which is responsible for instantiating, configuring, and assembling beans. spring performs the preceding operations by reading the metadata configuration. The metadata configuration includes xml, annotation, and JSR standards.
Spring also provides several application context implementations, such as ClassPathXmlApplicationContext and FileSystemXmlApplicationContext. As the name suggests, they read xml-based Metadata configurations.
Program example
The following is a simple example to illustrate the simple usage of spring containers and spring XMl Metadata configuration.
This example is very simple, that is, getting users in the system and printing them out. The entire program is divided into three layers, simulating dao, service and client, the client calls service, and the service calls dao. The dependency of the Service on dao is managed by the spring container. The code structure is as follows:
Code
The Code starts from a client call and goes a little deeper into the underlying layer. The comments in the Code describe the key points in detail.
First, the User class:
public class User { public int id; public String name; /** * @param id * @param name */ public User(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
Client
The client is simulated by calling the main function. The Code is as follows:
Package com. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. test. service. userService;/*** @ Description: the client first initializes the spring container and obtains benn from the container to call the corresponding method. Unlike the traditional method, * the instantiation of the Business Object UserService is not produced by the client through new, but directly obtained from the spring container. ** In this example, eclipse */public class TestMain {public static void main (String [] args) {/*** ApplicationContext represents the spring container, classPathXmlApplicationContext is an implementation of ClassPathXmlApplicationContext. It reads the corresponding * xml Metadata configuration from the class path and initializes the container. Among them, allbean. xml is the corresponding metadata configuration */ApplicationContext context = new ClassPathXmlApplicationContext ("allbean. xml "); // get the userService object named user from the container, which is configured after the configuration file. UserService userService = context. getBean ("user", UserService. class); System. out. println (userService. getUser (); // can be compared with the traditional method: // UserService user = new UserServiceIml (); // UserDao dao = new UserDaoImp (); // (UserServiceIml) user ). setUserDao (dao); // System. out. println (user. getUser ());}}
The content of allbean. xml is as follows:
Service
The Service layer contains an interface, an implementation class, and a Spring xml Metadata configuration file:
As follows:
/*** Interface */public interface UserService {public List
GetUser ();}/*** implementation class. A field references the UserDao interface and its specific business logic call {@ link UserDao # getUser ()} implement ** and there is a set method, which is injected and used by spring containers. If no error is returned, * This is a method for spring container dependency injection-setter injection */public class UserServiceIml implements UserService {private UserDao userDao; @ Override public List
GetUser () {return userDao. getUser ();} public void setUserDao (UserDao userDao) {this. userDao = userDao ;}}
The Xml Metadata configuration is as follows. It defines userServicebean and its dependencies:
Dao
The Dao layer defines the corresponding interfaces to implement class and xml Metadata configuration. The first is the interface and implementation class:
public interface UserDao { public List
getUser(); }public class UserDaoImp implements UserDao{ @Override public List
getUser() { List
res = new ArrayList
(); User a = new User(1, "aa"); User b = new User(2, "bb"); res.add(a); res.add(b); return res; } }
Then the xml configuration file defines a bean named userDao with the Implementation class com. test. dao. UserDaoImp:
Output
Run the client program and enter the following as expected:
Summary
This article briefly describes the usage of Spring containers and bean configuration. Starting from a very simple example, we analyze the advantages of Spring from top to bottom. Of course, I am currently at a limited level. If not, please correct me.
In addition, there are many aspects to understand about Spring bean configuration, not only Xml, but also annotations, JSR and their combinations. Bean configurations also include other aspects, such as basic data types, Collection types, lifecycles, scopes, and container extension points. These are all core features of spring and need to be understood step by step, we look forward to making common progress.