spring 的一个属性注入的问题

问题描述

我的项目要使用两个数据源这样的话,自然就出来的两套sessionfactory了.<!--以下是mysql的DAO--><beanid="quartsPlanDao"class="com.hpb.quarts.mysql.dao.impl.QuartsPlanDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryMysql"></property></bean><beanid="hpbMoneyInfoDao"class="com.hpb.quarts.mysql.dao.impl.HpbMoneyInfoDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryMysql"></property></bean>------------------------<!--以下是mysql的DAO--><beanid="memAccessIssueRuleDao"class="com.hpb.quarts.oracle.dao.impl.MemAccessIssueRuleDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryOracle"></property></bean><beanid="sysIssuePlanDao"class="com.hpb.quarts.oracle.dao.impl.SysIssuePlanDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryOracle"></property></bean>我怎么用component或扫描的方式.注入这个sqlsessionFactory这样的话,就不用我每写一个DAO,要到这里来写的BEAN了.他们的包是不同的..很久没登这论坛了.没想到我有这么多的分.

解决方案

解决方案二:
想要不重复这么写,可以搞两个baseDao继承来用
解决方案三:
AbstractRoutingDataSource配合aop是实现多数据源切换.可以最大程度降低代码的耦合,不管是service,dao本身都无需关注数据源问题.不同的数据源仅仅是切点特征不同而以,比如:service方法名.mysqlXXX查询mysql数据源,oracleXXX查询oracle数据源
解决方案四:
@Repository("quartsPlanDao")publicclassQuartsPlanDaoImplimplementsQuartsPlanDao{@ResourceSessionFactorysqlSessionFactoryMysql;}@Repository("sysIssuePlanDao")publicclassSysIssuePlanDaoImplimplementsSysIssuePlanDao{@ResourceSessionFactorysqlSessionFactoryOracle;}

Springconfigxml:<context:component-scanbase-package="com.hpb.quarts"/>

不会这么简单吧,我理解错了?
解决方案五:
貌似我配过多个数据源,我看分来的~
解决方案六:
<?xmlversion="1.0"encoding="utf-8"?><beansxmlns="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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:applicationContext.properties</value></list></property></bean><!--使用annotation自动注册bean,并保证@Required、@Autowired的属性被注入--><context:component-scanbase-package="com.jfpal"></context:component-scan><!--数据源--><beanid="dataSourceOne"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="${jdbc.driver}"></property><propertyname="jdbcUrl"value="${jdbc.url}"></property><propertyname="user"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/><propertyname="minPoolSize"value="1"/><propertyname="maxPoolSize"value="12"/><propertyname="maxIdleTime"value="1800"/><propertyname="acquireIncrement"value="2"/><propertyname="maxStatements"value="10"/><propertyname="initialPoolSize"value="2"/><propertyname="idleConnectionTestPeriod"value="1800"/><propertyname="acquireRetryAttempts"value="30"/><propertyname="acquireRetryDelay"value="100"/><propertyname="breakAfterAcquireFailure"value="false"/><propertyname="testConnectionOnCheckout"value="false"/></bean><!--数据源2--><beanid="dataSourceTwo"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="${jdbc.driver2}"></property><propertyname="jdbcUrl"value="${jdbc.url2}"></property><propertyname="user"value="${jdbc.username2}"/><propertyname="password"value="${jdbc.password2}"/><propertyname="minPoolSize"value="1"/><propertyname="maxPoolSize"value="12"/><propertyname="maxIdleTime"value="1800"/><propertyname="acquireIncrement"value="2"/><propertyname="maxStatements"value="10"/><propertyname="initialPoolSize"value="2"/><propertyname="idleConnectionTestPeriod"value="1800"/><propertyname="acquireRetryAttempts"value="30"/><propertyname="acquireRetryDelay"value="100"/><propertyname="breakAfterAcquireFailure"value="false"/><propertyname="testConnectionOnCheckout"value="false"/></bean><beanid="dynamicDataSource"class="com.jfpal.framework.dao.DynamicDataSource"><!--通过key-value的形式来关联数据源--><propertyname="targetDataSources"><map><entryvalue-ref="dataSourceOne"key="dataSourceOne"></entry><entryvalue-ref="dataSourceTwo"key="dataSourceTwo"></entry></map></property><propertyname="defaultTargetDataSource"ref="dataSourceOne"/></bean><beanid="dataSourceInterceptor"class="com.jfpal.framework.interceptor.DataSourceInterceptor"></bean><aop:config><aop:aspectid="dataSourceAspect"ref="dataSourceInterceptor"><aop:pointcutid="switch"expression="execution(*com.jfpal.pmout.payterminal.service.*.*.*(..))"/><!--进入方法前设置为数据源2--><aop:beforepointcut-ref="switch"method="setdataSourceTwo"/><!--退出方法,设置回数据源1--><aop:afterpointcut-ref="switch"method="setdataSourceOne"/></aop:aspect></aop:config><!--SqlSessionFactory配置--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dynamicDataSource"/><!--自动扫描entity目录,省掉Configuration.xml里的手工配置--><propertyname="typeAliasesPackage"value="com.jfpal"/><!--显式指定Mapper文件位置--><propertyname="mapperLocations"value="classpath*:mybatis/**/*Mapper.xml"/><propertyname="configurationProperties"><props><propkey="dialect">oracle</prop></props></property></bean><beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"><constructor-argindex="0"ref="sqlSessionFactory"/></bean><!--================================事务相关控制=================================================--><beanname="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dynamicDataSource"></property></bean><tx:adviceid="userTxAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="insert*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="select*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="find*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="get*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="save*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="delete*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/></tx:attributes></tx:advice></beans>

