spring (1)demo 初识IoC容器
最近开始学习spring,第一天 先敲一个 实例demo,大概了解
1.新建一个java project,导入项目所学的jar包,这个jar包很复杂,本来打算用spring-framework3.3.1的,可以去了spring官网发现没有这个版本的可以下载,而且都是maven与gradle有关的,这里推荐一个比较好的下载jar的地址:
http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.1.RELEASE/
在这里下载的jar比较靠谱,另外在往工程里面加入jar包的时候学会new user library这一类的包,这样项目中的jar包就不是很乱,分类比较清楚
2.新建com.bean包
写一个person接口
package com.bean; public interface Person { public void speak(); }
3.新建两个实现这个接口的类
package com.bean; public class ChineseImpl implements Person { private String name; private int age; 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; } @Override public void speak() { System.out.println("chinese,name,age"+this.name+this.age); } }
另一个
package com.bean; public class AmericanImpl implements Person { private String name; private int age; 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; } @Override public void speak() { System.out.println("american,name,age"+this.name+this.age); } }
在spring中,spring容器负责调用这两个类的setter方法以设置实例中属性的值。 这里要用到xml的配置文件
4在src下新建applicationContext.xml这个配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true" default-autowire="byName"> <bean id="chinese" class="com.bean.ChineseImpl"> <property name="name"><value>小明</value></property> <property name="age"><value>10</value></property> </bean> <bean id="american" class="com.bean.AmericanImpl"> <property name="name"><value>jack</value></property> <property name="age"><value>10</value></property> </bean> </beans>
在配置文件中,他帮我们办了这样一件事,就是
<bean id="chinese" class="com.bean.ChineseImpl"> <property name="name"><value>小明</value></property> <property name="age"><value>10</value></property> </bean>
就是
ChineseImpl chinese=new ChineseImpl();
chinese.setName("小明");
chinese.setAge("10");
这样的作用
5新建com.spring包,新建test类
package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.Person; public class Test { public static void main(String[] args) { System.out.println("a"); ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person=(Person)context.getBean("chinese"); person.speak(); //person=(Person)context.getBean("american"); //person.speak(); } }
6查看输出的结果
没有学不会的程序员,只有不努力的程序员。