使用IDEA搭建spring

  从前使用eclipse开发,集成jar包,现在使用maven来管理

一:

1.框架

  

2.pom

  需要spring core与spring context。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>SpringTest</groupId>
 8     <artifactId>SpringTest</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <dependencies>
12         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
13         <dependency>
14             <groupId>org.springframework</groupId>
15             <artifactId>spring-core</artifactId>
16             <version>5.0.0.RELEASE</version>
17         </dependency>
18         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
19         <dependency>
20             <groupId>org.springframework</groupId>
21             <artifactId>spring-context</artifactId>
22             <version>5.0.0.RELEASE</version>
23         </dependency>
24 
25     </dependencies>
26 </project>

3.接口HelloWorld

1 package com.it.service;
2 
3 public interface HelloWorld {
4     public void sayHello();
5 }

4.实现类

1 package com.it.service;
2 
3 public class SpringHelloWorld implements HelloWorld {
4 
5     public void sayHello() {
6         System.out.println("say hello");
7     }
8 }

5.bean

 1 package com.it.bean;
 2 
 3 import com.it.service.HelloWorld;
 4 
 5 public class HelloWorldService {
 6     private HelloWorld helloWorld;
 7 
 8     public HelloWorldService() {
 9 
10     }
11 
12     public void setHelloWorld(HelloWorld helloWorld) {
13         this.helloWorld = helloWorld;
14     }
15 
16     public HelloWorld getHelloWorld() {
17         return this.helloWorld;
18     }
19 }

6.运行main

 1 package com.it.main;
 2 
 3 import com.it.service.HelloWorld;
 4 import com.it.bean.HelloWorldService;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class HelloMain {
 9     public static void main(String[] args) {
10 
11         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
12 
13         HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
14 
15         HelloWorld hw= service.getHelloWorld();
16 
17         hw.sayHello();
18     }
19 }

7.beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     
 6     <bean id="helloWorldService" class="com.it.bean.HelloWorldService">
 7         <property name="helloWorld" ref="springHelloWorld"/>
 8     </bean>
 9 
10     <bean id="springHelloWorld" class="com.it.service.SpringHelloWorld"></bean>
11 </beans>

8.效果

  

原文地址:https://www.cnblogs.com/juncaoit/p/7783203.html