Spring

一、概述

  1. spring是一站式服务站点。
  2. Spring 是轻量级的框架,核心特性是可以用于开发任何 Java 应用程序
  3. Spring 最认同的技术是控制反转的依赖注入(DI)模式。对于依赖注入,举例:我是这样理解的,将依赖注入四字拆开。依赖:类A依赖于类B。注入:类B通过反转IOC注入到类A中。
  4. 面向方面的程序设计(AOP):一个程序中跨越多个点的功能被称为横切关注点。
  5. 结构图如下:

                

二、HelloWorld案例

        spring的jar包可以从 http://repo.spring.io/release/org/springframework/spring 下载。

第一步:使用eclipse开发软件,创建java Project项目,导入spring的核心库和通用日志包。

         

         

         

项目结构如下:

        

相应的代码如下:

   Test程序入口文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  10.         HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");  
  11.         helloWorld.getTestHelloWorld();  
  12.     }  
  13.   
  14. }  

HelloWorld文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class HelloWorld {  
  4.     private String testHelloWorld;  
  5.   
  6.     public String getTestHelloWorld() {  
  7.         return testHelloWorld;  
  8.     }  
  9.   
  10.     public void setTestHelloWorld(String testHelloWorld) {  
  11.         System.out.println("打印信息:" + testHelloWorld);  
  12.     }  
  13.       
  14. }  

beans配置文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.     <bean name="helloWorld" class="com.liwei.test.HelloWorld">  
  7.         <property name="testHelloWorld" value="世界,你好!"/>  
  8.     </bean>  
  9. </beans>  
在上面的代码中,需要 注意 的是:

  1. beans.xml文件中,property属性中的name必须是HelloWorld中的属性。
  2. Test中的getBean中的字段属性,必须是beans.xml中的name属性名称

输出结果如下:

    

三、spring的相关配置

1.id和name属性

id给Bean 起个名字, 在约束中采用 ID 的约束:唯一, 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号 。id不能出现特殊字符。

nameBean 起个名 name可以出现特殊字符,如果<bean>没有 id 的话 , name 可以当做 id 使用。

2.scope 属性

scope:控制Bean 的作用范围。

singleton :默认值,单例的。

prototype :多例的。

3.Bean 的生命周期

初始化通过配置<bean>标签上的 init-method 作为 Bean 的初始化的时候执行的方法

销毁配置 destroy-method作为 Bean 的销毁的时候执行的方法。销毁方法想要执行,需要是单例创建的 Bean 而且在工厂关闭的时候,Bean 才会被销毁。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.     <bean name="helloWorld" class="com.liwei.test.HelloWorld" init-method="init" destroy-method="destroy">  
  7.         <property name="testHelloWorld" value="世界,你好!"/>  
  8.     </bean>  
  9. </beans>  

除此之外,还需要配置实体类中初始化方法。

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class HelloWorld {  
  4.     private String testHelloWorld;  
  5.   
  6.     public String getTestHelloWorld() {  
  7.         return testHelloWorld;  
  8.     }  
  9.   
  10.     public void setTestHelloWorld(String testHelloWorld) {  
  11.         System.out.println("打印信息:" + testHelloWorld);  
  12.     }  
  13.       
  14.     public void init() {  
  15.         System.out.println("我是初始化方法");  
  16.     }  
  17.       
  18.     public void destroy() {  
  19.         System.out.println("我是销毁方法");  
  20.     }  
  21.       
  22. }     

Test.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class Test {  
  6.   
  7.     public static void main(String[] args) {  
  8.         ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  9.         HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");  
  10.         helloWorld.getTestHelloWorld();  
  11.         ac.close();  
  12.     }  
  13. }  

打印:



四、spring的依赖注入

1. spring构造函数的注入

当容器调用带有一组参数的类构造函数时,基于构造函数的依赖注入(DI) 就完成了,其中每个参数代表一个对其他类的依赖。

Teacher.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class Teacher {  
  4.     public Teacher() {  
  5.         System.out.println("这是教师类的构造方法");  
  6.     }  
  7.   
  8.     public void teaching() {  
  9.         System.out.println("这是老师教的方法");  
  10.     }  
  11. }  

Student.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class Student {  
  4.     private Teacher teacher;  
  5.   
  6.     public Student(Teacher teacher) {  
  7.         System.out.println("这是学生类的构造方法");  
  8.         this.teacher = teacher;  
  9.     }  
  10.     public void teaching() {  
  11.         teacher.teaching();  
  12.         System.out.println("这是调用老师类中教的方法");  
  13.     }  
  14.       
  15. }  

Test.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  10.         Student student = (Student)ac.getBean("student");  
  11.         student.teaching();  
  12.     }  
  13. }  

beans.xml配置文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <constructor-arg ref="teacher"/>  
  9.     </bean>  
  10.       
  11.     <bean name="teacher" class="com.liwei.test.Teacher"></bean>  
  12. </beans>  
打印结果:

        


2. spring的set方式的注入

Teacher.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class Teacher {  
  4.     private String name;  
  5.       
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.   
  10.     public void setName(String name) {  
  11.         System.out.println("这是set方式的注入");  
  12.         this.name = name;  
  13.     }  
  14.   
  15.     public void teaching() {  
  16.         System.out.println(name + "老师, 您好,非常感谢您教我们这个方法!");  
  17.     }  
  18. }  

Student.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. public class Student {  
  4.     private Teacher teacher;  
  5.   
  6.     public Teacher getTeacher() {  
  7.         return teacher;  
  8.     }  
  9.   
  10.     public void setTeacher(Teacher teacher) {  
  11.         this.teacher = teacher;  
  12.     }  
  13.   
  14.     public void teaching() {  
  15.         teacher.teaching();  
  16.     }  
  17.       
  18. }  

