zoukankan      html  css  js  c++  java
  • spring属性的三种注入方法

    (1)使用set方法:

    public class Book {
      private String bookname;
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    xml配置:

    <bean id="book" class="com.example.propetys.Book">
    <!-- 注入属性值 name属性:值是你定义的属性的名称,value属性:设置的具体的值 -->
    <property name="bookname" value="九阳真经"></property>
    </bean>

    (2)有参数的构造

    public class PropertDemo1 {
        private String username;
        public PropertDemo1(String username) {
            this.username = username;
        }

    xml配置:

    <bean id="demo" class="com.example.propetys.PropertDemo1">
    <!-- 有参构造方法注入 -->
        <constructor-arg name="username" value="liuguxiia"></constructor-arg>
     </bean>

    (3)使用接口注入

    public Interface Dao{

      public void delete(String name);

    }

    public class DaoImpl implements Dao{

       private String name;

       public void delete(String name){

           this.name=name;

          }

    }

    在spring框架里,只允许前两种方式。

  • 相关阅读:
    Vue 04
    Vue小练习 03
    Vue 03
    Vue小练习 02
    Vue 02
    Vue 小练习01
    Vue 01
    Django 11
    JUC(一):volatile关键字
    Kubernetes【K8S】(五):Service
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8682349.html
Copyright © 2011-2022 走看看