I. create objects using Spring
Legacy create object
Type name variable name = new type ();
Hello hello = new Hello();
Use Spring to create objects, which are called bean s in Spring
id = variable name
class = new object
Property property (property name + assignment)
<bean id="hello" class="com.ming.pojo.Hello"> <property name="str" value="Spring"/> </bean>
The hello object is created by spring, The properties of the hello object are set by the spring container (and spring uses set injection to create the object (which is done by reflection) (reflection core: get the class. The reflection mechanism is that in the running state, you can know all the properties and methods of any class; you can call any method and property of any object; this dynamically obtained information and the function of dynamically calling the method of the object are called the reflection mechanism of java language))
You can see the flag of the set method called by spring using the reflection mechanism in the entity class
This process is called control reversal
Control: who controls the creation of objects? Traditionally, they are created by the program itself. Now they are created in Spring
Dependency injection: use the set method to inject
IOC is a programming idea, from active programming to passive receiving
new ClassPathXmlApplicationContext view the underlying source code
2, Testing
//Get the context object of Spring ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //Our objects are managed in Spring. We need to use them and get them directly Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString());
//Why is the classPathXmlApplicationContext created and an ApplicationContext returned?
By viewing show diagrams
Use Spring to refactor the previous three-tier schema (directly add a beans.xml configuration 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--be careful: Spring An object (implementation class) is created, and the interface is an abstract class, not an object--> <!--Spring establish dao object--> <bean id="MySqlImp" class="com.ming.dao.UserDaoMySqlImp"/> <bean id="OracleImp" class="com.ming.dao.UserDaoOracleImp"/> <bean id="UserDaoImp" class="com.ming.dao.UserDaoImp"/> <bean id="SqlServerImp" class="com.ming.dao.UserDaoSqlServerImp"/> <bean id="RedisImp" class="com.ming.dao.UserDaoRedisImp"/> <!--Spring establish service object--> <bean id="ServiceImp" class="com.ming.service.UserServiceImp"> <!-- name Attribute name: because it is a business layer, the attribute can also be an interface, because these implementation classes implement this interface ref quote Spring Created object in container (via id (identification) value: A specific value, basic data type --> <property name="UserDao" ref="RedisImp"/> </bean> </beans>
//The test here is equivalent to the user layer public class MyTest { public static void main(String[] args) { // Old method // //What the user actually calls is the business layer // UserServiceImp userService = new UserServiceImp(); // //The idea is to add a layer, add a method in the business layer, and put the change requirements into the user layer // userService.setUserDao(new UserDaoSqlServerImp()); // userService.getUser(); //======================================================================================================= // The Spring container prevents the creation of good objects (like a factory), which can be called directly by the user, that is, Spring takes over the task of creating objects //You just call (which object you want) and leave the creation to Spring //Get ApplicationContext (access the Spring container through the configuration file) ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //When the container is in hand, it is like taking a menu and directly calling which dish (which object is needed). The restaurant chef is responsible for making (creating objects), //Spring is really like the factory pattern (the factory is responsible for creating objects and the user can call them directly) UserServiceImp userServiceImp = (UserServiceImp) context.getBean("ServiceImp"); userServiceImp.getUser(); }
//Summary:
//Since the objects are managed by the Spring container, the need to create objects should also be changed in Spring instead of the business layer
//What's more, this is a dynamic modification (that is, you don't have to stop the server, because every time you stop the server is a great loss)