spring入门(一) 根据xml实例化一个对象

文档: https://docs.spring.io/spring/docs/5.0.9.RELEASE/spring-framework-reference/core.html#beans-factory-metadata

包下载的位置  http://repo.spring.io/release/org/springframework/spring/5.0.9.RELEASE/

1.pom如下:

 1 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 2         <dependency>
 3             <groupId>org.springframework</groupId>
 4             <artifactId>spring-core</artifactId>
 5             <version>5.0.9.RELEASE</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>spring-context</artifactId>
10             <version>5.0.9.RELEASE</version>
11         </dependency>
12         <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
13         <dependency>
14             <groupId>org.springframework</groupId>
15             <artifactId>spring-beans</artifactId>
16             <version>5.0.9.RELEASE</version>
17         </dependency>
18         <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
19         <dependency>
20             <groupId>org.springframework</groupId>
21             <artifactId>spring-expression</artifactId>
22             <version>5.0.9.RELEASE</version>
23         </dependency>
View Code

2.建立User类

 1 public class User {
 2     private String name;
 3     private Integer age;
 4 
 5     public String getName() {
 6         return name;
 7     }
 8 
 9     public void setName(String name) {
10         this.name = name;
11     }
12 
13     public Integer getAge() {
14         return age;
15     }
16 
17     public void setAge(Integer age) {
18         this.age = age;
19     }
20 }
View Code

3.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
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="..." class="...">
 8         <!-- collaborators and configuration for this bean go here -->
 9     </bean>
10 
11     <bean id="..." class="...">
12         <!-- collaborators and configuration for this bean go here -->
13     </bean>
14 
15     <!-- more bean definitions go here -->
16 
17 </beans>

4.实例化User类

main方法里:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
1         //位置:setting/applicationContext.xml
2         ApplicationContext context = new ClassPathXmlApplicationContext("setting/applicationContext.xml");
3         User user = context.getBean("user",User.class);
4         System.out.println(user);

 

转载于:https://www.cnblogs.com/ICE_Inspire/p/9721382.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值