commons-logging 和log4j包下载 Spring根据XML配置文件生成对象

需要用到Spring压缩包中的四个核心JAR包

beans 、context、core 和expression

下载地址:

https://pan.baidu.com/s/1qXLHzAW

以及日志jar包

commons-logging 和log4j

下载地址:

https://pan.baidu.com/s/1mimTW5i


 

创建一个Dynamic Web Project 动态Web项目,在src中建立一个测试的类User如下:

package com.swift;

public class User {
    public void fun() {
        System.out.println("fun is ready.");
    }
}

原始的方法是在main()中 User user=new User(); user.fun();

现在交给Spring帮我们创建对象,它的底层会使用反射机制等,我们只需要配置xml文件就可以了。

在src下建立applicationContext.xml

添加schema约束,文件代码如下:

<?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">

</beans>

配置对象创建

<?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">
<!-- IoC 控制反转 SpringSpring根据XML配置文件生成对象 -->
<bean id="user" class="com.swift.User"></bean>
</beans>

创建Servlet类观察结果

package com.swift;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/test")
public class TestIOC extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public TestIOC() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        @SuppressWarnings("resource")
        //就是下边这几句了
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User) context.getBean("user");
        String userInfo=user.fun();
        response.getWriter().println();
        response.getWriter().append(userInfo);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

//就是下边这几句了
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//解析xml User user=(User) context.getBean("user");//得到对象 String userInfo=user.fun();//使用对象 response.getWriter().append(userInfo);//服务器输出

注意,如果User类中写了有参构造,而找不到无参构造,则<bean id="user" class="com.swift.User"></bean>这种约束会失败,无法成功创建对象,所以要加上无参构造,代码如下
package com.swift;

public class User {
    private String userName;
    public User(String s) {
        this.userName=s;
    }
    public User() {}
    
    public String fun() {
        return "User's fun is ready.";
    }
}

 换一种方法,使用静态工厂的方法

package com.swift;

public class BeanFactoryUser {
    public static User getUser() {
        return new User();
    }
}

 

类名.加static的方法——静态工厂的方法

这时xml配置文件增加

<bean id="beanFactory" class="com.swift.BeanFactoryUser" factory-method="getUser"></bean>

把静态方法也填上

Servlet类的代码如下:

package com.swift;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/test2")
public class TestIOCServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public TestIOCServlet2() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User) context.getBean("beanFactory");
        String s=user.fun();
        response.getWriter().println(s);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

 实例工厂的方法

package com.swift;

public class FactoryInstance {
    public User fun() {
    return new User();
    }
}

只有这么个非静态的方法,怎么通过xml配置文件得到User对象呢?

也是可以的

配置文件如下写法

<?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">
<!-- IoC 控制反转 SpringSpring根据XML配置文件生成对象 -->
<bean id="factoryInstance" class="com.swift.FactoryInstance"></bean>
<bean id="user2" factory-bean="factoryInstance" factory-method="getUser"></bean>
</beans>

Servlet类实现结果

package com.swift;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/test3")
public class TestIOCServlet3 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public TestIOCServlet3() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User) context.getBean("user2");
        String s=user.fun();
        response.getWriter().append(s);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 User user1=(User) context.getBean("user2");

 User user2=(User) context.getBean("user2");

得到的user1和user2是同一个对象,因默认情况下xml配置文件中是sigleton的单例模式

相当于<bean id="user" class="com.swift.User" scope="singleton"></bean>

多实例的对象模式——prototype(原型)

<bean id="user" class="com.swift.User" scope="prototype"></bean>

 

 


 

posted @ 2017-11-10 17:07  Advancing-Swift  阅读(543)  评论(0编辑  收藏  举报