Spring基础——一个简单的例子
2016-07-15 13:50
246 查看
一、学习版本 spring-framework-4.0.0
二、导入 jar 包:
三、在类路径下创建 Spring Config 文件:ApplicationContext.xml
四、创建一个非侵入的 Bean
五、在 Spring Config 文件中配置该 Bean
六、通过 IOC 容器对象来获取在 Spring Config 文件中配置的 Bean,并调用其方法
七、总结
两个重点:
1.在 Spring Config 中配置 Bean
2.通过 IOC 容器中获取 Sring Config 配置的 Bean 对象
二、导入 jar 包:
三、在类路径下创建 Spring Config 文件: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"> </beans>
四、创建一个非侵入的 Bean
/** * @author solverpeng * @create 2016-07-15-10:09 */ public class HelloWorld { private String userName; public void setUserName(String userName) { this.userName = userName; } public void hello() { System.out.println("hello:" + userName); } }
五、在 Spring Config 文件中配置该 Bean
<?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 id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld"> <property name="userName" value="spring"/> </bean> </beans>
六、通过 IOC 容器对象来获取在 Spring Config 文件中配置的 Bean,并调用其方法
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml"); HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); helloWorld.hello(); // 输出 hello:spring }
七、总结
两个重点:
1.在 Spring Config 中配置 Bean
2.通过 IOC 容器中获取 Sring Config 配置的 Bean 对象
相关文章推荐
- Spring MVC相关注解
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- Java类之间的关联关系
- Java 中的中文编码问题
- SpringMVC 使用JSR-303进行校验 @Valid
- 关于Mysql,MyEclipse集成svn
- Ubuntu下安装jdk8
- javaee对xml的解析练习
- Mac OS X下Maven的安装与配置(JDK7)
- eclipse OutOfMemoryError设置
- Struts2的注解方式Annotation
- java_list,set,map集合
- java读取远程文件
- Java 进程 线程 程序编程 1
- spring学习笔记(一)
- Spring Security-03-核心类简介
- Spring Security-02-关于登录
- Spring Security-01-初体验
- java枚举工具类
- java异常处理