Introduction to Spring configuration files (1)

1. Spring
        Spring is a lightweight framework, equivalent to a platform, which greatly simplifies the development of Java enterprise-level applications and provides powerful and stable functions. . The Spring framework consists of approximately 20 functional modules, which are divided into 6 parts, as shown in the figure:


        Spring Core is the most basic part, providing IOC features; Spring AOP is a standard-compliant aspect-oriented programming implementation based on Spring Core.

        Spring IOC container is the core part of Spring. IOC (Inversion of Control), inversion of control, also known as dependency injection, is a design concept in object-oriented programming that is used to reduce the complexity of program code. degree of coupling between.
        In layman’s terms, in practical applications, multiple classes need to depend on each other to complete certain specific businesses, and dependency injection is to maintain the relationship between classes in the form of configuration files. relationship instead of hard coding, thus reducing the coupling between systems and increasing maintainability.
2. Build the environment
        Let’s start with a simple HelloWorld
        1. Download the spring jar package: http://projects .spring.io/spring-framework/ (This article uses version 4.1.4)
        2. Create an ordinary java project (it does not have to be a web project, for simplicity)
        3. Add jar package:

 4.Create the spring-config.xml file under src (you can choose the name yourself), and create the Student class and test class

(1) Student class:


 
public class Student {
	private int id;
	private String name;
	
	//省略get、set方法
	//为了方便显示,重写了toString方法
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
}

(2) Configuration of spring-config.xml file:

<?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:util="http://www.springframework.org/schema/util" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-4.0.xsd
	http://www.springframework.org/schema/p
	http://www.springframework.org/schema/p/spring-p-4.0.xsd">
	
	<!-- 以这种方式来声明Student类的实例 -->
	<bean id="student" class="com.wzj.entity.Student">
		<!-- 为Student对象的属性赋值 -->
		<property name="id" value="1"/>
		<property name="name" value="好人"/>
	</bean>
</beans>


 test class:

 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wzj.entity.Student;
 
public class Test {
	
	public static void main(String[] args) {
		//通过ApplicationContext接口的实现类实例化Spring上下文对象
		ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
		//通过getBean()方法来获取到Student类的对象
		Student student=(Student)context.getBean("student");
		System.out.println(student);
	}
 
}

4) Running results: Student [id=1, name=good guy]


3. Bean configuration
1.BeanFactory and ApplicationContext
        The Spring framework provides two implementations of IOC containers:
(1) BeanFactory: the basic implementation of the IOC container
(2) ApplicationContext (recommended): provides more advanced features and is a sub-interface of BeanFactory
        BeanFactory is the infrastructure of the Spring framework, oriented to Spring itself;
        ApplicationContext is oriented to developers using the Spring framework. Almost all applications use ApplicationContext directly instead of the underlying BeanFactory

        ApplicationContext implementation class:

—— ClassPathXmlApplicationContext: Load the configuration file from the class path
— FileSystemXmlApplicationContext: Load the configuration file from the file system
2. Configure Bean— — Based on the form of xml file
(1) Use the <bean> element in the spring configuration file to configure the Bean instance, such as:
 

<!-- 
		id:Bean的名称,在IOC容器中必须是唯一的,还可以指定多个名字,名字之间用英文逗号、分号或空格隔开
		class:Bean所对的类的全名,其实内部是通过反射来动态创建对象的
	 -->
	<bean id="student" class="com.hy.entity.Student">
		<!-- 为Student对象的属性赋值,value也可以作为子标签 -->
		<property name="id" value="1"/>
	</bean>

(2) Get the bean instance:

//The parameter name of getBean() should correspond to the bean id in the configuration file
    Student student=(Student)context.getBean("student");

The above are some of my insights into Spring. See you in the next article.

Guess you like

Origin blog.csdn.net/hy123154/article/details/129432550