Spring笔记(一)

Spring框架:

借鉴:http://www.cnblogs.com/cyjch/archive/2012/02/06/2340415.html

Spring框架依赖于两个jar包:spring.jar、commons-logging.jar。

为便于测试,加入了Junit.jar。

一、一个简单的示例:

接口类:

1 package com.imooc.myinterface;
2 
3 public interface PersonInterface {
4     void show();
5 }

实现类:

 1 package com.imooc.mybean;
 2 
 3 import com.imooc.myinterface.PersonInterface;
 4 
 5 /**
 6  * UserBean实现了PersonInterface接口
 7  */
 8 public class UserBean implements PersonInterface {
 9     @Override
10     public void show() {
11         System.out.println("UserBean 实现了 PersonInterface接口的 show方法");
12     }
13 }

在spring.xml中配置实现类的bean:

 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        xmlns:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 7        xmlns:tx="http://www.springframework.org/schema/tx"
 8        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/aop
11          http://www.springframework.org/schema/aop/spring-aop.xsd
12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
13 
14 
15     <bean id="userBean" class="com.imooc.mybean.UserBean"></bean>
16 
17 </beans>

测试代码:

 1 package com.imooc.test;
 2 
 3 import com.imooc.myinterface.PersonInterface;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class TestBean {
 9     @Test
10     public void testUserBean(){
11         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
12         PersonInterface person = (PersonInterface) context.getBean("userBean");
13         person.show();
14     }
15 }

测试代码的输出结果:

 1 UserBean 实现了 PersonInterface接口的 show方法 

二、实例化Bean的三种方式:

类构造器、静态工厂、实例工厂实例化bean。

BeanFactory类:

 1 package com.imooc.factory;
 2 
 3 import com.imooc.mybean.UserBean;
 4 import com.imooc.myinterface.PersonInterface;
 5 
 6 public class BeanFactory {
 7 
 8     //使用静态工厂实例化使用
 9     public static PersonInterface UserBeanService(){
10         return new UserBean();
11     }
12 
13     public PersonInterface getUserBeanService(){
14         return new UserBean();
15     }
16 }

spring.xml:

1   <!--使用类构造器直接实例化-->
2     <bean id="userBean" class="com.imooc.mybean.UserBean"></bean>
3     <!--使用静态工厂实例化-->
4     <bean id="userBean2" class="com.imooc.factory.BeanFactory" factory-method="UserBeanService"></bean>
5     <!--使用实例工厂实例化-->
6     <bean id="factory" class="com.imooc.factory.BeanFactory"></bean>
7     <bean id="userBean3" factory-bean="factory" factory-method="getUserBeanService"></bean>

三、Bean的节点信息:

默认情况下,Spring的IOC容器启动时会初始化bean,但可以设置bean的lazy-init="true",来延迟bean的启动,这时只有第一次获取bean的时候才会初始化bean

若想对所有bean设置初始延时,则可以再根节点beans设置default-lazy-init="true"。

初始化方法可以在bean中加入init_method="init"属性,其中init为该bean的一个方法;对应有destory-method属性。

在Spring中通过getBean方法获得实例,可以用“==”来测试是一个还是多个实例。

1      ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
2         PersonInterface person = (PersonInterface) context.getBean("userBean3");
3         PersonInterface person2 = (PersonInterface) context.getBean("userBean3");
4         person.show();
5         System.out.println(person == person2);

结果:

 1 UserBean 实现了 PersonInterface接口的 show方法 2 true 

调用了2次getBean但只输出了一次:UserBean 实现了 PersonInterface接口的 show方法,且,返回true说明是一个实例。

Bean交给容器后,默认是单实例的,那么如何获取一个新的实例对象?

scope属性:

令scope=“prototype”即可。

四、Spring的核心机制---依赖注入(Dependence Injection)

依赖注入让bean与bean直接通过配置文件组织在一起;

依赖注入与控制反转(Inversion of Control)是同一个概念;

借鉴:http://www.cnblogs.com/xdp-gacl/p/4249939.html

A对象需要使用B对象时,A对B产生了依赖,传统上我们会new出B(主动权在自己);

但在Spring中创建B的工作由Spring容器完成,然后注入到A中(控制权在Spring容器)

控制反转:创建对象的控制权进行了转移(自己-->spring容器)

什么被反转了?获得依赖对象的方式被反转了!!!


UserDao接口:

1 package com.imooc.myinterface;
2 
3 public interface UserDao  {
4     void show();
5 }

UserDao的实现类:UserDao4MysqlImpl和UserDao4OracleImpl

 1 package com.imooc.impl;
 2 
 3 import com.imooc.myinterface.UserDao;
 4 public class UserDao4MysqlImpl implements UserDao {
 5     @Override
 6     public void show() {
 7         System.out.println("UserDao4MysqlImpl");
 8     }
 9 }
10 
11 
12 package com.imooc.impl;
13 
14 import com.imooc.myinterface.UserDao;
15 
16 public class UserDao4OracleImpl  implements UserDao{
17     @Override
18     public void show() {
19         System.out.println("UserDao4OracleImpl");
20     }
21 }

UserService接口:

 1 package com.imooc.impl;
 2 
 3 import com.imooc.myinterface.UserDao;
 4 import com.imooc.myinterface.UserService;
 5 
 6 /**
 7  * 实现UserService接口是调用了userDao的show方法,一般需要实例化一个UserDao对象:
 8  * UserDao userDao = new UserDao4MysqlImpl();
 9  * 这样耦合度会上升;
10  * 所以可以配置spring.xml进行注入。
11  */
12 public class UserServiceImpl implements UserService {
13     private UserDao userDao;    //私有的,用于注入
14 
15     @Override
16     public void show() {
17         userDao.show();
18     }
19 
20     public UserDao getUserDao() {
21         return userDao;
22     }
23 
24     public void setUserDao(UserDao userDao) {
25         this.userDao = userDao;
26     }
27 }

spring.xml:

1  <bean id="mysqlDao" class="com.imooc.impl.UserDao4MysqlImpl"></bean>
2     <bean id="oracleDao" class="com.imooc.impl.UserDao4OracleImpl"></bean>
3     <bean id="userService" class="com.imooc.impl.UserServiceImpl">
4         <property name="userDao" ref="mysqlDao"></property>
5     </bean>

测试代码:

1    @Test
2     public void testUserService(){
3         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
4         UserService userService = (UserService) context.getBean("userService");
5         userService.show();
6     }

结果:

 1 UserDao4MysqlImpl 

这样就实现了注入!

posted @ 2016-08-30 09:40  重重的博客园  阅读(390)  评论(1编辑  收藏  举报