欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > windows >内容正文

windows

JAVA_OA管理系统(二):SpringMVC笔记基础篇01注入方法

发布时间:2025/3/21 windows 63 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JAVA_OA管理系统(二):SpringMVC笔记基础篇01注入方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

配置

springapplicationContext.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属性


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注入的基本步骤如下:

  • 将所有的类在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注入方法的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。