首先,打开IDEA;
创建maven项目;
什么都不要选,直接Next;
输入groupid、artifaid;
进来后,配置pom.xml文件;
此时有个坑,假如填写完配置文件,右上角出现了一个东西,一定要点进去,进去之后选定,apply就OK(就不截图了这里),否则到了后面就会发现IoC容器无法创建!
然后就很简单,写实现类:
配置xml文件:
在resources目录下,创建.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
然后就可以创建IoC容器了;
在实现类的同一个包下,创建一个类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main1 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.gretting();
}
}
此时,已经全部完成;
运行此容器: