[03] Spring "Hello World"
0、写在前面的话
1、使用Maven建立Spring项目

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>

2、建立一个简单的Java类
public class Coder {
private String name;
private int age;
public void print(){
System.out.println("Hello World");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Coder {
private String name;
private int age;
public void print(){
System.out.println("Hello World");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3、建立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.xsd">
<bean id="coder" class="dulk.learn.spring.Coder"></bean>
</beans>
<?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="coder" class="dulk.learn.spring.Coder"></bean>
</beans>
4、初始化容器,获取实例
- BeanFactory,早期的JavaBean工厂类实现(不推荐)
- ApplicationContext,对BeanFactory的扩展,提供了更多的功能,如国际化处理、Bean自动装配等
public class Test {
public static void main(String[] args) {
//BeanFactory
ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
Resource resource = rpt.getResource("/applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(resource);
Coder coder = (Coder) bf.getBean("coder");
coder.print();
}
}
public class Test {
public static void main(String[] args) {
//BeanFactory
ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
Resource resource = rpt.getResource("/applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(resource);
Coder coder = (Coder) bf.getBean("coder");
coder.print();
}
}
public class Test {
public static void main(String[] args) {
//ApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
Coder coder = (Coder) ac.getBean("coder");
coder.print();
}
}
public class Test {
public static void main(String[] args) {
//ApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
Coder coder = (Coder) ac.getBean("coder");
coder.print();
}
}

[03] Spring "Hello World"的更多相关文章
- 03 Spring的父子容器
1.概念理解和知识铺垫 在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关 ...
- (03) spring Boot 的配置
1. spring boot 的核心配置 spring boot 项目建立之后,已经创建好了application.properties 配置文件 其实, 配置文件还支持*.yml 格式的: 2. 多 ...
- Spring框架学习03——Spring Bean 的详解
1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...
- spring boot2.03 spring cloud Finchley.RELEASE遇到的问题
1.spring cloud bus 本地不能更新 原因是@RefreshScope注解要加在需要更新的controller上 2.No instances found of configserver ...
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
- 03.Spring IoC 容器 - 初始化
基本概念 Spring IoC 容器的初始化过程在监听器 ContextLoaderListener 类中定义. 具体由该类的的 configureAndRefreshWebApplicationCo ...
- 03 Spring对Bean的管理
Spring创建bean的三种方式 1.第一种方式:使用默认构造函数创建 bean.xml <?xml version="1.0" encoding="UTF-8& ...
- 03 spring security执行流程分析
spring security主要是依赖一系列的Filter来实现权限验证的,责任链设计模式是跑不了的.下面简单记录一下spring操作这些Filter的过程. 1. WebSecurityConfi ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
随机推荐
- MSCRM中报表开发二:创建基于FetchXML报表
1. 获取FetchXML.因为FetchXML难以撰写,所以我们一般都是使用高级查找来生成FetchXML或者通过其他工具来生成.我这里在商机界面通过高级查找制作了一个新的视图,名称为 商机查询, ...
- LVS主从部署配置和使用
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS是L ...
- nginx-1-初识nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- Intent调用常见系统组件
// 调用浏览器 Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail"); Intent intent = ...
- springboot 升级到2.0后 context-path 配置 不起作用,不生效 不管用 皆是因为版本改动导致的在这里记录一下
不知不觉,新的项目已经将springboot升级为2.0版本了.刚开始没有配置server.contextpath,默认的“/”,然后今天放到自己的服务器上,所以就要规范名称. 结果,失败了,无论我 ...
- python爬虫之Beautifulsoup学习笔记
相关内容: 什么是beautifulsoup bs4的使用 导入模块 选择使用解析器 使用标签名查找 使用find\find_all查找 使用select查找 首发时间:2018-03-02 00:1 ...
- Python-初识模块
#系统自带的模块 import sys print(sys.path)#打印环境变量 print(sys.argv)#打印绝对路径 import os #cmd_res = os.system(&qu ...
- python pip 使用时错误: Patal error in launcher:Unable to create process using '"'
当前我的电脑配置是64位, 装有python2.7 和python 3.6 两个版本 在使用pip install mysqlclient 的时候,出现了 Patal error in launch ...
- entity framework异常 The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid
ROW_NUMBER() OVER (ORDER BY (select Null)) AS Id entity framework 查询中有这句会有异常
- P进制转Q进制
// 对一个P进制的数,如果要转换成Q进制的数 // 1)将P进制数x转换成十进制数y int y=0,product=1;//product在循环中会不断成P,得到1.P^2..... while( ...