<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<jedis.version>2.6.2</jedis.version>
<jedis.version>2.9.0</jedis.version>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--Spring整合jedis -->
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg name="host" value="192.168.161.135"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
</beans>
如果ip地址写活
redis.properties
redis.host=192.168.161.135
redis.port=6379
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--1.配置包扫描 -->
<context:component-scan base-package="com.jt"></context:component-scan>
<!--2.配置数据源,它将被mybatis引用 -->
<!--2.1导入pro配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<!-- private Resource[] locations; -->
<list>
<value>classpath:/property/jdbc.properties</value>
<value>classpath:/property/redis.properties</value>
</list>
</property>
</bean>
<!--2.2配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--3.配置事务控制 -->
<tx:annotation-driven />
<!--3.1定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--3.2定义事务策略
propagation="REQUIRED" 执行该操作必须添加事务
propagation="SUPPORTS" 事务支持的,原来的操作有事务,则添加事务
原有的操作没事务,不添加事务
propagation="NEVER"从不添加事务
propagation="REQUIRES_NEW" 不管之前有没有事务,都会创建一个新的事务
read-only="true" 该操作只读
*除此之外的方法只读
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--3.3定义事务切面
Content Model : (pointcut*, advisor*, aspect*)
连接点, 通知, 自定义切面
expression 切入点表达式
within(包名.类名) 按类匹配-控制粒度-粗粒度
execution(返回值类型 包名.类名.方法名(参数列表))
execution(返回值类型 包名.类名.方法名(int))
execution(返回值类型 包名.类名.方法名(String))
execution(返回值类型 包名.类名.方法名(int,String))
-->
<aop:config>
<aop:pointcut expression="execution(* com.jt.manage.service..*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
applicationContext-redis.xml
构造注入注意事项
1.name属性:代表构造方法中的参数名称
注意:使用构造注入时,程序打包部署时最好添加源码
如果没有导入源码,那么程序不会维护形参 则名称name为arg0,arg1,arg2
2index属性
表示下标,从0开始。根据参数位置匹配构造方法
User(String a,String b)
User(int a,String b)
3type=""属性
表示参数类型,一般默认不写。Spring框架内部自动进行强转。
所以在为参数赋值时,必须严格的规范构造方法。否则注入会有问题。
第一种写法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--Spring整合jedis -->
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis.port}"></constructor-arg>
</bean>
</beans>
第二种写法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--Spring整合jedis -->
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg index="0" value="${redis.host}" type="java.lang.String"></constructor-arg>
<constructor-arg index="1" value="${redis.port}" type="int"></constructor-arg>
</bean>
</beans>