导入依赖jar包, 使用的是org.spring.framewok 5.1.5 RELEASE
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 第三方jar-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
创建xml文件, beans.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 definitions here -->
</beans>
创建接口
package cn.lacknb.iml;
public interface DoSomething {
void add();
void delete();
void find();
void update();
}
package cn.lacknb.spring;
import cn.lacknb.iml.DoSomething;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DoImpl implements DoSomething {
@Override
public void add() {
System.out.println("添加成功");
}
@Override
public void delete() {
System.out.println("删除成功");
}
@Override
public void find() {
System.out.println("查询成功");
}
@Override
public void update() {
System.out.println("修改成功");
}
public static void main(String[] args) {
// 传统方式
// DoSomething doing = new DoImpl();
// doing.add();
// doing.delete();
// spring方式
// 加载容器
ApplicationContext context = new ClassPathXmlApplicationContext("xml/beans.xml");
DoImpl doing = (DoImpl) context.getBean("dosomething");
doing.find();
doing.update();
}
}