Spring001

练习一

第一步:创建实体类 Person.java

package com.spring.beans;

/**
 * @auther zll
 * @create 2020/7/14-19:00
 */
public class Person {
    private String name;
    private String sayworld;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSayworld() {
        return sayworld;
    }

    public void setSayworld(String sayworld) {
        this.sayworld = sayworld;
    }

    public void say(){
        System.out.println(this.name+"说: "+this.sayworld);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sayworld='" + sayworld + '\'' +
                '}';
    }
}

 第二步:建立applicationContext.xml 文件,并在其中进行配置

<bean id="personSay" class="com.spring.beans.Person">
        <property name="name" value="张嘎"></property>
        <property name="sayworld" value="三天不打小鬼子,手都痒痒"></property>
    </bean>

    <bean id="personSay1" class="com.spring.beans.Person">
        <property name="name" value="Rod"></property>
        <property name="sayworld" value="世界上有两种人,认识二进制的和不认识二进制的"></property>
    </bean>

 第三步:在测试类Testperson.java 中进行测试

package com.spring.test;

import com.spring.beans.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @auther zll
 * @create 2020/7/14-19:08
 */
public class Testperson {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person personSay = (Person) context.getBean("personSay");

        System.out.println(personSay.toString());

        Person personSay1 = (Person) context.getBean("personSay1");

        System.out.println(personSay1.toString());


    }
}

 练习二:

第一步:创建Lnk(墨盒),和 Paper(纸张) 接口

public interface Lnk {

    public String getColor();
}
public interface Paper {
    public String Size();
}

 第二步:实现Lnk和Paper接口

public class A4Paper implements Paper {
    @Override
    public String Size() {
        return "A4";
    }

}
public class B5Paper implements Paper {
    @Override
    public String Size() {
       return "B5";
    }
}
public class LnkBlackWhite implements Lnk {
    @Override
    public String getColor() {
        return "blackwhite";
    }
}
public class LnkColor implements Lnk {
    @Override
    public String getColor() {
        return "color";
    }
}

 第三步:创建Printer.java 类

public class Printer {
    private Lnk lnk;
    private Paper paper;

    @Override
    public String toString() {
        return "Printer{" +
                "lnk=" + lnk.getColor() +
                ", paper=" + paper.Size() +
                '}';
    }

    public Lnk getLnk() {
        return lnk;
    }

    public void setLnk(Lnk lnk) {
        this.lnk = lnk;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }
}

 第四步:在applicationContext.xml 文件 配置

  <bean id="a4" class="com.spring.dao.impl.A4Paper"></bean>
    <bean id="b5" class="com.spring.dao.impl.B5Paper"></bean>
    <bean id="color" class="com.spring.dao.impl.LnkColor"></bean>
    <bean id="black" class="com.spring.dao.impl.LnkBlackWhite"></bean>

    <bean id="printer" class="com.spring.beans.Printer">
        <property name="lnk" ref="color"></property>
        <property name="paper" ref="a4"></property>
    </bean>

 第五步:进行测试

 

public class TestPrinter {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Printer p =  (Printer)context.getBean("printer");
        System.out.println(p.toString());
    }

}

 练习三:Aop

第一步:创建ArithMatic 接口

public interface ArithMatic {
    //加法运算
    public int add(int i , int j);
}

 第二步:实现接口

public class ArithMaticImpl implements ArithMatic {
    @Override
    public int add(int i, int j) {
        int res = i+j;
        System.out.println("正在计算中。。。。。。");
        return res;
    }
}

 第三步:创建 切面类ArithMaticImplLogger

public class ArithMaticImplLogger {

    public void beforeLogger(JoinPoint joinPoint){

        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The Method "+methodName+" with "+args);
    }

    public void afterLogger(JoinPoint joinPoint ,Object object){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The Method "+methodName+ " ends res: "+object);
    }
}

 第四步:进行xml配置

<bean id="Arith" class="com.spring.aop.ArithMaticImpl"></bean>

    <bean id="ArithLogger" class="com.spring.aop.ArithMaticImplLogger"></bean>
    <aop:config>
<!--        配置切入点-->
        <aop:pointcut id="pointcut" expression="execution(public int com.spring.aop.ArithMaticImpl.add(int,int))"/>
<!--        切面具体配置-->
        <aop:aspect ref="ArithLogger">
<!--             前置增强-->
            <aop:before method="beforeLogger" pointcut-ref="pointcut"></aop:before>
<!--              后置增强-->
            <aop:after-returning method="afterLogger" pointcut-ref="pointcut" returning="object"></aop:after-returning>
        </aop:aspect>
    </aop:config>

 第五步:测试

public class TestArithMaticImpl {
    @Test
    public  void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ArithMatic arith = (ArithMatic) context.getBean("Arith");
        arith.add(1, 2);

    }
}

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值