Download the Spring framework spring-framework-4.3.10.RELEASE-dist.zip:
http://maven.springframework.org/release/org/springframework/spring/4.3.10.RELEASE/ You
need to use five jars for initial learning:
spring-beans-4.3.10.RELEASE.jar
spring-aop-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-context-4.3. 10.RELEASE.jar
plus a tripartite log jar:
commons-logging-1.1.1.jar : https://mvnrepository.com/artifact/commons-logging/commons-logging/1.1.1
1. Build a configuration environment
Build these six jars into the project.
Of course, if the tool you use is IDEA, you don’t need to do all the steps above (hahahahaha)
2. Write configuration files
New file under src: applicationContext.xml
<?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">
<bean id="UserDao" class="com.itheima.ioc.UserDaoImpl"/>
<bean id="person" class="com.itheima.entity.Person">
<property name="id" value="1"></property>
<property name="name" value="zs"></property>
<property name="age" value="13"></property>
<property name="sex" value="true"></property>
</bean>
</beans>
3. Develop Spring program
Build package: com.itheima.ioc
package build interface: UserDao
package com.itheima.ioc;
public interface UserDao {
public void say();
}
Implementation interface: UserDaoImpl
package com.itheima.ioc;
public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("UserDao say hello world!");
}
}
Test class: TestIoc
package com.itheima.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc {
public static void main(String[] args) {
//初始化spring容器,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取userDao实例
UserDao userDao = (UserDao) applicationContext.getBean("UserDao");
//调用say方法
userDao.say();
Person person = (Person) applicationContext.getBean("person");
System.out.println(person.toString());
}
}