zoukankan      html  css  js  c++  java
  • JAVA_OA管理系统(二):SpringMVC笔记基础篇01注入方法

    配置

    springapplicationContext.xml导入src根目录(建包导入也可以,建包的标准com.Throne.Font.until),

    导入jar,将文件复制粘贴进入WebRootWEB-INFlib.

    实例化时:

    实例化方法如下:
              String conf = "applicationContext.xml";
              ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
    
    
     
    实例化时,类型强转-Student s=ac.getBean("student",Student.class);(建议的标准)
     
    xml配置JavaBean:
        <bean id="student"  class="com.Throne.Font.entity.Student"></bean>
    
    


     scope属性


    Spring也可以管理Java中的类,比如:
    XML文件中:
    <bean id="c" class="java.util.Calendar" factory-method="getInstance"></bean>
    
    
           JAVA文件中:
            Calendar C = ac.getBean("c",Calendar.class);
    
    
    


    静态工厂模式写出的 C ,默认也是单例的,地址相同.

            Student s=ac.getBean("student",Student.class);
            Student s1=ac.getBean("student",Student.class);
            System.out.println(s==s1);


          结果:

            True

    可以通过修改bean的作用域,来改变创建模式

          修改scope属性,用来指明bean的作用域

    <bean id="student"  class="com.Throne.Font.entity.Student" scope="prototype"></bean>


    此时每次创建方式为实例工厂,每次创建都会new新的对象

    implements Serializable

    进行序列化。

    推荐博文:我对Java Serializable(序列化)的理解和总结

    Bean的生命周期:

     <bean id="JavaBeanexample" class="com.Throne.Font.entity.ExampleBean" init-method="init" destroy-method="destroy" lazy-init="true"></bean>
    
    
     
    //init-method:创建时实例化
    //Destroy-method:销毁方法
    //Lazy-init:实例化延迟
     
    //销毁在容器停止时执行,Close:调用容器的 close方法 
    //用ApplicationContext的父接口AbstractApplicationContext;
    //比如:
    AbstractApplicationContext  ac=new ClassPathXmlApplicationContext(str);
    ac.close;
     




    spring的注入方式

    基本步骤(该部分为引用)

            Spring注入的基本步骤如下:

    1. 将所有的类在spring-conf.xml中创建bean

      语法如下:

      <bean id="beanId" class="包名.类名">

    2. 对所有依赖的类进行注入

      1. 如果是属性注入,需要为每一个依赖类创建相应的getter和setter方法

      2. 如果是构造方法注入,需要为依赖类创建相应的构造方法            

    3.  在测试方法中测试是否正确注入 


    Spring的配置注入(setter)

    通过调用无参构造器或者无参static 工厂方法实例化bean之后,调用该bean

     setter方法,即使set注入

    Java:

    public class Book implements Serializable{
    	private int id;
    	private String name;
    	public Book() {
    	}
    	
    	public Book(int id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	


    配置:

    <bean id="book" class="com.Throne.Font.entity.Book">
       <property name="id" value="12"></property>
       <property name="name" value="平凡的世界"></property>
     </bean>


    调用:

    Book b=ac.getBean("book",Book.class);
    System.out.println(b.getId());
    System.out.println(b.getName());


    构造器注入:

    JAVA:

    private int id;
    	private String name;
    	public Book1(int id, String name) {
    		super();
    		this.id = id;
    		this.name = name;
    	}
    	public String toString() {
    		return "Book1 [id=" + id + ", name=" + name + "]";
    	}


    XML配置:

    <bean id="book33" class="com.Throne.Font.entity.Book1">
       <property name="id" value="121"></property>
       <property name="name" value="平凡的世界"></property>
     </bean>


    调用:

    Book1 b1=ac.getBean("book1",Book1.class);
    System.out.println(b1.toString());


    结果:

    [id=121,name=平凡的世界]

    自动装配:

    略微了解即可,在自动装配太多时,容易造成混乱.故不常用.

    XML文件:

     <bean id="teacher" class="com.Throne.Font.entity.Teacher"
          autowire="byType">
       </bean>


      autowire的参数值可选有:“default”、“byName”、“byType”、“constructor”、“no”。

            default:根据bean的自省机制决定采用byType还是constructor进行自动装配,如果Bean提供了默认的构造函数,则采用byType,否则采用constructor。

            byName:通过属性名自动注入。

            byType:通过属性类型自动注入。

            constructor:与byType相同,但作用于构造器。

            no:不自动注入。

     

    JAVA文件:

    public class Teacher  implements Serializable{
    	 private int id;
    	 private Book1 book;
    	 
    	public Teacher() {
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public Book getBook() {
    		return book;
    	}
    	public void setBook(Book book) {
    		this.book = book;
    	}
    }


    执行:

    Teacher t=ac.getBean("teacher",Teacher.class);
    System.out.println(t.getBook().getName());
    
    

    结果:

    平凡的世界


    除此之外:

    也可以进行Annotation方式(其不太常用,多用于一些小项目)

    参考文章:

    Spring注入方式介绍


  • 相关阅读:
    C++的开源跨平台日志库glog学习研究(一)
    C++实现的字符串模糊匹配
    Git&GitHub学习日志
    UTF-8和GBK等中文字符编码格式介绍及相互转换
    HDU
    340. 通信线路(分层图最短路)
    ACwing 你能回答这些问题吗(线段树求最大连续字段和)
    Laptop(线段树+离散化)
    Infinite Inversions(树状数组+离散化)
    HDU-4417-Super Mario(主席树解法)
  • 原文地址:https://www.cnblogs.com/fonttian/p/7294866.html
Copyright © 2011-2022 走看看