Spring Framework-01-02-The specific implementation of Spring Framework Simple Demo


Architecture design principles
Insert picture description here


URL for downloading the Spring package

Open Spring's official website https://spring.io/

Insert picture description here

Insert picture description here

If there is no GitHub logo on the right, it may be that the ad blocking plug-in of your browser has been blocked, just open it in a different browser.
Or directly open the following URL
https://github.com/spring-projects/spring-framework

Insert picture description here

method 1

Insert picture description here

Insert picture description here

Insert picture description here

Method 2

Insert picture description here

Insert picture description here
Choose the middle one

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

https://repo.spring.io/libs-milestone-local/org/springframework/spring/5.3.0-M2/

Insert picture description here




Bean

Insert picture description here




Simple Demo

Create a Java project

  1. Start to guide the package
    Insert picture description here

Insert picture description here
Find the compressed file downloaded above, unzip it, and select all the packages in the libs folder

Insert picture description here
Need a binding data source file, the name is generally called: beans.xml or applicationContext.xml

Insert picture description here

XML file-based approach

Engineering structure drawing

Insert picture description here

beans.xml

Insert picture description here

<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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    
    <!-- id名字随意,区分大小写。 class是包名 -->
    <bean id="IntelCpu111" class="com.IntelCpu"></bean>
    <bean id="Computer" class="com.Computer">
     
    	<!-- name=被注入的对象给注入者的属性,ref=注入的目标,是上面自定义的ID -->
        <property name="intelCpu" ref="IntelCpu111"></property>
    </bean>
</beans>


Computer.java

package com;

public class Computer {
    
    
	//Computer类依赖CPU类。
	
	private IntelCpu intelCpu;
	
	
	
	//通过Set方法实现注入
	public void setIntelCpu(IntelCpu intelCpu) {
    
    
		this.intelCpu = intelCpu;
	}

	public void play() {
    
    
		intelCpu.run();
		System.out.println("pc is running");
	}

}

IntelCpu.java

package com;

public class IntelCpu {
    
    

	public void run() {
    
    
		System.out.println("intel cpu is running");
	}
}

Test.java

package com;

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

public class Test {
    
    

	public static void main(String[] args) {
    
    
		
		//1. 启动Spring容器,参数是数据配置的文件名,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		//2. 从容器中拿到对象
		Computer pc = (Computer)ctx.getBean("Computer");
		pc.play();
	}

}

Annotation-based approach

Project structure

Insert picture description here

Beans.xml

<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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    



<!-- 这是注解方式配置 -->
    <context:annotation-config />
    	<!-- 提示扫描那个包的注解 -->
    	<context:component-scan base-package="com">
    </context:component-scan>



</beans>


Computer.java

package com;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

//@Component表示是Spring当中类的容器的组件
//括号代表着给此对象加上了Id,
//因为不加的话默认会认为此类的名字开头是小写,而实际是大写开头
@Component("Computer")
public class Computer {
    
    
	//Computer类依赖CPU类。
	
	
	//表明是一种资源,有了此注释可以省略相应的Set方法
	@Resource
	private IntelCpu intelCpu;
	
	
//	//通过Set方法实现注入
//	public void setIntelCpu(IntelCpu intelCpu) {
    
    
//		this.intelCpu = intelCpu;
//	}

	public void play() {
    
    
		intelCpu.run();
		System.out.println("pc is running");
	}

}

IntelCpu.java

package com;

import org.springframework.stereotype.Component;

@Component
public class IntelCpu {
    
    

	public void run() {
    
    
		System.out.println("intel cpu is running");
	}
}

Test.java

package com;

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

public class Test {
    
    

	public static void main(String[] args) {
    
    
		
		//1. 启动Spring容器,参数是数据配置的文件名,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		//2. 从容器中拿到对象
		Computer pc = (Computer)ctx.getBean("Computer");
		pc.play();
	}

}


Configuration based on Java

Precautions

  • On the basis of the above annotation configuration, the XML file is missing. Put the information that should be configured by the Xml file into the Test test class.
  • The difference with the above annotation configuration is that the xml configuration file is deleted, and the annotation is added to the Test class
  • Note: The test method of obtaining the Spring container is different

Project structure

Insert picture description here

Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan("com")
public class Test {
    
    

	public static void main(String[] args) {
    
    
		
		//1. 启动Spring容器,参数当前这个类
		ApplicationContext ctx = new AnnotationConfigApplicationContext(Test.class);
		//2. 从容器中拿到对象
		Computer pc = (Computer)ctx.getBean("Computer");
		pc.play();
	}

}

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/114819764