Spring Ioc的基本原理及其xml的实现方法(上)

来源:互联网 发布:网络市场中间商的类型 编辑:程序博客网 时间:2024/06/05 14:53
Spirng Ioc 的基本原理及XML的实现方法


下面为spring的主要原理:
IoC(Inversion of Control=控制反转):IoC就是应用本身不依赖对象的创建和维护而是交给外部容器(这里为spring),
这要就把应用和对象之间解耦,控制权交给了外部容器。
即Don't call me ,I'll call you!所以IoC也称DI(依赖注入=dependency injection ),其对象的创建和维护依赖于外部容器.









这里举一个例子来说明Spring Ioc 的xml文件的实现方法:


UserServiceImpl类中的代码:(UserService是UserDAO和Action联系的桥梁)
public class UserServiceImpl implements UserService {

UserDAO dao;///被动等待创建=Ioc = 依赖注入(DI) 
public UserDAO getDao() {
return dao;
}
public void setDao(UserDAO dao) {
this.dao = dao;
}
@Override
public void add(User user) {

   dao.save(user);
}


}


UserAction中的代码:
public class UserAction {
    private User user;
    private UserService service;//同样使用IoC(都没有主动的创建对象)
public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}
    public UserService getService() {
return service;
}


public void setService(UserService service) {
this.service = service;
}


///做一个保存数据或者更新数据的action,名字为add,转到main.jsp上
public String add()
{  
service.add(user);
return "main";
}
}


然后就是spring的配置文件:
applicationContext.xml的代码:
<?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.xsd">
       
       <bean id="mydao" class="net.xinqushi.dao.Impl.UserDAOFileImpl"></bean>///这是dao层中的一个实现方法(可以改变实现方法)
                                                                             ///这个实现方法的代码就不具体写了
       <bean id="service" class="net.xinqushi.service.Impl.UserServiceImpl">


       <property name="dao" ref="mydao"></property>///在这spring外部容器中创建service对象的时候一定要连dao也一起创建了 
                                                   ///不然会报空指针错误,因为service和dao是有联系的(基于dao的)


       </bean>
</beans>


web.xml文件的代码:
 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <display-name>Struts Blank</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>


    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>


</web-app>






下面通过此例子来详细说明一下spring Ioc通过xml文件的实现过程:
首先前端(前端页面就省略了)会找到对应的action(通过对应struts.xml文件找到对应的action),也就是UserAction,这时候发现
service没有创建对象,然后就到applicationContext.xml文件中去寻找对应的service(在此之前web.xml文件已经把struts.xml,hiber
nate.cfg.xml与spring的xml文件都注入进来了),并创建了此对象(UserServiceImpl这个方法)(service这里面的名字一定要和action中名字相同),
然后又发现还有属性dao ,转到mydao,这个时候dao也创建了对象了(也就是这个方法.UserServiceImpl),
基本上就是这些执行过程了,还有一点就是: <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
这行代码就把spring的xml文件带进来了,通过我们之前引入的插件实现的!)