Spring的自动装配

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring的自动装配

dongyaotou   2020-01-11 我要评论

我们都知道,在使用xml进行Bean装配的时候,在业务层装配的时候,我们是这样做的。

 

 SERVICE层调用Dao层的时候,我们还得手动指定,这一步,在实际开发中,我们很容易忽略。那么能不能进行自动装配的呢?答案是肯定的。

改造:

 

 这样就好了,同样能够实现预期的效果。

 

 

配置使用全局的自动装配

applicationContext.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 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
 9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context-3.2.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
14     http://www.springframework.org/schema/aop
15     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
16 
17     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" autowire="no">    
18         <property name="location">
19             <value>classpath:database.properties</value>        
20         </property>
21     </bean>
22     <!-- 配置DataSource -->
23     <!-- <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" autowire="no">
24         <property name="driverClassName" value="${jdbc.driver}" />
25         <property name="url" value="${jdbc.url}" />
26         <property name="username" value="${jdbc.username}" />
27         <property name="password" value="${jdbc.password}" />
28     </bean> -->
29     
30     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" autowire="no">
31         <property name="jndiName">
32             <value>java:comp/env/jndi/smbms</value>
33         </property>         
34     </bean>
35 
36     <!-- 配置SqlSessionFactoryBean -->
37     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="no">
38         <!-- 引用数据源组件 -->
39         <property name="dataSource" ref="dataSource" />
40         <!-- 引用MyBatis配置文件中的配置 -->
41         <property name="configLocation" value="classpath:mybatis-config.xml" />
42     </bean>
43     <!-- 配置DAO -->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" autowire="no">
45         <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
46         <property name="basePackage" value="cn.smbms.dao" />
47     </bean>
48     <!-- 配置业务Bean autowire="default"不指定情况下的默认值,手动装配-->
49    <bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
50   <!--       <property name="userMapper" ref="userMapper" /> -->
51     </bean>
52  <!--    <context:component-scan base-package="cn.smbms.service" /> -->
53     <!-- 定义事务管理器 -->
54     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire="no">
55         <property name="dataSource" ref="dataSource"></property>
56     </bean>
57     <tx:annotation-driven />
58     <!-- <tx:advice id="txAdvice">
59         <tx:attributes>
60             <tx:method name="find*" propagation="SUPPORTS" />
61             <tx:method name="add*" propagation="REQUIRED" />
62             <tx:method name="del*" propagation="REQUIRED" />
63             <tx:method name="update*" propagation="REQUIRED" />
64             <tx:method name="*" propagation="REQUIRED" />
65         </tx:attributes>
66     </tx:advice> -->
67     <!-- 定义切面 -->
68     <!-- <aop:config>
69         <aop:pointcut id="serviceMethod"
70             expression="execution(* cn.smbms.service..*.*(..))" />
71         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
72     </aop:config> -->
73 </beans>

其实这种方式在实际的项目开发中是不推荐使用的,因为这种方式导致组件之间的依赖关系变得不再明确,为后期改bug增加了难度。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们