spring基础(1)-基本使用
来源:互联网 发布:格雷厄姆格林知乎 编辑:程序博客网 时间:2024/06/01 08:21
- Spring的安装
Help—EclipseMarketPlace中直接搜索进行安装。 创建一个spring(HelloWorld)程序
(1)创建一个Java工程,导入spring所必要包(四个必须包和一个日志包)创建spring的配置文件applicationContext.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 --> <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld"> <property name="name2" value="Spring"></property> </bean></beans>
4.创建一个HelloWorld类
public class HelloWorld { private String name; public void setName2(String name) { System.out.println("setName"+name); this.name = name; } public void hello(){ System.out.println("hello"+name); } //调用构造器对配置文件中的bean进行初始化,同时调用set方法对属性进行赋值 public HelloWorld(){ System.out.println("HelloWorld's Constructor..."); }}
5.创建一个Main方法进行测试使用
public class Main { public static void main(String[] args){ /*//创建HelloWorld 的一个对象,此处的调用等同于下面的Spring调用 HelloWorld helloworld = new HelloWorld(); //为name属性赋值 helloworld.setName("atguiug");*/ //1.创建Spring 的IOC容器对象 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器中获取bean实例 HelloWorld helloworld = (HelloWorld) ctx.getBean("helloworld"); //调用hello方法 helloworld.hello(); }}
本文章仅作为作者学习的笔记,此类下所有文章均为此用途。
0 0
- spring基础(1)-基本使用
- ActiveMQ与Spring整合:(1)基本使用
- Spring 的基本使用(一)
- 09 Spring 事务管理(@Transactional)基本使用
- spring AspectJ 基本使用
- Spring的基本使用
- spring 基本使用
- Spring.NET 基本使用
- Spring retry基本使用
- Spring Profile基本使用
- Spring Redis基本使用
- spring +springmvc 基本使用
- Spring IOC 基本使用
- spring基本使用
- Spring task基本使用
- Spring retry基本使用
- spring的基本使用
- Spring cache基本使用
- Java温习——关键字和保留字
- eclipse 使用技巧
- Python:Scrapy中出现的乱码问题
- storm的ack和fail
- 集合
- spring基础(1)-基本使用
- Memcache技术笔记
- 获取开启的所有PHP扩展及该扩展下的函数
- JavaScript 常用实例收集
- 统一组织机构信用代码(从正则到方法验证,JavaScript)
- Swift下自定义printf, 在状态DEBUG打印, 在release状态不打印
- 工厂模式结合RPC进行多节点数据调用的代码套路
- Linux三大共享文件的方法
- pandas学习笔记(1)--pandas简介