栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring初见

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring初见

一、Spring框架

1、Spring是一个由Java语言开发的框架。

2、Spring在项目开发时主要提供两种服务:IOC服务/AOP服务

3、目前为止,所有的Java项目中都会使用Spring框架

二、IOC服务

1、IOC服务,意为“控制反转”

2、IOC服务中,可以由Spring容器为开发人员提供项目运行时需要的对象

3、IOC服务简单来说,就是由Spring容器负责帮你创建需要的对象

三、Spring容器对象:

1、容器:存储对象的载体。一般指的是List,Set,Map,数组

2、容器对象:如果某个对象中的属性是集合,且这个集合用于存储其他的对象

四、IOC最终认识
  • 之前在开发时,需要某个对象时,需要自己来负责创建;IOC服务中,当你需要某个对象时,可以向Spring容器对象索要。

五、为什么需要由Spring容器提供需要的对象
  • 在开发时,某些对象的获得相对困难且繁琐,例如jdbc中的PreparedStatment对象和MyBatis中的SqlSession对象。

    • 1、Class.forName(Driver接口);

    • 2、Connection con = DriverManager.getConnection();

    • 3、PreparedStatment ps = con.preparedStatment();

    • 1、InputStream in = Resources.getResourceAsStream("mybatis.xml");-

    • 2、 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

    • 3、SqlSession sqlSession = factory.openSession(true);

