gpt4 book ai didi

java - redisTemplate @Resource 不适用于 Spring Framework

转载 作者:IT王子 更新时间:2023-10-29 06:10:03 26 4
gpt4 key购买 nike

我正在构建一个 Web 应用程序,它使用 Spring Framework 4.1.3 并将 Jersey 用于 RESTful Web 服务。

我想连接到 Redis 服务器,我正在使用 Spring Data RedisJedis 驱动程序。

这是我的 Beans.xml 文件的样子:

<!-- Jedis ConnectionFactory -->
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="server" p:port="6379" p:use-pool="true"
/>

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnFactory"
/>

这是访问Redis服务器的Servlet:

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

@Path("/")
public class RoutesServlet {

@Autowired
private RedisTemplate<String, String> template;


// inject the template as ListOperations
@Resource(name="redisTemplate")
private ListOperations<String, String> listOps;

@GET
@Produces("text/plain")
public String index() {

listOps.leftPush("user", "name");
...

此时我得到一个 NullPointerException。我猜 @Resource 注释由于某种原因不能正常工作。有什么想法吗?

更新:

这是我的完整 Beans.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xmlns:p="http://www.springframework.org/schema/p"
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

<!-- Initialization for data source -->
<!--bean id="dataSource" class="db.mysql.DBConn"> </bean -->

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/insider" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="jmxEnabled" value="true" />
<property name="testWhileIdle" value="false" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
<property name="testOnReturn" value="false" />
<property name="validationInterval" value="30000" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="maxActive" value="100" />
<property name="initialSize" value="10" />
<property name="maxWait" value="10000" />
<property name="removeAbandonedTimeout" value="60" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<property name="minIdle" value="10" />
<property name="logAbandoned" value="true" />
<property name="removeAbandoned" value="true" />
<property name="jdbcInterceptors"
value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" />
</bean>

<!-- Definition for categoryJDBCTemplate bean -->
<bean id="categoryJDBCTemplate" class="db.mysql.CategoryJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- Definition for itemJDBCTemplate bean -->
<bean id="itemJDBCTemplate" class="db.mysql.ItemJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- Definition for userJDBCTemplate bean -->
<bean id="userJDBCTemplate" class="db.mysql.UserJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- Definition for reviewJDBCTemplate bean -->
<bean id="reviewJDBCTemplate" class="db.mysql.ReviewJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- Jedis ConnectionFactory -->
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="server" p:port="6379" p:use-pool="true"
/>

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnFactory"
/>

</beans>

最佳答案

我相信很简单,名称为 redisTemplate 的 bean 的类型为 RedisTemplate。检索 ListOperations 的一种方法是执行以下操作:

public class RedisExample {
// Just use the RedisTemplate - don't inject the ListOperations
private final RedisTemplate<String, String> redisTemplate;

// Use constructor injection (preferred over field injection)
@Autowired
public RedisExample(final RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}

public void addLink(String userId, URL url) {
// Here is the trick:
// You can either retrieve the ListOperations this way
ListOperations<String, String> listOps = redisTemplate.opsForList();
listOps.leftPush(userId, url.toExternalForm());

// or, you can retrieve it this way
redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());
}
}

该示例表明您应该注入(inject)一个名为 redisTemplateListOperations bean。由于没有这样的 bean,因此注入(inject)失败。只需删除 @Resource 注释(和字段)并使用上述代码即可。

关于java - redisTemplate @Resource 不适用于 Spring Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com