全部配置在这里了,具体数据库配置就不用了吧?
解决方案七:
多套SessionFactory必须是可以的;你用Spring3.x来做的话直接用注解把SessionFactory注入到各种Dao中即可。
解决方案八:
引用1楼wobuxiangnila的回复:

想要不重复这么写,可以搞两个baseDao继承来用

现在用的就是继承.但是,父类是注入进去的.子类,出来的时候,非spring对象.
解决方案九:
引用2楼whos2002110的回复:

AbstractRoutingDataSource配合aop是实现多数据源切换.可以最大程度降低代码的耦合,不管是service,dao本身都无需关注数据源问题.不同的数据源仅仅是切点特征不同而以,比如:service方法名.mysqlXXX查询mysql数据源,oracleXXX查询oracle数据源

是的.事务的部分是这样配置的.<aop:pointcutid="appService"expression="execution(*com.hpb..*.*MService*(..))"/>因为他支持表达式.
解决方案十:
引用3楼zhangjihao的回复:

@Repository("quartsPlanDao")publicclassQuartsPlanDaoImplimplementsQuartsPlanDao{@ResourceSessionFactorysqlSessionFactoryMysql;}@Repository("sysIssuePlanDao")publicclassSysIssuePlanDaoImplimplementsSysIssuePlanDao{@ResourceSessionFactorysqlSessionFactoryOracle;}

Springconfigxml:<context:component-scanbase-package="com.hpb.quarts"/>

不会这么简单吧,我理解错了?

你的思路应该是对的..开始的时候,我也是这样配置..但是,不知道为什么注入不进去.而且,sqlsession不是一个属性.它只是那个support的一个SET方法.我不理解,为什么就注入不进去了.就是想用这样的扫描方式但是我没试成功

时间: 2024-07-30 14:21:26

spring 的一个属性注入的问题的相关文章

Spring中属性注入详解_java

本文演示了int.String.数组.list.set.map.Date等属性的注入. 其中Date类型的注入则是借助了Spring提供的属性编辑器来实现的,首先是用到的五个实体类 package com.jadyer.model; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; /** * 常见属性的注入 * @see 包括int,String,Array,lis

Spring boot将配置属性注入到bean 专题

@ConfigurationProperties和@value都是将外部属性注入到对象 @ConfigurationProperties很方便使用. 比用@value注解好吗? 在特定的方案中是的,这只是一个选择问题 @EnableConfigurationProperties //开启属性注入,有此注解就可以通过@autowired注入, 是配合@ConfigurationProperties使用的.如果没有@EnableConfigurationProperties,则使用@Configur

Spring高级应用之注入各类集合

    先定义一个测试类,由于本文将要介绍注入各种集合时如何配置,故这个类包含各种集合,类名和属性名不好取,没有特殊含义: ? 1 2 3 4 5 6 7 8 9 public class Test {     private List<String> listTest;     private Map<String, Object> mapTest;     private Set setTest;     private String[] arrayTest;     priv

面试-spring事务传播属性有哪几种

问题描述 spring事务传播属性有哪几种 今天面试遇到的问题,spring事务传播属性有哪几种?常用的有哪些 解决方案 spring事务传播属性与隔离级别spring事务传播属性与隔离级别Spring 事务传播属性 解决方案二: 1.propagation_required spring默认的事务方式,如果当前存在事务,沿用当前事务,不存在事务,开启一个事务2.propagation_suports 以当前事务方式运行,当前没事务,不开启新的事务3.propagation_mandatory

Guice框架-DI(依赖注入属性注入)

1.2.1 基本属性注入 首先来看一个例子.Service.java  @ImplementedBy(ServiceImpl.class)   public interface Service {       void execute();  }   ServiceImpl.java  public class ServiceImpl implements Service {     @Override     public void execute() {         System.out

spring中如何获取注入bean里方法上的注解?

问题描述 spring中如何获取注入bean里方法上的注解? 有这样一个需求,在spring中,想用反射获取一个注入bean中方法的注解 一般情况下这样的操作就行了: Method method = bean.getClass().getMethod(...); Annotation[] annotations = method.getAnnotations(); 这样就能获取注解了. 但在spring中,这种方法是获取不到annotation的,原因是这里的bean其实是一个代理类.那么问题来

spring-使用Spring在Service中注入了Dao的对象,在Service中怎么获取Dao的对象

问题描述 使用Spring在Service中注入了Dao的对象,在Service中怎么获取Dao的对象 使用Spring在Service中注入了Dao的对象,然后在Service中需要使用Dao对象的话,可以用ApplicationContext的getBean方法,我这边看的一个程序没有用ApplicationContext,而是在Service中对Dao对象进行了声明,然后定义了Dao对象的set和get方法,这样也可以吗,还是说这个程序我没有看明白,希望有明白的可以给解释一下,另希望有大神

spring中bean的注入 单选题

问题描述 最好能解释一下各个选项为什么选或不选. 解决方案 晕了,看代码选d吧解决方案二:选B如果Spring发现用户配置了对象的构造注入方法,那么它在调用构造方法的时候会把构造方法依赖的对象都实例化好,然后再将这些对象作为参数传递给构造方法.----<JavaWeb开发实战宝典>原话.解决方案三:不知道d错在哪里,选b吧解决方案四:A.应该非为构造注入跟属性注入 所以不对B.如果在构造中注入,应该是组合关系,不是依赖,所以不对.C.在关联的对象很多时,应该选用属性注入.D.是对的.实例如下:

使用Hibernate 和 Spring 实现一个事务持久层

1.首先实现一个接口. ============================================================ package com.cqtele.tnbos;import java.util.*;public interface IUserDao { public void insertUser(userInfo user); public List findUser(String sql);} ===============================