JAVA_OA管理系统(二):SpringMVC笔记基础篇01注入方法
配置
將spring的applicationContext.xml導入src根目錄(建包導入也可以,建包的標準com.Throne.Font.until),
導入jar包,將文件復制粘貼進入\WebRoot\WEB-INF\lib下.
?
?
實例化時:
實例化方法如下: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屬性
?
?
靜態工廠模式寫出的 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注入的基本步驟如下:
將所有的類在spring-conf.xml中創建bean
語法如下:
<bean id="beanId" class="包名.類名">
對所有依賴的類進行注入
如果是屬性注入,需要為每一個依賴類創建相應的getter和setter方法
如果是構造方法注入,需要為依賴類創建相應的構造方法????????????
?在測試方法中測試是否正確注入?
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注入方式介紹
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的JAVA_OA管理系统(二):SpringMVC笔记基础篇01注入方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java_OA管理系统(一):Servl
- 下一篇: JAVA_OA管理系统(三):Sprin