使用Spring框架的步骤
2015-08-13 19:21
477 查看
“好记性,不如烂笔头”。今天正式接触了Spring框架,第一次接触Spring框架感觉Spring框架简化了好多程序代码,开发效率大大提高。现在介绍使用Spring框架的步骤。(使用spring-framework-2.5.6版本)
1、导入jar包:找到压缩包里边的dist/Spring.jar;然后再找到
lib\jakarta-commons\commons-logging.jar
2、编写spring配置文件....;添加一个bean(将一个类/依赖对象交给spring来维护和创建)
3、通过ApplicationContext context = new ClassPathXMLApplicationContext("applicationContext.xml");
context.getBean("bean名称");来获取创建的bean
1、导入jar包:找到压缩包里边的dist/Spring.jar;然后再找到
lib\jakarta-commons\commons-logging.jar
2、编写spring配置文件....;添加一个bean(将一个类/依赖对象交给spring来维护和创建)
<?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-2.5.xsd"> <!-- bean:将一个类交给spring维护和创建就是一个bean 之前手动new的对象现在交给spring来做 --> <!--<bean id="springioc" class="com.msit.spring.IOC.DI.ObjectRef.SpringIOC"></bean>--> <!-- bean:就是一个类(对象) property:代表类或者对象中的属性;name跟类中的属性名称一样;value就相当于= SpringIOC.msg = "Hello-Spring" --> <bean id="hellospring" class="com.msit.spring.IOC.DI.propertity.HelloSpring"> <property name="msg" value="Hello-Spring"></property> </bean> </beans>
3、通过ApplicationContext context = new ClassPathXMLApplicationContext("applicationContext.xml");
context.getBean("bean名称");来获取创建的bean
package com.msit.spring.IOC.createObject; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // 加载配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //得到Bean HelloWorld ch = (HelloWorld) context.getBean("hellospring"); ch.hello(); //ch.createhello().hello(); //CreateHelloWorld.createhello().hello(); } }
相关文章推荐
- 精通Hibernate——Hibernate中Java对象的状态详解
- 懒得笔记4 spring annotation
- [Java] Majority Element II 找众数2
- eclipse启动报错A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse.
- 深入理解java的finalize
- Eclipse ADT的Custom debug keystore所需证书规格
- [Java] Majority Element (求过半数,或者翻译成 求出现次数过半的众数)
- Java的反射机制
- 修改eclipse默认workspace
- 【Head First Java 读书笔记】(三)primitive主数据类型和引用
- java dom解析xml
- (转)Eclipse快捷键
- Java中Filter、Servlet、Listener
- Java SE 学习笔记(二)
- java两个线程打印奇数和偶数
- Eclipse远程调试java web应用
- java大数 hdu5241
- Java之LinkedHashSet
- Eclipse操作hive数据库以及操作HDFS的jar包和环境配置
- Java之HashSet