1) Spring_HelloWorld

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:

六月 09, 2015 10:22:39 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]1ef7de4: startup date [Tue Jun 09 10:22:39 CST 2015]; root of context hierarchy
六月 09, 2015 10:22:39 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-hello.xml]
Person [name=XiaoMing, age=22, 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文件不存在

六月 09, 2015 10:15:53 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]14e4e31: startup date [Tue Jun 09 10:15:53 CST 2015]; root of context hierarchy
六月 09, 2015 10:15:53 上午 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:344)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at cn.zno.hello.Main.main(Main.java:24)
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:172)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 13 more

2.xml文件中bean未配置

六月 09, 2015 10:17:08 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]1ef7de4: startup date [Tue Jun 09 10:17:08 CST 2015]; root of context hierarchy
六月 09, 2015 10:17:08 上午 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:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at cn.zno.hello.Main.main(Main.java:26)
时间: 2024-11-16 23:02:54

1) Spring_HelloWorld的相关文章

Spring_HelloWorld

目录: 各个类文件: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

spring 课程

官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // 4. Spring_属性配置细节 42:56 // 5. Spring_自动装配 14:05 // 6. Spring_Bean 之间的关系 14:09 // 7. Spring_Bean 的作用域 07:45 // 8. Spring_使用外部属性文件 08:38 // 9. Spring_SpEL 12

java之spring之helloword

这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建lib文件夹 把需要的jar包复制到lib文件夹下,jar包如下: commons-logging-1.1.1.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.j