Spring 和aop初步

本文介绍Spring框架的基本使用方法,包括配置依赖、创建Service类和服务实例,并通过示例展示了如何利用Spring实现依赖注入(DI)及控制反转(IoC),帮助读者快速掌握Spring的核心概念。
Spring 作用:管理各种业务Bean(dao,servcie,action)


IOC和DI ,可以看成是一个东东。
IOC 思想控制反转
什么是控制反转:


DI 实现的层面,依赖注入。
IOC和DI可以看成是站在不同维护来看待问题的方式,其实他们的思想是一致的。
1.控制反转(IOC Inverse Of Control)
HappyService happyService;
try
{
  
}catch(Exception ex)
{
 ex.Message
}
7.Spring第一个入门案例
   1.引入节点
      <!--beans  自动将依赖的core引入了-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.3.RELEASE<ersion>
    </dependency>
    <!--context 依赖N个-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.2.RELEASE<ersion>
    </dependency>


 2.书写一个Service类
  public class SomeService {
    //info
    private String info;




    //work
    public  void work(){
        System.out.println("Hello"+info);
    }




    public String getInfo() {
        return info;
    }


    public void setInfo(String info) {
        this.info 


 = info;
    }
}




3.      # printer
            #  ink
                 # ColorInk


public class ColorInk implements ink {


    public String getColor() {
        return "太阳墨盒";
    }
}


                 # GrayInk
public class GrayInk implements ink
{


    public String getColor() {
        return "权萌萌墨盒";
    }
}
                 # ink
public interface ink {
//啥颜色啊????
    public String getColor();
}
             # Paper
                   # A4page
public class A4page implements Paper {
    public String getPaper() {
        return "A4的纸";
    }
}


                   #B5Paper
 */
public class B5Paper implements Paper{


    public String getPaper() {
        return "俺是B5的纸";
    }
}


                   #Paper
public interface Paper {


        public String getPaper();




}


             # print   
                    #printer
public class Printer {
    //域属性
    private ink ink;


    //纸张 接口
    //域属性
    private Paper paper;


    public void print(){
        System.out.println("用"+ink.getColor()+"\t颜色的墨盒在"+paper.getPaper()+"\t类型的纸张上打印出来 我爱你中国");
    }




    public cn.happy.printer.ink.ink getInk() {
        return ink;
    }


    public void setInk(cn.happy.printer.ink.ink ink) {
        this.ink = ink;
    }


    public Paper getPaper() {
        return paper;
    }


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




        # Service
              #HappyService
public class HappyService {
    private String info;
  public void work()
  {
      System.out.println("work"+info);
  }


    public String getInfo() {
        return info;
    }


    public void setInfo(String info) {
        this.info = info;
    }
}


4.在src下创建一个名称为applicationContext.xml的文件






 <!--准备一个彩色墨盒-->
        <bean id="colorInk" class="cn.happy.printer.ink.ColorInk"></bean>


        <!--准备一个B5纸-->
        <bean id="b5Paper" class="cn.happy.printer.Paper.B5Paper"></bean>
        <bean id="a4Paper" class="cn.happy.printer.Paper.A4page"></bean>
        <!--准备一台打印机-->
        <bean id="pinter" class="cn.happy.printer.print.Printer">


                <property name="ink" ref="colorInk" ></property>
                <property name="Paper" ref="a4Paper"></property>
        </bean>
5.书写测试类
@Test
    public void test01(){


            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        HappyService service=(HappyService)ctx.getBean("happyService");
        service.setInfo("spring");
        service.work();
    }
    @Test
    //打印机案例
    public void test02(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //装配出一台打印机


        Printer pp=  (Printer)ctx.getBean("pinter");
        pp.print();
    }
AOP的目标:
AOP原理:
将复杂的需求分解出 不同方面
将散布在 系统中的公共功能集中解决采用代理机制组装起来运行,在不改变原程序

的基础上对代码进行增强处理、增加新的功能













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值