1. Start Spring

An understanding of the Spring

In order to achieve inversion of control, we can imagine java create a factory class, factory class can go to read a configuration file such as a .property file. With this configuration file, the reflection creates objects when the objects of a required outside of class, just as the factory upon request.

But there is a problem that every request will create a new object, rather than a singleton. So you get a java Map dictionary, which maintain a dictionary, the object does not exist as long as the user needs, put objects created put into the dictionary. If the object user needs in

The dictionary exists, it is taken directly from the dictionary, which achieved a singleton, this dictionary will be treated as a storage container class object. It was later developed into what we call the ioc container --ApplicationContext.

 

2. One of the easiest Spring Inversion of Control case are as follows:

. A create an interface implementation and interface are as follows:

package com.itheima.service;

public interface IAccountService {
    void saveAccount();
}

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

public class AccountServicePureImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("朝辞白帝彩云间");
    }
}

 

b. Create a bean.xml under the Resource

Used to configure the implementation class, after starting the program can be adjusted to read this file to create a container class

<?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="accountServicePureImpl" class="com.itheima.service.impl.AccountServicePureImpl"></bean>


    </beans>

c. If you want to call this interface implementation class must first create a container

Package com.itheima.ui; 

Import com.itheima.service.IAccountService;
 Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.support.ClassPathXmlApplicationContext; 

public  class Client {
     public  static  void main (String [] args) {
         // tell container ClassPathXmlApplicationContext bean.xml read configuration is one of the three main container common implementation class 
        the ApplicationContext context = new new the ClassPathXmlApplicationContext ( "bean.xml" ); 
        IAccountService AC = (IAccountService) context.getBean ( " accountServicePureImpl " ); 
        ac.saveAccount (); 
    } 
}

 There are three ways 3.IOC IOC first set must be noted that the method can not only define a set method constructor third variable second injection annotations

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;

public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao=null;
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }


    public void saveAccount() {
        accountDao.saveAccount();
    }
}
---------------以上是实现类--------------------

Here is the configuration

<?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="accountServiceImpl" class="com.itheima.service.impl.AccountServiceImpl">
        <property ref="accountDaoImpl" name="accountDao"></property>
    </bean>
    <bean id="accountDaoImpl" class="com.itheima.dao.impl.AccountDaoImpl"></bean>


    </beans>

 

Sansei III

Guess you like

Origin www.cnblogs.com/wholeworld/p/11531085.html