spring4.3.5基本配置

1.去官网下载必要的jar包,以及

2.新建一个web项目,在Window->Java->Build Path->User Libraries

按照步骤1,2把spring的jar放进去。

3.右键项目-》Build Path-》Configure Build Path-》

第1步选择User Library-》勾选Spring4.3,第2步选择commons-logging-1.2.jar。如此必要的jar包就导进来了。

接下来项目src下写一个类HelloSpring:

 1 package spring;
 2 
 3 public class HelloSpring {
 4     private String info;
 5     public void setInfo(String info) {
 6         this.info = info;
 7     }
 8     public void message()
 9     {
10         System.out.println("hello "+info);
11     }
12 }

然后在src下写一个xml配置文件,比如applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 5     <!-- 配置需要被Spring管理的Bean(创建,创建后放在了Spring IOC容器里面) -->
 6     <bean id="hello" class="spring.HelloSpring">
 7         <!-- 配置该Bean需要注入的属性(是通过属性set方法来注入的) -->
 8         <property name="info" value="zhangsan" />
 9     </bean>
10 </beans>  

接下来写一个测试类Test:

 1 package spring;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Test {
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
10         HelloSpring hs = context.getBean("hello", HelloSpring.class);
11         hs.message();
12     }
13 }

测试类中的getBean第一个参数是和applicationContext.xml文件中的bean的id对应

 

转载于:https://www.cnblogs.com/luoxiaozhao/p/6972961.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值