J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el

一.Spring的建立过程

1.建立Spring的配置文件--->Spring.xml

J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el_第1张图片

Spring 中的Bean标签

J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el_第2张图片




    
        
        
        
            
                看电影
                购物
            
        
        
            
                
                
            
            
                
                
            
            
                
            
        
    
    
        
        
    
    
        

        
        
    

    
        

    
    
        

    
    
    
    
        
    

2.初始化Spring的框架

package com.westos.web02.util;

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

/**
 * 使用单例模式初始化spring
 * Created by Administrator on 2018/8/23.
 */
public class SpringUtil {
    private static SpringUtil instance;
    private ApplicationContext context;
    private SpringUtil(){
        String filename="spring.xml";
        //初始化spring
        context = new ClassPathXmlApplicationContext(filename);
    }

    public static SpringUtil getInstance(){
        if(instance==null){
            instance=new SpringUtil();
        }
        return instance;
    }

    public Object getBean(String name){
        return context.getBean(name);
    }
    public ApplicationContext getContext(){
        return context;
    }
}

3.使用Spring来建立对象

 

J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el_第3张图片

J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el_第4张图片

代码示例操作如下:

student.java--->

package com.westos.web02.bean;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/24.
 */
public class Student {
    private String xuhao;
    private String xingming;
    private List aihao;
    private Map chengji;

    @Override
    public String toString() {
        return "Student{" +
                "xuhao='" + xuhao + '\'' +
                ", xingming='" + xingming + '\'' +
                ", aihao=" + aihao +
                ", chengji=" + chengji +
                '}';
    }

    public Map getChengji() {
        return chengji;
    }

    public void setChengji(Map chengji) {
        this.chengji = chengji;
    }

    public List getAihao() {
        return aihao;
    }

    public void setAihao(List aihao) {
        this.aihao = aihao;
    }

    public String getXuhao() {
        return xuhao;
    }

    public void setXuhao(String xuhao) {
        this.xuhao = xuhao;
    }

    public String getXingming() {
        return xingming;
    }

    public void setXingming(String xingming) {
        this.xingming = xingming;
    }

}

teacher.java--->

package com.westos.web02.bean;

/**
 * Created by Administrator on 2018/8/24.
 */
public class Teacher {
    private String name;
    private Student student;
    private Student stu;

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", student=" + student +
                ", stu=" + stu +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void init(){
        System.out.println("Teacher的初始化,"+this.getName());
    }
}

client.java --->

package com.westos.web02.util;

import com.westos.web02.bean.Student;
import com.westos.web02.bean.Teacher;

/**
 * Created by Administrator on 2018/8/24.
 */
public class Client {
    public static void main(String[] args){
        Student stu = SpringUtil.getInstance().getContext().getBean("stu",Student.class);
        System.out.println(stu.hashCode());
        Student stu2 = SpringUtil.getInstance().getContext().getBean("stu",Student.class);
        System.out.println(stu2.hashCode());
        Teacher teacher=SpringUtil.getInstance().getContext().getBean("teacher",Teacher.class);
        Teacher teacher3=SpringUtil.getInstance().getContext().getBean("teacher",Teacher.class);
        Teacher teacher4=SpringUtil.getInstance().getContext().getBean("teacher",Teacher.class);
        System.out.println(teacher.getStudent());
        System.out.println(teacher);
        System.out.println(teacher3);
        System.out.println(teacher4);
        Teacher teacher2=SpringUtil.getInstance().getContext().getBean("teacher2",Teacher.class);
        System.out.println(teacher2.getStudent());

    }
}

client2.java--->

package com.westos.web02.util;

import com.westos.web02.bean.Student;
import com.westos.web02.bean.Teacher;
import com.westos.web02.dao.HaohanDao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * Created by Administrator on 2018/8/24.
 */
public class Client2 {

