Spring根据XML配置文件生成对象

commons-logging 和log4j包下载 Spring依照XML配置文件生成对象,

亟需用到Spring压缩包中的三个核心JAWrangler包

beans 、context、core 和expression

下载地址:

以及日志jar包

commons-logging 和log4j

下载地址:


 

成立一个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 控制反转 Spring生成对象小实例 -->
<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);
    }

}

 

 

 

 

和log4j包下载
Spring依照XML配置文件生成对象, 需求用到Spring压缩包中的两个宗旨JA大切诺基包
beans 、context、core 和expression 下载地址:…

急需用到Spring压缩包中的八个主旨JALX570包

beans 、context、core 和expression

下载地址:

以及日志jar包

commons-logging 和log4j

下载地址:


 

开创四个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();
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注