The first part: Spring's IoC (Inversion of Control)
is a metaphorical image below IoC
, that is, decoupling.
IoC
It is Spring
the core content of the framework. It is implemented in a variety of ways IoC
. You can use XML
configuration or annotations. In Spring
the inversion control is implementation dependent injection ( Dependency Injection, DI
).
So why not come and experience it
1. XML
RealizeIoC
Path:
Hello.java
Content:
package com.weizu;
public class Hello {
private String name;
public Hello() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
Then define the beans.xml
content:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.weizu.Hello">
<property name="name" value="Xiao wang."/>
</bean>
</beans>
Finally, use the test:
import com.weizu.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
@Test
public void Test(){
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.getName());
}
}
Test result:
Corresponding official document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-instantiation
Above we have learned how to use XML
to load Bean
objects, then we can be the last article of improvement, with Spring
for automatic hosting.
That is beans.xml
, specify Dao
the data source loaded by the layer in the file .
2. Case 2
Project structure
UserDao.java
:
public interface UserDao {
void getUser();
}
UserMySQLImpl .java
:
public class UserMySQLImpl implements UserDao{
public void getUser() {
System.out.println("MySQL impl.");
}
}
UserOracleImpl.java
:
public class UserOracleImpl implements UserDao{
public void getUser() {
System.out.println("Oracle impl.");
}
}
UserService.java
:
public interface UserService {
void getUserService();
}
UserServiceImpl.java
public class UserServiceImpl implements UserService{
private UserDao userDao;
// set注入
public void setUserDao(UserDao dao){
this.userDao = dao;
}
public void getUserService() {
userDao.getUser();
}
}
Then, we define the beans.xml
file to define the dao
layer data source that needs to be loaded :
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mySqlDao" class="com.dao.UserMySQLImpl"/>
<bean id="oracleDao" class="com.dao.UserOracleImpl"/>
<bean id="userService" class="com.service.UserServiceImpl">
<property name="UserDao" ref="mySqlDao"/>
</bean>
</beans>
Then, test in the test:
public class myTest {
@Test
public void Test(){
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.getUserService();
}
}
Result:
That is, you can xml
perform simple data source object to specify required by the Spring
decision how to load. No longer need to be used new
to create objects, which can reduce the coupling degree of the code. Remember: ApplicationContext
and ClassPathXmlApplicationContext
.
Just modify the configuration file, without modifying the program.
Object of Spring
the allocation and management come.
Video address: https://www.bilibili.com/video/BV1WE411d7Dv?p=5&spm_id_from=pageDriver