    public static void main(String[] args){
        //单例和多例的演示
        Teacher teacher2 = SpringUtil.getInstance().getContext().getBean("teacher2", Teacher.class);
        Teacher teacher21 = SpringUtil.getInstance().getContext().getBean("teacher2", Teacher.class);
        Teacher teacher3 = SpringUtil.getInstance().getContext().getBean("teacher3", Teacher.class);
        Teacher teacher31 = SpringUtil.getInstance().getContext().getBean("teacher3", Teacher.class);
        Student stu4 = SpringUtil.getInstance().getContext().getBean("stu4", Student.class);
        Student stu5 = SpringUtil.getInstance().getContext().getBean("stu5", Student.class);
        System.out.println("teacher2:"+teacher2);
        System.out.println("teacher21:"+teacher21);
        System.out.println("teacher3:"+teacher3);
        System.out.println("teacher31:"+teacher31);
        System.out.println("stu4:"+stu4);
        System.out.println("stu5:"+stu5);
        SqlSessionFactory sqlsessionFactory = SpringUtil.getInstance().getContext().getBean(SqlSessionFactory.class);
        SqlSession sqlsession = sqlsessionFactory.openSession();
        HaohanDao dao = sqlsession.getMapper(HaohanDao.class);
        System.out.println(dao.listNews());
        sqlsession.close();
    }
}

J2EE基本框架之Spring的建立,代码实现,重要jar包,spring-el_第5张图片

StudentFactory.java--->factory-method

package com.westos.web02.factory;

import com.westos.web02.bean.Student;

/**
 * Created by Administrator on 2018/8/24.
 */
public class StudentFactory {

    public static Student create(){
        return new Student();
    }
}

StudentFactoryBean.java

package com.westos.web02.factory;

import com.westos.web02.bean.Student;
import org.springframework.beans.factory.FactoryBean;

/**
 * Created by Administrator on 2018/8/24.
 */
public class StudentFactoryBean implements FactoryBean {
    @Override
    public Student getObject() throws Exception {
        return new Student();
    }

    @Override
    public Class getObjectType() {
        return Student.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

 MybatisFactoryBean.java--->mybatis 与 FactoryBean相结合

package com.westos.web02.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

/**
 * Created by Administrator on 2018/8/24.
 */
public class MybatisFactoryBean implements FactoryBean {
    private String resource;

    public String getResource() {
        return resource;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    @Override
    public SqlSessionFactory getObject() throws Exception {

        SqlSessionFactory sqlSessionFactory=null;
        try{
            InputStream in = Resources.getResourceAsStream(resource);
            //初始化sqlSession
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return sqlSessionFactory;
    }

    @Override
    public Class getObjectType() {
        return SqlSessionFactory.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

mybatis.xml文件




    
        
    
    
        
    

    
        
            
            
            
                
                
                
                
            
        
    
    
        
        
        
    


client4.java--->
 

4.使用Spring与westos   framework整合

二.Spring的jar包

1.BeanFactory  : Spring的核心jar包;

2.AplicationContext:ontext包的核心是ApplicationContext接口。它由BeanFactory接口派生而来,因而提供了BeanFactory所有     的功能。

三.Spring-el表达式

package com.westos.web02.util;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/24.
 */
public class Client4 {
    public static void main(String[] args){
        ExpressionParser parser = new SpelExpressionParser();
        String strel="";
        strel="'Hello World'+(1+2)";
        String result=(String)parser.parseExpression(strel).getValue();
        System.out.println(result);

        strel="#stu.xuhao"; //访问对象的属性
        strel="#stu.aihao[0]"; //访问对象属性中的列表

        EvaluationContext context=new StandardEvaluationContext();
        context.setVariable("stu",SpringUtil.getInstance().getBean("stu"));
        result=(String)parser.parseExpression(strel).getValue(context);
        System.out.println(result);
        strel="#stu.chengji[english]"; //访问对象属性中的map
        System.out.println(parser.parseExpression(strel).getValue(context));
    }
}

 

.

你可能感兴趣的:(J2EE,Spring)