1) Spring_HelloWorld
方式一:下载对应eclipse版本的文件,离线安装
4.4.2 springsource-tool-suite-3.6.4.RELEASE-e4.4.2-updatesite.zip 87MB
选择
- Core / Spring IDE
- Extensions / Spring IDE
- Integrations / Spring IDE
- Resources / Spring IDE
方式二:在线安装 install new sorftware - add site
2. 创建maven项目
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
- spring-aop
- spring-beans
- spring-context
- spring-core
- spring-expression
- commons-logging
- aopalliance
创建IoC 容器配置文件
<?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 id="person" class="cn.zno.hello.Person" >
<property name="name" value="XiaoMing"></property>
<property name="age" value="22"></property>
<property name="savings" value="10000"></property>
<property name="car" ref="car"></property>
</bean> <bean id="car" class="cn.zno.hello.Car">
<property name="brand" value="BYD"></property>
<property name="price" value="54000"></property>
</bean>
</beans> </beans>
Person.java
package cn.zno.hello; public class Person { private String name; private int age; private double savings; private Car car; public void say(){
System.out.println("Hello World!");
} // -------------getter & setter------------- 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 double getSavings() {
return savings;
} public void setSavings(double savings) {
this.savings = savings;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", savings=" + savings
+ ", car=" + car + "]";
}
}
Car.java
package cn.zno.hello; public class Car { private String brand; private double price; public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} @Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
} }
Main.java
package cn.zno.hello; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) {
// /*传统方式*/
// Person person = new Person();
// person.setName("XiaoMing");
// person.setAge(22);
// person.setSavings(10000);
// //-->
// Car car = new Car();
// car.setBrand("BYD");
// car.setPrice(50000);
// //<--
// person.setCar(car);
//
// System.out.println(person);
// person.say(); /*IoC*/
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-hello.xml"); Person person = (Person) ctx.getBean("person");
System.out.println(person);
person.say(); } }
Console:
六月 , :: 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef7de4: startup date [Tue Jun :: CST ]; root of context hierarchy
六月 , :: 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-hello.xml]
Person [name=XiaoMing, age=, savings=10000.0, car=Car [brand=BYD, price=54000.0]]
Hello World!
注意问题:
applicationContext.xml 是借助sts生成
ApplicationContext ctx; 应该记死!其实现类可Ctrl+T | F4 查询
目录
└─src
└─main
├─java
│ └─cn
│ └─zno
│ └─hello
│ Car.java
│ Main.java
│ Person.java
│
└─resources
beans-hello.xml
常见错误:
1. xml文件不存在
六月 , :: 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14e4e31: startup date [Tue Jun :: CST ]; root of context hierarchy
六月 , :: 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-hello1.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans-hello1.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans-hello1.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:)
at cn.zno.hello.Main.main(Main.java:)
Caused by: java.io.FileNotFoundException: class path resource [beans-hello1.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
... more
2.xml文件中bean未配置
六月 , :: 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef7de4: startup date [Tue Jun :: CST ]; root of context hierarchy
六月 , :: 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-hello.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'person1' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:)
at cn.zno.hello.Main.main(Main.java:)
1) Spring_HelloWorld的更多相关文章
- Spring_HelloWorld
目录: 各个类文件: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...
- Spring学习之旅(一)极速创建Spring框架java工程项目
编译工具:eclipse 1)创建java工程:Spring_helloworld 2)导入所需jar包: 3)创建一个实体类: public class HelloBeans { private S ...
- Spring4.x 基础
把以下 jar 包加入到工程的 classpath 下: 搭建Spring开发环境 Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于 ...
- spring 课程
官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...
- java之spring之helloword
这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建l ...
- Spring从入门到源码—IOC基本使用(二)
1.spring_helloworld 使用maven的方式来构建项目(Mavaen) 添加对应的pom依赖 pom.xml <dependencies> <!-- https:// ...
随机推荐
- Objective C, post 请求
-(void)postData { NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithS ...
- Mybatis知识(2)
1.#{}和${}的区别是什么? 注:这道题是面试官面试我同事的. 答:${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静 ...
- sql server 2000能否得到一个表的最后更新日期?
如果是SQL 2005 或 2008.运行下面的代码.就可以看到从上次启动SQL 服务以来,某个表的使用情况,包括select/update/delete/insert. SELECT * FROM ...
- Haskell语言学习笔记(34)System.Environment
System.Environment getArgs :: IO [String] getProgName :: IO String getExecutablePath :: IO FilePath ...
- 显示AVI文件的桢数
procedure TForm1.Button1Click(Sender: TObject);begin MediaPlayer1.TimeFormat := tfFrames; ShowMess ...
- conductor任务域
任务域 任务域有助于支持任务开发.这个想法是相同的“任务定义”可以在不同的“域”中实现.域名开发人员控制的任意名称.因此,当工作流程启动时,调用者可以在工作流中的所有任务中指定哪些任务需要在特定域中运 ...
- 大型运输行业实战_day09_1_日期转换与My97DatePicker插件使用
1.日期转换 1.1字符串类型转换成时间Date类型 /** * 给定字符串 转变 为 Date 类型 * @param date 时间 * @param format 时间格式 如:yyyy-MM- ...
- Oracle Client安装报错
Oracle Client安装报错:引用数据不可用于验证此操作系统分发的先决条件 http://tunps.com/p/11797.html 原因是Oracle Client 11g版本不支持最新的W ...
- IDEA 码云 安装
安装方式: 从IDEA插件仓库搜索Gitee下载并安装即可. 登陆并拉取项目代码 1. 启动 idea,选择Check out from Version Control-码云 2. 输入用户名和密码, ...
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
问题:csv.writer().writerow()保存的csv文件,打开时每行后都多一行空行 解决方法:在open()内增加一个参数newline='' 即可 问题现象: 1.代码 with ...