public class Dept {
    private Integer deptNo;
    private String dname;
    private String loc;
​
    @Override
    public String toString() {
        return "Dept{" +
                "deptNo=" + deptNo +
                ", dname='" + dname + ''' +
                ", loc='" + loc + ''' +
                '}';
    }
​
    public Integer getDeptNo() {
        return deptNo;
    }
​
    public void setDeptNo(Integer deptNo) {
        this.deptNo = deptNo;
    }
​
    public String getDname() {
        return dname;
    }
​
    public void setDname(String dname) {
        this.dname = dname;
    }
​
    public String getLoc() {
        return loc;
    }
​
    public void setLoc(String loc) {
        this.loc = loc;
    }
}
//====================================================
public class Emp {
    private Integer empNo;
    private String ename;
​
    @Override
    public String toString() {
        return "Emp{" +
                "empNo=" + empNo +
                ", ename='" + ename + ''' +
                '}';
    }
​
    public Integer getEmpNo() {
        return empNo;
    }
​
    public void setEmpNo(Integer empNo) {
        this.empNo = empNo;
    }
​
    public String getEname() {
        return ename;
    }
​
    public void setEname(String ename) {
        this.ename = ename;
    }
}
public class ApplicationContext {
​
    private static Map map ;//存储项目开发时的实例对象
​
    //Spring 容器类第一次被加载时,将项目中指定类的对象创建出来保存到容器
    static {
        map = new HashMap();
        map.put("dept",new Dept());
        map.put("emp",new Emp());
    }
​
​
    public static Object getBean(String key ){
        return map.get(key);
    }
}
public class Tes_IOC {
    public static void main(String[] args) {
        Dept dept = (Dept) ApplicationContext.getBean("dept");
        Emp emp = (Emp) ApplicationContext.getBean("emp");
        System.out.println("dept对象"+dept);
        System.out.println("emp对象"+emp);
    }
}
六、Spring框架中的DI服务:
  • 1、DI服务:依赖注入

  • 2、DI服务从IOC服务衍生的一个服务,用于帮助开发人员为对象的属性进行初始化操作

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Value {
    //声明Value注解中的属性
    public String value() default "";
}
public class Dept {
​
    @Value(value = "10")
    private Integer deptNo;
    @Value(value = "Accounting")
    private String dname;
    private String loc;
​
    @Override
    public String toString() {
        return "Dept{" +
                "deptNo=" + deptNo +
                ", dname='" + dname + ''' +
                ", loc='" + loc + ''' +
                '}';
    }
​
    public Integer getDeptNo() {
        return deptNo;
    }
​
    public void setDeptNo(Integer deptNo) {
        this.deptNo = deptNo;
    }
​
    public String getDname() {
        return dname;
    }
​
    public void setDname(String dname) {
        this.dname = dname;
    }
​
    public String getLoc() {
        return loc;
    }
​
    public void setLoc(String loc) {
        this.loc = loc;
    }
}
//==================================================
public class Emp {
    @Value(value = "9527")
    private Integer empNo;
    @Value(value = "pm")
    private String ename;
​
    @Override
    public String toString() {
        return "Emp{" +
                "empNo=" + empNo +
                ", ename='" + ename + ''' +
                '}';
    }
​
    public Integer getEmpNo() {
        return empNo;
    }
​
    public void setEmpNo(Integer empNo) {
        this.empNo = empNo;
    }
​
    public String getEname() {
        return ename;
    }
​
    public void setEname(String ename) {
        this.ename = ename;
    }
}
public class ApplicationContext {
​
    private static Map map ;//存储项目开发时的实例对象
​
    //Spring 容器类第一次被加载时,将项目中指定类的对象创建出来保存到容器
    static {
        map = new HashMap();
        try {
            map.put("dept",init(Dept.class));
            map.put("emp",init(Emp.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
​
    //侦测指定类中是否绑定了@Value注解,如若绑定,那么根据注解中的值对当前属性进行赋值
    private static Object init(Class classManager)throws Exception{
        Object obj = null;
        Field fieldArray[] = null;
        //1、通过class文件来申请创建当前类的实例对象
        obj = classManager.newInstance();
        //2、通过class文件得到当前类文件中所有的属性信息
        fieldArray = classManager.getDeclaredFields();
        //3、循环遍历,如果发现某个属性上绑定了@Value注解,则需要将注解中的值赋值给当前属性
        for (Field field:fieldArray){
            //3.1、询问当前属性是否关联了@Value注解
            if (field.isAnnotationPresent(Value.class)) {
                Value value = field.getAnnotation(Value.class);
                //从当前注解对象中得到value属性的值
                String field_Value = value.value();
                //将注解中的值赋值到当前对象的属性
                field.setAccessible(true);
                String typeName = field.getType().getSimpleName();
                if ("Inteder".equals(typeName)) {
                    field.set(obj, Integer.valueOf(field_Value));
                } else if ("Double".equals(typeName)) {
                    field.set(obj, Double.valueOf(field_Value));
                } else if ("String".equals(typeName)) {
                    field.set(obj, field_Value);
                }
            }
        }
        return obj;
    }
​
    public static Object getBean(String key ){
        return map.get(key);
    }
}
public class Test_DI {
    public static void main(String[] args) {
​
        Emp emp = (Emp) ApplicationContext.getBean("emp");
        System.out.println(emp.toString());
​
        Dept dept = (Dept) ApplicationContext.getBean("dept");
        System.out.println(dept.toString());
​
    }
}
七、通过xml文件向Spring容器索要IOC服务
  • 开发环境搭建

    • 1、向Maven索要Spring框架中spring-context.jar

    • 2、在resources下创建Spring框架的核心配置文件,通过该配置文件通知Spring容器当前项目中哪些类的对象需要由Spring容器来提供


    org.springframework
    spring-context
    5.2.5.RELEASE
    
​
    
public class Student {
    public void sayHello(String name){
        System.out.println("hello : " + name);
    }
}
public class TestMain {
    
    @Test
    public void test1(){
        //1、获取Spring容器对象,Spring容器对象在创建时自动读取指定的Spring核心配置。
        ApplicationContext spring = new ClassPathXmlApplicationContext("spring.xml");
        //2、向Spring容器对象索要Student类对象
        Student stu = (Student) spring.getBean("student");
        stu.sayHello("pm");
    }
​
    @Test
    public void test2(){
        //1、获取Spring容器对象,Spring容器对象在创建时自动读取指定的Spring核心配置。
        ApplicationContext spring = new ClassPathXmlApplicationContext("spring.xml");
        //2、连续向Spring容器索要两个Student类型对象
        Student stu1 = (Student) spring.getBean("student");
        Student stu2 = (Student) spring.getBean("student");
        System.out.println("stu1:" + stu1.toString());
        System.out.println("stu2:" + stu1.toString());
    }
​
    @Test
    public void test3(){
        //1、获取Spring容器对象,Spring容器对象在创建时自动读取指定的Spring核心配置。
        ApplicationContext spring = new ClassPathXmlApplicationContext("spring.xml");
        //2、获得Spring容器对象中所管理的所有实例对象的key
        String keys[] = spring.getBeanDefinitionNames();
        for (String key :
                keys) {
            System.out.println("key = "+key);
        }
    }
}
test1结果: hello : pm
test2结果:stu1:com.pm.entity.Student@6acdbdf5
          stu2:com.pm.entity.Student@6acdbdf5  
test3结果:key = student
八、通过xml文件向Spring容器索要DI服务
  • part1

public class School {
​
     private String shcoolName;
     private String address;
​
     public String getShcoolName() {
          return shcoolName;
     }
​
     public void setShcoolName(String shcoolName) {
          this.shcoolName = shcoolName;
     }
​
     public String getAddress() {
          return address;
     }
​
     public void setAddress(String address) {
          this.address = address;
     }
​
     @Override
     public String toString() {
          return "School{" +
                  "shcoolName='" + shcoolName + ''' +
                  ", address='" + address + ''' +
                  '}';
     }
}
         
               
               
         
public class TestMain {
​
    @Test
    public void test1(){
        ApplicationContext spring = new ClassPathXmlApplicationContext("pack1/spring.xml");
        School school=(School) spring.getBean("school");
        System.out.println(school.toString());
    }
}
  • part2

public class School {
​
     private String shcoolName;
     private String address;
​
     public String getShcoolName() {
          return shcoolName;
     }
​
     public void setShcoolName(String shcoolName) {
          this.shcoolName = shcoolName;
     }
​
     public String getAddress() {
          return address;
     }
​
     public void setAddress(String address) {
          this.address = address;
     }
​
     @Override
     public String toString() {
          return "School{" +
                  "shcoolName='" + shcoolName + ''' +
                  ", address='" + address + ''' +
                  '}';
     }
}
//====================================================================
public class Student {
​
        private int sid;
        private String sname;
        private School school; //表示学员隶属的学校
        private List list;
​
    public List getList() {
        return list;
    }
​
    public void setList(List list) {
        this.list = list;
    }
​
    public int getSid() {
        return sid;
    }
​
    public void setSid(int sid) {
        this.sid = sid;
    }
​
    public String getSname() {
        return sname;
    }
​
    public void setSname(String sname) {
        this.sname = sname;
    }
​
    public School getSchool() {
        return school;
    }
​
    public void setSchool(School school) {
        this.school = school;
    }
​
    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + ''' +
                ", school=" + school +
                '}';
    }
}
         
         
         
               
               
         
​
​
         
         
             
             
             
             
         
public class TestMain {
​
    @Test
    public void test1(){
        ApplicationContext spring = new ClassPathXmlApplicationContext("pack2/spring.xml");
        Student student=(Student)spring.getBean("student");
        System.out.println(student.toString());
    }
}
  • part3

public class Student {
​
        private int sid;
        private String sname;
​
    public int getSid() {
        return sid;
    }
​
    public void setSid(int sid) {
        this.sid = sid;
    }
​
    public String getSname() {
        return sname;
    }
​
    public void setSname(String sname) {
        this.sname = sname;
    }
​
    public Student(int sid, String sname) {
        this.sid = sid;
        this.sname = sname;
    }
​
    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + ''' +
                '}';
    }
}
         
        
             
             
         
public class TestMain {
​
    @Test
    public void test1(){
        ApplicationContext spring = new ClassPathXmlApplicationContext("pack3/spring.xml");
        Student stu = (Student)spring.getBean("student");
        System.out.println(stu.toString());
    }
}
test1结果:School{shcoolName='卷王学院', address='大湾区卷王9527房'}
test2结果:Student{sid=10, sname='mike', school=School{shcoolName='卷王学院', address='大湾区卷王9527房'}}    
test3结果:Student{sid=20, sname='mike'}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号