spring study 1: concept and role of the IOC
A, spring advantage
1.1 convenient decoupling, simplify development
Ioc container provided by the spring, the dependencies between objects can be managed to the spring, to avoid hard-coding program causing a transition coupling
1.2 AOP programming support
Oriented Programming may be achieved by the spring function of aop
1.3 declarative transaction support
1.4 easy integration with other third-party frameworks
Two, ioc concept and role
ioc refers to the inversion of control, refers to the past, we used the time to get an object it is to create a way of their own, which is an active process; and after the inversion of control, just like when we need an object to the factory, and factory to help us create or find the object, which is a passive process.
This passive reception object is by inversion of control.
Its role is to reduce the coupling of a computer program (code dependency release)
Third, the simple use of spring ioc
Create a maven project, dependent on the introduction of spring
Pom file contents are as follows
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lyy</groupId>
<artifactId>spring01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Creating a class interface and implementation UserService, the following is the implementation class
public class UserServiceImpl implements IUserService {
@Override
public void findAllUser() {
userDao.findAll();
System.out.println("查询全部用户");
}
}
Create a profile applicationContext.xml spring in the resources directory in which the configuration of a bean userService
<?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">
<!--把对象的创建交给spring来管理-->
<bean id="userService" class="com.lyy.service.impl.UserServiceImpl">
</bean>
</beans>
Test uses ioc to get the object
public class Test2 {
public static void main(String[] args) {
//加载配置文件,初始化spring容器
ApplicationContext app=new ClassPathXmlApplicationContext("applicationCotext0.xml");
//获取bean
IUserService userService= (IUserService) app.getBean("userService");
//执行bean的方法
userService.findAllUser();
}
}
The above test method, initialize the spring container, bean object configuration in this process will spring configuration files are placed in a container. The object is then acquired from the id of the bean container, the method execution object.
Four three instantiate the bean
4.1 Using the no-argument constructor
spring will use the default no-argument constructor of the class specified bean class to create the bean, if there is no no-argument constructor creates failure.
4.2 spring static factory management, use the static factory method to create bean object
Simulate a static factory
public class UserServiceFactory {
public static IUserService getInstance(){
return new UserServiceImpl();
}
}
The use of plants in spring configuration file
<bean id="userService2" class="com.lyy.util.UserServiceFactory" factory-method="getInstance"></bean>
4.3 spring instance factory management, using an instance factory method to create bean object
One example plant simulation
public class UserServiceFactory2 {
public IUserService getUserService(){
return new UserServiceImpl();
}
}
In the spring configuration file first instance factory configured as a bean, use the factory to create an object instance
<bean id="userServiceFactory2" class="com.lyy.util.UserServiceFactory2">
</bean>
<bean id="userService3" factory-bean="userServiceFactory2" factory-method="getUserService">
</bean>
Five, spring dependency injection
By inversion of control, to create objects to the spring, but the code does not depend on circumstances that can not happen. ioc decoupling only reduce their dependency, but not eliminated. For example the implementation class service layer will certainly depend dao layer. After using spring allows spring to maintain this dependency is to let the object persistence layer framework to spread service layer
5.1 Constructor injection
Use constructor-arg tag Tags arranged to inject bean to bean properties
The following is a user class
public class User {
private String name;
private String age;
}
Incoming following properties when configuring the bean constructor
<bean id="user" class="com.lyy.domain.User">
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="18"/>
</bean>
5.2 set injection method
Provided the corresponding set method in the bean class using proerty tag in the configuration file to transfer the attribute value
<bean id="user2" class="com.lyy.domain.User">
<property name="name" value="李四"/>
<property name="age" value="22"/>
</bean>
5.3 p namespace injection, in essence, is set method
5.4 injection set of attributes
5.4.1 Array
Attribute value used to inject the bean array type
<bean id="user3" class="com.lyy.domain.User">
<property name="myArray">
<array>
<value>element1</value>
<value>element2</value>
<value>element3</value>
</array>
</property>
</bean>
5.4.2 List
Bean attribute values used to inject the type of List
<bean id="user4" class="com.lyy.domain.User">
<property name="myList">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
5.4.3 Set
Attribute values used to inject the bean type Set
<bean id="user5" class="com.lyy.domain.User">
<property name="mySet">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
</bean>
5.4.4 Map
Attribute values used to inject the bean type Map
<bean id="user6" class="com.lyy.domain.User">
<property name="myMap">
<map>
<entry key="a" value="1"></entry>
<entry key="b" value="1"></entry>
<entry key="c" value="1"></entry>
</map>
</property>
</bean>
5.4.5 Properties
Attribute values used to inject the bean type Properties
<bean id="user7" class="com.lyy.domain.User">
<property name="myProperties">
<props>
<prop key="x">1</prop>
<prop key="y">1</prop>
<prop key="z">1</prop>
</props>
</property>
</bean>