Spring在项目中需要的配置

要想了解Spring,首先要了解三层架构.....自行百度。

1.Spring相关概念: 少数jar+某一个功能的配置文件

Spring容器(轻量级):帮我们管理业务逻辑层,有很多业务逻辑对象,需要对对象的生命周期进行管理(创建、销毁)。

注意:Spring容器不能独立运行,需要借助其他容器(web容器:如tomcat)

业务逻辑处理完后,事务需要提交,需要配置一下。

2.Spring核心组件IOC和AOP

(1)IOC:

DI:Dependency Injection 依赖注入 ,组件不做定位查询,只提供相应方法,由容器创建对象,并调用相应方法设置所需
对象需要的组件

DL:Dependency Loopup依赖查找,容器创建对象并提供回调接口和上下文环境给组件,需要时通过接口从容器中查找对象
依赖查找,现在使用不太多。(EJB使用的更多,将对象创建好后,放到容器中。)


IOC解决:对象谁来创建的问题

DI解决:对象间的关系如何建立的问题。
将容器中的 对象 注入到容器中的另一个对象中

3.SpringIOC核心api

  (1)BeanFactory是Spring中Bean容器,IoC的核心接口,主要用于处理Bean的初始化和配置,建立对象间的依赖关系

  (2)ApplicationContext接口:该接口继承于BeanFactory,增强了BeanFactory,提供了事务处理AOP,国际化,事件传递

4.

  单例管理的对象:
  1.默认情况下,spring在读取xml文件的时候,就会创建对象。
  2.在创建的对象的时候(先调用构造器),会去调用init-method=".."属性值中所指定的方法.
  3.对象在被销毁的时候,会调用destroy-method="..."属性值中所指定的方法.(例如调用container.destroy()方法的时候)
  4.lazy-init="true",可以让这个对象在第一次被访问的时候创建

  非单例管理的对象:
  1.spring读取xml文件的时候,不会创建对象.
  2.在每一次访问这个对象的时候,spring容器都会创建这个对象,并且调用init-method=".."属性值中所指定的方法.
  3.对象销毁的时候,spring容器不会帮我们调用任何方法,因为是非单例,这个类型的对象有很多个,spring容器一旦把这个对象交给你之后,就不再管理这个对象了.

5.注解:

@Autowired:默认按照byType匹配的方式进行注入,可以结合 @Qualifier("beanName")来使用,则可以达到byName的效果,

注意: @Autowired使用后需要在xml文件加入以下配置才能生效:<context:annotation-config/>

@Resource:默认先用byname,如果找不到合适的就再用bytype来注入

@Component是Spring中所有bean组件的通用形式, @Repository @Service @Controller 则是 @Component的细化

  三大框架结合的时候 最好这样配置 不然要报错
    三层架构中:dao-->@Repository
    service-->@Service
    web-->@Controller

注意:
component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有 @Component @Repository @Service @Controller标签的类自动注册到spring容器。对标记了 Spring中的 @Required @Autowired @PostConstruct @PreDestroy @Resource @WebServiceRef @EJB @PersistenceContext @PersistenceUnit等注解的类进行对应的操作使注解生效(包含了annotation-config标签的作用)。

(2)AOP面向切面编程

xml文件配置:
    <!-- 配置dao层对象 -->
    <bean id="dao"
        class="com.briup.aop.dao.AccountDaoImpl"/>
     <!-- 配置目标对象 -->
    <bean name="target"
    class="com.briup.aop.service.AccountServiceImpl">
            <property name="accountDao" ref="dao"></property>
    </bean>
    <!-- 配置切面类 -->
    <bean name="handler" class="com.briup.aop.xml.XmlHandler"></bean>

    <!-- 配置aop的代理 -->
    <aop:config>
        <!-- 定义切入点名为myPointCut -->
        <aop:pointcut expression="execution(public * com.briup.aop.service.*.*(..))" id="myPointCut"/>

        <!-- 定义切面类 以及需要使用的advice -->
        <aop:aspect id="aspect" ref="handler">
            <!-- 表示beforeAdvice会把切面类handler中的beforeTest方法织入到名字叫myPointCut的切入点上面 -->
            <aop:before method="beforeTest" pointcut-ref="myPointCut"/>

            <!-- after表示不管方法是否正常结束都会起作用 -->
            <aop:after method="afterTest" pointcut-ref="myPointCut"/>

            <!-- after-returning表示方法正常结束才会起作用(抛异常时候不起作用) -->
            <aop:after-returning method="afterReturningTest" pointcut-ref="myPointCut"/>

            <aop:around method="aroundTest" pointcut-ref="myPointCut"/>

            <!-- throwing="ex"表示throwingTest方法中接收异常对象的名字一定要是ex -->
            <aop:after-throwing method="throwingTest" pointcut-ref="myPointCut" throwing="ex"/>

        </aop:aspect>
    </aop:config>

项目Spring配置:

  <!-- ioc -->
<!-- Spring可以自动到包下面扫描的java文件
<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,
并注册成bean.也就是@Component的子注解@Service,@Reposity等--> <context:component-scan base-package="com"/>
<!-- <mvc:annotation-driven/>:会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,
并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能--> <mvc:annotation-driven/>
<!-- 使用默认的Servlet来响应静态文件--> <mvc:default-servlet-handler/>

<!-- aop --> <!-- service 管理事物 --> <!-- spring和mybatis整合 --> <!-- 配置数据源 Oracle--> <bean name="dataSource" class="oracle.jdbc.pool.OracleConnectionPoolDataSource"> <property name="networkProtocol"> <value>tcp</value> </property> <property name="databaseName"> <value>XE</value> </property> <property name="driverType"> <value>thin</value> </property> <property name="portNumber"> <value>1521</value> </property> <property name="user"> <value>briup</value> </property> <property name="serverName"> <value>127.0.0.1</value> </property> <property name="password"> <value>briup</value> </property> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" />
     <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/dao/mapper/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- MyBatis 配置spring的事务管理器 相当于aop配置中的切面类--> <bean id="MytxTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务拦截器 相当于aop配置中advice --> <tx:advice id="tx" transaction-manager="MytxTransactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" no-rollback-for="Throwable" /> </tx:attributes> </tx:advice> <!-- 自动代理 --> <aop:config>
    <!-- 配置切入点 --> <aop:pointcut expression="execution(* com.service..*.*(..))" id="myPointCut"/>
    <!-- 配置事务拦截器在哪一个切入点上起作用 --> <aop:advisor advice-ref="tx" pointcut-ref="myPointCut"/> </aop:config>
原文地址:https://www.cnblogs.com/zhaomin08240115/p/9234086.html