spring Ioc源碼閱讀

在這裏插入圖片描述

spring 源碼

spring在ioc過程,初始化啓動類時,首先加載特定工廠處理器和類處理器,比如註解或者xml的處理器,然後實例化聲明的bean,如果有aop註解,也會在掃描配置時增加特定的工廠處理器和類處理器,實例化時,按優先級和順序實例化,如果有類被aop註解,先實例化改實體,隨後接口調用jdk代理生產代理類,實體類用cglib生成實體類,加載到全局的工廠處理器中,和全局的key class的map中,完成初始化,在後面使用註解獲取或者手動獲取時,根據參數找到key,然後找出map中對應的實體類即可

項目:

結構

在這裏插入圖片描述

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cxz</groupId>
  <artifactId>springRead</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springRead</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>
    
         <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
    
     <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.0</version>
  </dependency>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
</dependency>
  </dependencies>
</project>

code:

ioc:

App:

/**
 * Hello world!
 *
 */
public class App 
{
	  static Logger logger = LogManager.getLogger(App.class.getName());
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
        System.out.println("context 啓動成功");
        
        // 從 context 中取出我們的 Bean,而不是用 new MessageServiceImpl() 這種方式
        MessageService messageService = context.getBean(MessageService.class);
        // 這句將輸出: hello world
        logger.info(messageService.getMessage());
    }
}

public interface MessageService {
    String getMessage();
}

public class MessageServiceImpl implements MessageService {
	 
    public String getMessage() {
        return "hello world";
    }
}

aop

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("hello");
		  ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:aop02.xml");
	        Mat math = ctx.getBean("math", Mat.class);
	       // Mat math=new Mat();
	        int n1 = 100, n2 = 5;
	        math.add(n1, n2);
	        math.sub(n1, n2);
	        math.mut(n1, n2);
	        math.div(n1, n2);
	}

}
/**
 * 被代理的目標類
 */
@Service("math")
public class Mat {
	static {
		System.out.println("init----------math---------");
	}
	  //加
    public int add(int n1,int n2){
        int result=n1+n2;
        System.out.println(n1+"+"+n2+"="+result);
        return result;
    }
    
    //減
    public int sub(int n1,int n2){
        int result=n1-n2;
        System.out.println(n1+"-"+n2+"="+result);
        return result;
    }
    
    //乘
    public int mut(int n1,int n2){
        int result=n1*n2;
        System.out.println(n1+"X"+n2+"="+result);
        return result;
    }
    
    //除
    public int div(int n1,int n2){
        int result=n1/n2;
        System.out.println(n1+"/"+n2+"="+result);
        return result;
    }
}
@Component
@Aspect
public class Advices {
	static {
		System.out.println("init-------------------");
	}
	 @Before("execution(* com.cxz.springRead.aop2.Mat.*(..))")
    public void before(JoinPoint jp){
        System.out.println("----------前置通知----------");
        System.out.println(jp.getSignature().getName()+"\n"+jp.getKind());
    }
	 @After("execution(* com.cxz.springRead.aop2.Mat.*(..))")
    public void after(JoinPoint jp){
        System.out.println("----------最終通知----------");
    }
}

xml

application.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
 
    <bean id="messageService" class="com.cxz.springRead.MessageServiceImpl"/>
</beans>```

aop02.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        <context:component-scan base-package="com.cxz.**">
        </context:component-scan>
        <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

cglib簡單使用

public class Main {

	public static void main(String[] args) {      
		System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\class");
        BookFacadeImpl1 bookFacade=new BookFacadeImpl1();
        BookFacadeCglib  cglib=new BookFacadeCglib();  
        BookFacadeImpl1 bookCglib=(BookFacadeImpl1)cglib.getInstance(bookFacade);  
        bookCglib.addBook();  
    }

}

public class BookFacadeImpl1 {  
    public void addBook() {  
        System.out.println("新增圖書...");  
    }  
}
public class BookFacadeCglib implements MethodInterceptor {  
    private Object target;//業務類對象,供代理方法中進行真正的業務方法調用
  
    //相當於JDK動態代理中的綁定
    public Object getInstance(Object target) {  
        this.target = target;  //給業務對象賦值
        Enhancer enhancer = new Enhancer(); //創建加強器,用來創建動態代理類
        enhancer.setSuperclass(this.target.getClass());  //爲加強器指定要代理的業務類(即:爲下面生成的代理類指定父類)
        //設置回調:對於代理類上所有方法的調用,都會調用CallBack,而Callback則需要實現intercept()方法進行攔
        enhancer.setCallback(this); 
       // 創建動態代理類對象並返回  
       return enhancer.create(); 
    }
    // 實現回調方法 
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { 
        System.out.println("預處理——————");
        proxy.invokeSuper(obj, args); //調用業務類(父類中)的方法
        System.out.println("調用後操作——————");
        return null; 
    }}

參考:https://blog.csdn.net/nuomizhende45/article/details/81158383

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章