beans.xml文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <property name="teacher" ref="teacher"></property>  
  9.     </bean>  
  10.       
  11.     <bean name="teacher" class="com.liwei.test.Teacher">  
  12.         <property name="name" value="小李"/>  
  13.     </bean>  
  14. </beans>  

Test.java文件:

[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  10.         Student student = (Student)ac.getBean("student");  
  11.         student.teaching();  
  12.     }  
  13. }  

你应该注意定义在基于构造函数注入和基于设值函数注入中的 Beans.xml 文件的区别。唯一的区别就是

第一:在基于构造函数注入中,我们使用的是〈bean〉标签中的〈constructor-arg〉元素,而在基于设值函数的注入中,我们使用的是〈bean〉标签中的〈property〉元素。

第二个:你需要注意的点是,如果你要把一个引用传递给一个对象,那么你需要使用 标签的 ref 属性,而如果你要直接传递一个值,那么你应该使用 value 属性。

3. 对象类型的注入

对象类型的注入,其实就如上边所示中:


对teacher类文件进行引入。


另外一种方式如下:

直接将对象放入<property>属性中:



以上两种其结果都是一样的。


4. 注入集合

在上面你已经看到,使用 value 属性来配置基本数据类型和在bean 配置文件中使用<property>标签的 ref 属性来配置对象引用。这两种情况是将值传递给一个 bean。如果想传递多个值,可以使用 Java Collection 类型 List、Set、Map 和 Properties。

元素描述
<list>它有助于连线,如注入一列值,允许重复。
<set>它有助于连线一组值,但不能重复。
<map>它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。
<props>它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。

1)List的注入

Teacher.java文件:


[java]  view plain  copy
  1. package com.liwei.test;  
  2. import java.util.List;  
  3.   
  4. public class Teacher {  
  5.     private List<String> name;  
  6.       
  7.   
  8.     public List<String> getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.   
  13.     public void setName(List<String> name) {  
  14.         this.name = name;  
  15.     }  
  16.   
  17.   
  18.     public void teaching() {  
  19.         for (String string : name) {  
  20.             System.out.println(string + "老师, 您好,非常感谢您教我们这个方法!");  
  21.         }  
  22.     }  
  23. }  

beans.xml文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <property name="teacher" ref="teacher"></property>  
  9.     </bean>  
  10.   
  11.     <bean name="teacher" class="com.liwei.test.Teacher">  
  12.         <property name="name">  
  13.             <list>  
  14.                 <value>小李</value>  
  15.                 <value>小王</value>  
  16.                 <value>张三</value>  
  17.                 <value>王五</value>  
  18.             </list>  
  19.         </property>  
  20.     </bean>  
  21. </beans>  

Student.java和Test.java文件和上面的一样。

打印结果:

    

2)set的注入


主要是beans.xml中的配置,其余的和上面的一样。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <property name="teacher" ref="teacher"></property>  
  9.     </bean>  
  10.   
  11.     <bean name="teacher" class="com.liwei.test.Teacher">  
  12.         <property name="name">  
  13.             <set>  
  14.                 <value>小李</value>  
  15.                 <value>小王</value>  
  16.                 <value>张三</value>  
  17.                 <value>王五</value>  
  18.                 <value>小李</value>  
  19.             </set>  
  20.         </property>  
  21.     </bean>  
  22. </beans>  
打印结果:

3)map的注入

Teacher.java文件:
[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import java.util.Map;  
  4.   
  5. public class Teacher {  
  6.     private Map<String,String> name;  
  7.       
  8.     public Map<String, String> getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(Map<String, String> name) {  
  13.         this.name = name;  
  14.     }  
  15.   
  16.     public void teaching() {  
  17.             System.out.println(name.get("one") + "老师, 您好,非常感谢您教我们这个方法!");  
  18.     }  
  19. }  

beans.xml文件:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <property name="teacher" ref="teacher"></property>  
  9.     </bean>  
  10.   
  11.     <bean name="teacher" class="com.liwei.test.Teacher">  
  12.         <property name="name">  
  13.             <map>  
  14.                 <entry key="one" value="小李"></entry>  
  15.                 <entry key="two" value="小王"></entry>  
  16.                 <entry key="three" value="张三"></entry>  
  17.             </map>  
  18.         </property>  
  19.     </bean>  
  20. </beans>  

Student.java和Test.java文件和上面使用的一样,没有变。

打印结果:

   

注意:在teacher.java文件中name.get("one")中的one,就是beans.xml文件中的one。


4)Properties的注入


Teacher.java文件:


[java]  view plain  copy
  1. package com.liwei.test;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. public class Teacher {  
  6.     private Properties name;  
  7.       
  8.     public Properties getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.   
  13.     public void setName(Properties name) {  
  14.         this.name = name;  
  15.     }  
  16.   
  17.   
  18.     public void teaching() {  
  19.             System.out.println(name.get("one") + "老师, 您好,非常感谢您教我们这个方法!");  
  20.     }  
  21. }  

beans.xml文件:
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <bean name="student" class="com.liwei.test.Student">  
  8.         <property name="teacher" ref="teacher"></property>  
  9.     </bean>  
  10.   
  11.     <bean name="teacher" class="com.liwei.test.Teacher">  
  12.         <property name="name">  
  13.             <props>  
  14.                 <prop key="one">小李</prop>  
  15.                 <prop key="two">小王</prop>  
  16.                 <prop key="three">张三</prop>  
  17.             </props>  
  18.         </property>  
  19.     </bean>  
  20. </beans>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值