IDEA下用Spring写一个Hello World

首先,打开IDEA;

创建maven项目;

IDEA下用Spring写一个Hello World_第1张图片

什么都不要选,直接Next;

IDEA下用Spring写一个Hello World_第2张图片

输入groupid、artifaid;

IDEA下用Spring写一个Hello World_第3张图片

进来后,配置pom.xml文件;

此时有个坑,假如填写完配置文件,右上角出现了一个东西,一定要点进去,进去之后选定,apply就OK(就不截图了这里),否则到了后面就会发现IoC容器无法创建!

然后就很简单,写实现类:

IDEA下用Spring写一个Hello World_第4张图片

配置xml文件:

IDEA下用Spring写一个Hello World_第5张图片

在resources目录下,创建.xml文件;

IDEA下用Spring写一个Hello World_第6张图片

文件统一格式为:






然后就可以创建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();
    }
}

此时,已经全部完成;

运行此容器:

IDEA下用Spring写一个Hello World_第7张图片

你可能感兴趣的:(工具使用)