spring学习笔记之IOC

SPING核心概念:IOC(inversion of control)/DI(dependency injection)、AOP(aspect oriented programming)

控制反转(IOC): 传统的对象调用,a对象调用a对象的实现类,必须实现a a=new aImpl();如此一来就会导致产生一定的耦合。而控制反转是将调用对象这

一过程反转,将这一过程交给第三方IOC 容器来处理,从而a和aImpl之间理论上没有直接的耦合。

依赖注入(DI): 是在调用过程中将a所需要的类的方法或字段动态注入到ioc中,是控制反转的另一种叙述方式。

面向切面编程(AOP): 传统的编程思想是面向对象编程(OOP),是将万物看成对象,来处理一些程序中功能重复而又大量存在的代码块。

AOP(Aspect-Oriented Programming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。传统的OOP面向对象

编程关系是纵向的,一层一层的结构。而AOP是横向的,在程序各个模块中横切一刀,进行拦截。

IOC的实现:

 依赖注入的方式:

(1)set注入:property

class one: Man.java

package xz.learn.ioc;

import xz.learn.interfaces.animal;

public class Man implements animal{
    
    private Cat cat;
    
    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    private Dog dog;

    @Override
    public void eat() {
        System.out.println("人什么都敢吃。。");
    }
    
    public Man(){
        System.out.println("class man!!!");
        eat();
    }

}

 class two : Cat.java

package xz.learn.ioc;

import xz.learn.interfaces.animal;

public class Cat implements animal{

    @Override
    public void eat() {
        System.out.println("猫吃鱼。。");
    }
    
    public Cat() {
        System.out.println("class cat!!!");
        eat();
    }

}
package xz.learn.ioc;

import xz.learn.interfaces.animal;

public class Dog implements animal{
    
    public void eat(){
        System.out.println("狗吃骨头。。");
    }
    public Dog() {
        System.out.println("class dog!!!");
        eat();
    }
}
package xz.learn.interfaces;

public interface animal {
    
    public void eat();
    
}
package xz.learn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xz.learn.ioc.Man;

public class xzTest {
    public static void main(String[] args){
        
        ApplicationContext apc= new ClassPathXmlApplicationContext("xz/learn/pz/spring.xml");
        
        Man man=(Man)apc.getBean("man");
        
        System.out.println(man);
    }
}

结果显示: class man!!!
      人什么都敢吃。。
      class cat!!!
      猫吃鱼。。
      class dog!!!
      狗吃骨头。。
      xz.learn.ioc.Man@4e280c

 就是在这个过程中实现的了控制反转,将对象的实现交给IOC容器去实现,进而达到降低耦合的目的。

(2)构造注入:constructor-arg

修改配置文件spring.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.xsd" >
        
        <bean id="man" class="xz.learn.ioc.Man">
            <constructor-arg name="cat" ref="cat"></constructor-arg>
            <constructor-arg name="dog" ref="dog"></constructor-arg>
        </bean>
        
        <bean id="dog" class="xz.learn.ioc.Dog"></bean>
        
        <bean id="cat" class="xz.learn.ioc.Cat"></bean>
 </beans>
package xz.learn.ioc;

import xz.learn.interfaces.animal;

public class Man implements animal{
    
    private Cat cat;

    private Dog dog;

    @Override
    public void eat() {
        System.out.println("人什么都敢吃。。");
    }
    public Man() {

    }
    public Man(Dog dog,Cat cat){
        this.dog=dog;
        this.cat=cat;
        System.out.println("class man!!!");
        eat();
    }
}

显示结果:和上面相同。

注解方式实现IOC:

常用注解:

@Autowired:  注入方式:byType   (@Autowired+@Qualifier=@Resource)

@Resource:   注入方式:byName

@Qualifier:   指定注入bean的名称

@Component:  任何地方

@Service:    业务层

@Constroller:  控制层

@Postconstruct: 初始化之后

@Predestory:  销毁之前

实现:

package xz.learn.ioc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import xz.learn.interfaces.animal;

@Component
public class Man implements animal{
    
    @Autowired
    private Cat cat;
    
    @Autowired
    private Dog dog;

    @Override
    public void eat() {
        System.out.println("人什么都敢吃。。");
    }

    public Man(){
        System.out.println("class man!!!");
        eat();
    }
}

 

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        
        <context:component-scan base-package="xz/learn/ioc">
        </context:component-scan>
 </beans>

 

package xz.learn.ioc;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

import xz.learn.interfaces.animal;

@Component
public class Cat implements animal{

    @Override
    public void eat() {
        System.out.println("猫吃鱼。。");
    }
    
    public Cat() {
        System.out.println("class cat!!!");
        eat();
    }
    
    @PostConstruct
    public void pre(){
        System.out.println("构造之后========");
    }
    
    @PreDestroy
    public void after() {
        System.out.println("销毁之前========");
    }

}

 

package xz.learn.ioc;

import org.springframework.stereotype.Component;

import xz.learn.interfaces.animal;

@Component
public class Dog implements animal{
    
    public void eat(){
        System.out.println("狗吃骨头。。");
    }
    public Dog() {
        System.out.println("class dog!!!");
        eat();
    }
}

运行结果:

spring学习笔记之IOC_第1张图片

 

你可能感兴趣的:(spring学习笔记之IOC)