Quickly set up Spring source reading environment
Install gradle environment
Prerequisites Gradle environment reference installation blog
Clone Spring source code
GitHub’s Spring projectCloning from GitHub may be too slow. Use the code cloud to pull and then clone
The installation
Build a Gradle project
Execute gradlew. Bat down from the project directory
Configuring a Mirror Source
Go to the build.gradle directory of your project. In order to quickly download the related packages, it is best to add a domestic image source
repositories {
mavenCentral()
maven { url "https://maven.aliyun.com/repository/public" }
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
Copy the code
The idea of configuration
Idea After configuring gradle tool, configure and import the project. Gradle tool automatically downloads the packageOnce the download is complete, proceed to build ProjectWhen done, the source code should be in the post-build folderInside is the compiled class file
test
After setting up the reading environment, write a test case
Create a submodule
Configure the required modules
Locate the module you created and go to build.gradle in the module directory
Plugins {id 'Java'} group 'org. springFramework 'version '5.2.15. build-snapshot' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile(project(":spring-context")) testCompile group: 'junit', name: 'junit, version:' 4.12 '}Copy the code
Run the test
I created these files
// Group.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Group implements ApplicationContextAware.BeanFactoryAware {
private String group_name;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
public String getGroup_name(a) {
return group_name;
}
public void setGroup_name(String group_name) {
this.group_name = group_name;
}
public ApplicationContext getApplicationContext(a) {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public BeanFactory getBeanFactory(a) {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory; }}// User.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class User implements ApplicationContextAware.BeanFactoryAware {
private String name;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public ApplicationContext getApplicationContext(a) {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public BeanFactory getBeanFactory(a) {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory; }}// MyTest.java
import com.czy.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User bean = context.getBean(User.class); System.out.println(bean.getName()); }}Copy the code
The configuration file
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.czy.bean.User">
<property name="name" value="lisi"></property>
</bean>
<bean id="group" class="com.czy.bean.Group">
<property name="group_name" value="abc"></property>
</bean>
</beans>
Copy the code
Start MyTest successfully