Spring基础知识及第一个实例

来源:互联网 发布:软件开发的学校 编辑:程序博客网 时间:2024/04/30 22:00

    首先,了解一下几个基本概念:

    依赖注入(DI):由Spring容器负责创建被调用者实例,实例创建后将该实例注入调用者。

    控制反转(IoC):被调用者的实例创建工作不再由调用者创建而由Spring创建。

   面向切面编程(AOP):AOP利用“横切”技术,将封装好的对象剖开,找出对其中多个对象产生影响的公共行为,并将其封装成一个可重用的模块,这个模块称为“切面”。


Spring实例

1、新建SpringFirst项目。下载SpringK开发包,下载Commons-logging包,将所有的包导入到项目中完成Spring框架配置。
2、在src目录下创建com.bean包,在该包下分别创建Person.java、ChineseImpl.java、AmericanImpl文件。代码如下:
package com.bean;public interface Person {public void Speak();}
package com.bean;public class ChinesseImpl 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;}@Overridepublic void Speak() {// TODO Auto-generated method stubSystem.out.println("I am Chinese,My name is "+this.name+",I am "+this.age+" years old!");}}
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;}@Overridepublic void Speak() {// TODO Auto-generated method stubSystem.out.println("I am American,My name is "+this.name+",I am "+this.age+" years old!");}}
3、在src目录下创建applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?><!-- spring配置文件的根元素 --><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd   ">   <!-- bean元素通常就是用来在Spring容器中定义一个类及类的相关配置信息;property用来设置实例中属性的值,而且是通过调用实例的setter方法来设置各个属性的值 -->   <bean id="chinese" class="com.bean.ChinesseImpl">   <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>Tom</value></property>   <property name="age"><value>13</value></property>   </bean></beans>
4、在src目录下创建包com.spring,在该包下创建Test.java,编辑代码如下:
package com.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bean.Person;/** * 注意ChineseImpl和AmericanImpl类的实例并不是main()方法创建的, * 而是通过将ApplicationContext.xml文件加载后,交付给Spring * 当执行getBean()方法时,就可以得到创建实例的引用。 * @author robot-4 * */public class Test {@SuppressWarnings("resource")public static void main(String[] args) {// TODO Auto-generated method stub//创建Spring容器/* * 两种容器:BeanFactory和ApplicationContext * Application是BeanFactory的子接口,BeanFactory提供了Spring配置框架和基本功能, * ApplicationContext还提供了更多附加功能:对国际化的支持,可以对singleton Bean实现预初始化(它初始化后,所有的singleton Bean就都被实例化了) * 实现类有三个: * 1、ClassPathXmlApplicationContext:从类加载路径下的XML文件获取上下文定义信息,创建ApplicationContext. * 2、FileSystemXmlApplicationContext:从文件系统中的的XML文件获取上下文定义信息,创建ApplicationContext. * 3、XmlWebXmlApplicationContext:从Web系统的XML文件获取上下文定义信息,创建ApplicationContext. */ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//获取ChineseImpl实例Person person = (Person) context.getBean("chinese");person.Speak();person = (Person) context.getBean("american");person.Speak();}}







阅读全文
0 0