JavaEE 之 Spring(一)

1.Spring

  a.作用:降低组件与组件之间的关联,便于系统的维护和拓展

  b.核心思想:

    ①IOC:控制反转——配置文件依赖注入

    ②AOP:面向切面编程

 

 

2.IOC/DI

  a.建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
    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-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
    <bean id="jdbcDao" class="class01.JdbcDao"></bean>
    <bean id="myBatisDao" class="class01.MyBatisDao"></bean>
    <bean id="userService" class="class01.UserService">
        <property name="dao" ref="jdbcDao"></property>
    </bean>
</beans>

 

  b.在service层申明private UserDao dao, 并提供对应的set方法

  c.在表现层实例化service的类

//使用BeanFactory的方法(已过时,不建议使用)
    Resource resource=new ClassPathResource("applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(resource);

//使用ApplicationContext的方法(建议使用) ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService service = (UserService)act.getBean("userService");

 

  d.Bean的生命周期

    ①Spring实例化bean

    ②Spring注入bean的属性

    ③Spring设置bean的名字

    ④初始化

    ⑤预处理

    ⑥bean准备完毕可以使用

    ⑦销毁bean

      注:Bean对象一般是随着容器的启动而产生的

 

  e.Bean的属性

    ①lazy-init="true"——加载时不被初始化,在使用时才初始化

    ②scope="singleton"——默认为单利模式

     scope="prototype"——原型模式

    ③init-method="(方法名)"——初始化时执行方法

    ④destroy-method="(方法名)"——销毁时执行方法

 

  f.依赖注入的两个方法

    ①属性注入

<property name="" ref=""></property>

    ②构造注入

<constructor-arg name="dao" ref="MyBatisDao"></constructor-arg>

 

  g.集合属性注入

    ①List

<property name="要注入集和的名字">
        <list>
            <value>zhangsan</value>
            <value>李四</value>
        </list>
</property>

    ②Set

    <property name="names">
        <set>
            <value>zhangsan</value>
            <value>李四</value>
        </set>
    </property>

    ③Map

    <property name="maps">
        <map>
            <entry key="cd" value="成都"></entry>
            <entry key="bj" value="北京"></entry>
        </map>
    </property>

    ④properties

    <property name="properties">
        <props>
            <prop key="userName">root</prop>
            <prop key="userPwd">admin</prop>
        </props>
    </property>

 

  h.自动装配(少用)

    autowire:byName、byType、constructor、autodetect

 

  i.继承

    <bean id="animal" class="com.wode.test.Animal" abstract="true">
        <property name="name" value="动物"></property>
        <property name="age" value="1"></property>
    </bean>    
    <bean id="dog" class="com.wode.test.Dog" parent="animal"></bean>---这里的parent就指派了它的父类
    <bean id="cat" class="com.wode.test.Cat" parent="animal"></bean>

 

  j.注解的使用方式(福利)

    @Repository代表用于dao层的组件

    @Service代表用于service层的组件

    @Controller代表Action层的组件

    @Component代表表示其他的组件

    注意:如果不加任何参数,则id于当前的类名保持一致

 

    @Resource--先按照类型,再按照名字来匹配

    @Autowired---这种方式是自动去找想匹配的,找到了自动注入进来

    

 

  

    

@Resource--先按照类型,再按照名字来匹配---演示错误在获取的时候,我们还可以通过别的方式获取
@Autowired---这种方式是自动去找想匹配的,找到了自动注入进来同样存在上边的问题,使用@Qualifier("idName")来解决

posted @ 2017-03-21 19:38  晨M风  阅读(177)  评论(0编辑  收藏  举报