Spring journey (ii) transition from non-Spring to Spring -- Use of Spring IOC containers

Source: Internet
Author: User

Disadvantages of the previous Code

In the previous article, we used a simple example to describe the origin of Spring. in the previous article, UserManager needs to instantiate the specific Dao itself when calling Dao, or create the corresponding Dao through the factory, in the end, the client instantiates the specific UserManager, The UserManager obtains the factory, and the factory creates the corresponding Dao. The Spring framework encapsulates the creation process. Instead of manually going to new, we hand it over to the Spring IOC container to do this, and he will find it by himself (SpringIOC container, after the configuration file is found, the new file is passed to us directly, and we do not need to find the factory to instantiate it.

So what is Spring's IOC container?

IOC (Inversionof Control) is called Control inversion. Another name is dependency injection DI (Dependencyinjection ). In the previous code, we called the Dao layer in UserManagerImpl in this way, UserDaouserDao = new UserDaoOracleImpl (); in this way, using interface programming can increase code stability and robustness, but the interface must be implemented. Sooner or later, the code will be executed, so that the coupling relationship will arise. The ManagerImpl class and the UserDaoOracleImpl class are dependencies. Of course, we can use the factory to create Dao, as shown in.


Figure 2.1 Dao created without a factory


Figure 2.2 Use a factory to create a Dao

The above problems are solved to some extent on the surface of the factory, but the code coupling has not changed in essence. IOC containers can completely solve this coupling problem by removing the coupled code and placing it in a unified XML file. A container forms the dependency as needed, that is, inject the required interface into the required class, which is the source of "dependency injection. There are many types of IOC containers: PicoContainer, JBoss, HiveMind, and spring. Here we mainly talk about Spring IOC containers.

The following describes how to use the Spring IOC container for control.

1. Import the Spring dependency package (because the IOC of Spring is used here, You can import the core package)

* Spring_home/dist/spring. jar.

* Introduce related logging packages, two, spring-framework-2.0 \ lib \ log4j \ log4j-1.2.14.jar spring-framework-2.0 \ lib \ jakarta-commons \ commons-logging.jar

Create UserLibraries directly in userlibraries and import JARs to our project.

2. Provide the configuration file applicationContext. xml, put it in the src directory of our project, we can copy Spring-framework-2.0 \ samples \ jpetstore \ war \ WEB-INF

ApplicationContext. xml is stored in the src directory.

3. Provide the log4j. propertiers configuration file.

4. Provide the constructor or setter method in UserManagerImpl to allow Spring to inject the relationship between UserDao and UserManagerImpl (DI.

5. To allow Spring to manage object creation and dependency, you must configure the dependency to the core configuration file of Spring.

In the previous Code, our Dao Layer Code remains unchanged, and our UserManagerImpl code is as follows.

UserManagerImpl. java

Public class UserManagerImpl implements UserManager {// you cannot see the specific implementation. however, the assembly process is displayed when the client is called. private UserDao userDao; // assign values to the constructor. // public UserManagerImpl (UserDao userDao) {// this. userDao = userDao; //} // inject the dependent object using the setter method. Public void setUserDao (UserDao userDao) {this. userDao = userDao;} @ Overridepublic void addUser (String username, String password) {userDao. addUser (username, password );}}


Spring dependency injection method. We can obtain and set Bean attribute values through value assignment injection, that is, getter and setter. We generally use this method. In Spring, every object in the configuration file And child elements. Indicates that the setter method of this JavaBean is used to inject the value. You can define the attributes to be configured and the values to be injected. You can inject attributes into any type of values, not only the basic type of java data type, but also the Bean object, here we inject Bean objects.

The other is constructor injection, which is not often used. Parameters to be injected when the Bean is instantiated.


The ApplicationContext. xml Code is as follows.

 
 
  
  
  
  
   
   
   
  
 


The above XML file is used in Spring to configure JavaBean. In this way, the scattered JavaBean can be assembled into a whole system to implement the specific business logic. After Bean development and assembly, the next step is to call and test the Bean.

The Code is as follows.

Public class client {public static void main (String [] args) {// BeanFactory in spring, which is an interface. // read applicationContext. xml file implementation. // The ClassPathXmlApplicationContext class is the implementation of the BeanFactory interface. // applicationContext. the xml file is read in and the object is created. beanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml "); // product ID. userManager userManager = (UserManager) factory. getBean ("userManager"); userManager. addUser ("Zhang San", "123 ");}}


In this way, the SpringIOC container is used to solve the dependency problem. We do not need to create a factory by ourselves. We only need to declare an interface. The specific implementation tells the SpringIOC container in the configuration file, he will find and create the corresponding instance and pass it to us. Originally, we will control the dependency between objects by ourselves, and now it is controlled by IOC, this is the control reversal. This makes it easy for us to understand.



Spring coming .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.