Step 1: download the Spring development package.
Official website: https://spring.io/ Download address: https://repo.spring.io/libs-release-local/org/springframework/spring/
spring-framework-3.0.2.RELEASE-dependencies and spring-framework-4.2.4.RELEASE-dist extract the second one, and extract the directory:
Step 2: build a project and introduce Spring project
Create project: since Spring is a one-stop Java Se / EE framework, you can create either a web project or a java project. Create a web project here.
Import jar: the core package is definitely needed to build the spring framework. Four of the above core containers are the core. For other functions, you can find which jar to add in libs. There are also log recording packages that spring has already made. In dependencies, under log4j and commons packages
Step 3: import the related configuration file (created under src)
log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### # error warn info debug trace log4j.rootLogger= info, stdout
applicationContext.xml file (Spring's core configuration file)
Header introduction: in the extracted spring-framework-4.2.4. Release dist \ docs \ Spring Framework Reference \ HTML \ xsd-configuration.html, there are configuration file header schema constraints
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- Configuration, giving the object creation of the implementation class to Spring Administration bean Label: id:to bean If a name is unique, it cannot be repeated or special characters cannot appear, it is regarded as programming according to the interface orientation new Examples from class: The full path of a class, and the underlying layer generates instances based on the full path --> <bean id="userDao" class="cn.xxx.dao.impl.UserDaoImpl"> <!-- Attribute dependency injection, the underlying layer will be automatically encapsulated as long as dao Implementation classes provide properties and set Method is OK. --> <property name="name" value="Zhang San"></property> </bean> </beans>
Userdaoimpl.java (implementation of userdao interface)
package cn.xxx.dao.impl; import cn.xxx.dao.UserDao; /** *@param *@author cxh */ public class UserDaoImpl implements UserDao { private String name; public void setName(String name) { this.name = name; } @Override public void insert() { System.out.println(name+"User information insertion"); } }
Test class:
@Test public void demo1() { // Create by loading a profile Spring Factory class(The default loading path of the framework is src lower,So you can put the configuration file in one directory) ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/config/applicationContext.xml"); // Analyze through factory XML Obtain Bean Example UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.insert(); }
Operation result:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. //Zhang San user information insertion
The above is the construction of IOC based on Spring.
IOC: inversion of control, giving the creation right of the object to Spring
DI: dependency injection, if there is an IOC environment, Spring will inject the dependency properties of the class when managing this class.