zoukankan      html  css  js  c++  java
  • spring bean

    1.类库

    2构造注入

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <!-- -->
        <bean id="student" class="com.lzp.bean.Student" init-method="before"
            destroy-method="afert">
            <constructor-arg value="15" />
            <constructor-arg ref="book"></constructor-arg>
        </bean>
        <bean id="student2" class="com.lzp.bean.Student">
    
            <constructor-arg ref="book"></constructor-arg>
        </bean>
        <bean id="book" class="com.lzp.bean.Book">
            <constructor-arg value="语文"></constructor-arg>
        </bean>
    
        <bean id="stage" class="com.lzp.bean.Stage" factory-method="getInstance">
        </bean>
    
    </beans>

    3.属性注入

    person类

    package com.lzp.bean;
    
    public interface Person {
        public void speak();
        public String getName();
    }

    account类

    public class Account implements Person{
    
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public void speak() {
            // TODO Auto-generated method stub
        }
    }

    contract类

    public interface Contract {
        public void print();
    }

    xdContract类

    public class XDContract implements Contract{
        
        
        
        private Person person;
         
        public Person getPerson() {
            return person;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
        private double totalLoan;
         
        public double getTotalLoan() {
            return totalLoan;
        }
        public void setTotalLoan(double totalLoan) {
            this.totalLoan = totalLoan;
        }
        
        private List<Repay> repayList;
        
        
        public List<Repay> getRepayList() {
            return repayList;
        }
        public void setRepayList(List<Repay> repayList) {
            this.repayList = repayList;
        }
        @Override
        public void print() {
            // TODO Auto-generated method stub
            System.out.println(person.getName()+"贷款金额为"+totalLoan);
            for (Repay repay : repayList) {
                System.out.println(repay.getMonth()+"月,应该还款金额为"+repay.getRepayTotal());
                
            }
        }
        
    }

     repay类

    public class Repay {
        private int month;
        private double repayTotal;
        public int getMonth() {
            return month;
        }
        public void setMonth(int month) {
            this.month = month;
        }
        public double getRepayTotal() {
            return repayTotal;
        }
        public void setRepayTotal(double repayTotal) {
            this.repayTotal = repayTotal;
        }
    }

    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"
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <!-- -->
        <bean id="contract" class="com.lzp.bean.XDContract">
            <property name="person" ref="person" />
            <property name="totalLoan" value="100000.00"></property>
            <property name="repayList">
                <list>
                    <ref bean="repayOne" />
                    <ref bean="repayTwo" />
                </list>
            </property>
        </bean>
    
        <bean id="person" class="com.lzp.bean.Account">
            <property value="张三" name="name"></property>
        </bean>
    
        <bean id="repayOne" class="com.lzp.bean.Repay">
            <property name="month" value="1"></property>
            <property name="repayTotal" value="60000"></property>
        </bean>
        <bean id="repayTwo" class="com.lzp.bean.Repay">
            <property name="month" value="2"></property>
            <property name="repayTotal" value="70000"></property>
        </bean>
    </beans>

    测试代码

    @Test
    	public void testProperty() {
    		ApplicationContext context = new ClassPathXmlApplicationContext(
    				"contract.xml");
    		Contract contract = (Contract) context.getBean("contract");
    		contract.print();
    	}
    

      

  • 相关阅读:
    OpenGL 一
    Git-SSH
    CGAffineTransform 视频旋转
    UI学习笔记---第十一天UITableView表视图高级-自定义cell
    UI学习笔记---第十天UITableView表视图编辑
    UI学习笔记---第九天UITableView表视图
    UITextView 点return 隐藏键盘
    UI学习笔记---第八天
    UI学习笔记---第七天
    UI学习笔记---第六天
  • 原文地址:https://www.cnblogs.com/lzhp/p/5553162.html
Copyright © 2011-2022 走看看