方式一:下载对应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:
六月 09, 2015 10:22:39 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.context.support.ClassPathXmlApplicationContext@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)