• java高级教程 注入装配问题


    Stu.java

     

    package com.yan;

    import java.util.List;

    public class Stu {

        private String username;

        private Integer xuehao;

        private List<String> list;

        public Stu(String username,Integer xuehao,List<String> list) {

           super();

           this.username = username;

           this.xuehao = xuehao;

           this.list = list;

        }

        public Stu() {

           super();

        }

        public void setUsername(String username) {

           this.username = username;

        }

        public void setXuehao(Integer xuehao) {

           this.xuehao = xuehao;

        }

        public void setList(List<String> list) {

           this.list = list;

        }

        public String toString() {

           return "User {学生=" + username + ", 学号=" + xuehao + ", list=" + list + "}";

        }

     

    }

    GraStu.java

     

    package com.yan;

    import com.yan.Stu;

    public class GraStu {

        private String teacher;

        private Stu m;

        private Stu n;

        public void setStu1(Stu n){

           this.n = n;

        }

        public void setStu(Stu m){

           this.m = m;

        }

        public String teacher() {

           return "teacher";

        }

        public GraStu(String teacher) {

           super();

           this.teacher = teacher;

        }

        public GraStu() {

           super();

        }

        public void setTeacher(String teacher) {

           this.teacher = teacher;

        }

        public void setN(Stu n) { this.n = n;}

        public void setM(Stu m) { this.m = m;}

        public String toString1() {

           return "User {" + n + " 老师:" + teacher + "}";

        }

        public String toString() {

           return "User {" + m + " 老师:" + teacher + "}";

        }

     

    }

    abc.xml

     

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

        <!-- 将指定类配置给Spring,让Spring创建其对象的实例 -->

        <bean id="stu1" class="com.yan.Stu" >

            <constructor-arg index="0" value="耀哥" />

            <constructor-arg index="1" value="133233" />

            <constructor-arg index="2">

               <list>

                  <value>"constructorvalue1"</value>

                  <value>"constructorvalue2"</value>

               </list>

            </constructor-arg>

        </bean>

        <bean id="stu2" class="com.yan.Stu">

             <property name="username" value="小耀耀"></property>

             <property name="xuehao" value="11111111"></property>

             <property name="list">

                 <list>

                     <value>"setlistvalue1"</value>

                     <value>"setlistvalue2"</value>

                 </list>        

             </property>

        </bean>

        <bean id="userGrad" class="com.yan.GraStu">

            <property name="m" ref="stu1" />

            <property name="teacher" value="小明"></property>

        </bean>

            <bean id="useGrad" class="com.yan.GraStu">

            <property name="m" ref="stu2" />

            <property name="teacher" value="小王"></property>

        </bean>

    </beans>

    Text.java

     

    package com.yan;

    import org.springframework.context.ApplicationContext;

    import

         org.springframework.context.support.ClassPathXmlApplicationContext;

    import org.springframework.context.support.AbstractApplicationContext;

    @SuppressWarnings("unused")

    public class Text {

        public static void main(String[] args) {

           String xmlPath = "com/yan/abc.xml";

           ApplicationContext applicationContext =

                        new ClassPathXmlApplicationContext (xmlPath);

           System.out.println(applicationContext.getBean("stu1"));

           System.out.println(applicationContext.getBean("stu2"));

           System.out.println(applicationContext.getBean("userGrad"));

           System.out.println(applicationContext.getBean("useGrad"));

        }

    }

  • 相关阅读:
    hdu1124 Factorial (求解一个数的阶乘中出现多少个0)
    SQL on Linux: Erro Unable to read instance id from /var/opt/mssql/.system/instance_id
    Error during WebSocket handshake: Unexpected response code: 200 问题处理
    CUP计算资源争抢通过IIS启用处理器关联解决
    ABP在MultipleDbContext也就是多库的场景下发布后异常“Could not find content root folder”问题处理
    ABP运行Login failed for user 'IIS APPPOOL XXXXX Reason: Could not find a login matching the name provided问题解决
    vs2017cpu占用过高解决方案
    docker查看挂载目录Volume
    windows 10安装docker一直挂起在Installing Components and Removing Files
    ABP vue+asp.net core yarn serve报 Cannot find module 'typescript/package.json错误
  • 原文地址:https://www.cnblogs.com/1314-520/p/12635966.html
Copyright © 2020-2023  润新知