1. Spring Tool Suite™

方式一:下载对应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项目

导入spring jar包

<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的更多相关文章

  1. Spring_HelloWorld

    目录: 各个类文件: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

  2. Spring学习之旅(一)极速创建Spring框架java工程项目

    编译工具:eclipse 1)创建java工程:Spring_helloworld 2)导入所需jar包: 3)创建一个实体类: public class HelloBeans { private S ...

  3. Spring4.x 基础

    把以下 jar 包加入到工程的 classpath 下: 搭建Spring开发环境 Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于 ...

  4. spring 课程

    官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...

  5. java之spring之helloword

    这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建l ...

  6. Spring从入门到源码—IOC基本使用(二)

    1.spring_helloworld 使用maven的方式来构建项目(Mavaen) 添加对应的pom依赖 pom.xml <dependencies> <!-- https:// ...

随机推荐

  1. Tomcat数据库连接池配置

    Tomcat数据库连接池配置 1.            Server.xml的配置 (1)找到tomcat所在目录下的conf\server.xml文件 (2)在文件最后一个</host> ...

  2. HP LaserJet MFP M227_M231双面打印

    双面打印设置

  3. Cannot create inner bean '(inner bean)#67f903b5' of typ

    严重: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error crea ...

  4. 取消svn add

    svn commit之前,add的东西都可以取消. 通过先执行svn cleanup,再执行svn revert --recursive example_folder.

  5. Little-endian和Big-endian(小端数据和大端数据)

    Little和Big指的是内存地址的大小,end指的是数据的末尾. Little-endian指内存地址低的地方存数据的末尾(即低字节) Big-endian指内存地址高的地方存数据的末尾(即高字节) ...

  6. Linux找不到动态库

    首先系统上得有,只是路径问题 可使用ldd查看可执行程序的依赖库 以下都需要超级权限: find / -name libnet.so.9 // 可能在/usr/lib或/usr/local/lib中 ...

  7. IO模型与select,poll,epoll

    五种:阻塞,非阻塞,IO复印,信号驱动,异步. select,poll,epoll select: 典型用32个32位的整数表示1024个描述符,并发的局限. poll:功能同上,但数据结构不一样(链 ...

  8. Java 深浅拷贝

    2016-07-02 1深拷贝:不仅拷贝对象,而且对象所引用地址的内容一块拷贝.改变一个对象的某个属性,并不影响另一个对象所引用的内容. 2浅拷贝:仅拷贝对象本身,并不对所引用(所指的)内容进行拷贝, ...

  9. python之列表【list】

    这里介绍下列表的功能 #切片:列表[a:b],从下标为a开始,到下标为(b-1)的元素 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[1:6]) # # 结果 ...

  10. 一个性能较好的JVM参数配置

    一个性能较好的web服务器jvm参数配置: -server//服务器模式-Xmx2g //JVM最大允许分配的堆内存,按需分配-Xms2g //JVM初始分配的堆内存,一般和Xmx配置成一样以避免每次 ...