Spring5入门
程序员文章站
2022-03-05 10:36:41
...
Spring入门程序
1.导包
利用maven导入相关的依赖
5个核心依赖:
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
2.创建一个普通类,添加方法
创建一个User类,里面有一个add()方法
public class User {
protected void add() {
System.out.println("Add.........");
}
}
3.配置Spring配置文件,在配置文件里面配置创建的对象
<!--配置User对象的创建-->
<bean id="User" class="club.huantang.spring5.User"></bean>
4.运行测试代码
public class demo01 {
public static void main(String[] args) {
// 1.加载配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("bean01.xml");
// 2.获取配置创建的的对象
User user01 = context.getBean("User",User.class);
System.out.println(user01);
user01.add();
}
}