The main function of spring is control inversion and aspect-oriented programming, so let's write the first spring program to experience control inversion
The first is to load the configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
-middle tier application context definition for the image database.
-
<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"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-2.5.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
</beans>
Below we load the configuration file in the program
ApplicationContext ac = new Classpathxmlapplicationcontext ("Spring.xml");
Then create a new class
Package Com.service.impl;
Import Com.service.Service;
public class Servicebean implements Service {
@Override
public void Save () {
System.out.println ("Save ()");
}
}
Extract the interface of the class refactor----->extract interface
And then, in the test method, Confucius reversed
Package junit.test;
Import static org.junit.assert.*;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.service.Service;
public class Springtest {
@Test
public void Test () {
ApplicationContext ac = new Classpathxmlapplicationcontext ("Spring.xml");
Service service = (service) Ac.getbean ("service");
Service.save ();
}
}
Spring Introductory Learning (i)