cacheConfig.xml中读取配置文件信息三2010-07-07

本文介绍了一个使用XPath表达式从XML文档中解析特定元素内容的Java类。该类提供了几个方法来读取默认Keyspace、默认ColumnFamily及DataBindings映射。
/** 解析默认Keyspace.
*/
private String readDefaultKeyspace(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
XPathExpression pathExpression = aXpath.compile("//defaultKeyspace");
Node node = (Node) pathExpression.evaluate(aDoc, XPathConstants.NODE);
return node.getTextContent();
}

/** 解析默认ColumnFamily
* @param aDoc 文档对象
* @return String 默认ColumnFamily
*/
private String readDefaultColumnFamily(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
XPathExpression pathExpression =
aXpath.compile("//defaultColumnFamily");
Node node = (Node) pathExpression.evaluate(aDoc, XPathConstants.NODE);
return node.getTextContent();
}

/** 解析DataBindings.
* @param aDoc 文档对象
*/
private Map<String, String> readDataBindingMap(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
Map<String, String> dataBindingMap = new HashMap<String, String>();
XPathExpression pathExpression =
aXpath.compile("//dataBindings/property");
NodeList nodeList = (NodeList) pathExpression.evaluate(aDoc,
XPathConstants.NODESET);
String name = null;
String value = null;
for (int i = 0; i < nodeList.getLength(); i++)
{
name = nodeList.item(i)
.getAttributes()
.getNamedItem("name")
.getNodeValue();
value = nodeList.item(i)
.getAttributes()
.getNamedItem("value")
.getNodeValue();
dataBindingMap.put(name, value);
}
return dataBindingMap;
}
}
1. 添加 Ehcache 依赖 在 Maven 中添加 Ehcache 的依赖: ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> </dependency> ``` 2. 创建 Ehcache 配置文件 在项目的 classpath 下创建 Ehcache 的配置文件 ehcache.xml,配置缓存策略和缓存区域。 示例: ```xml <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.ehcache.org/v3' xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <cache alias="userCache"> <key-type>java.lang.String</key-type> <value-type>com.example.User</value-type> <expiry> <ttl unit="seconds">60</ttl> <tti unit="seconds">30</tti> </expiry> <resources> <heap unit="entries">100</heap> <offheap unit="MB">10</offheap> </resources> </cache> </config> ``` 3. 配置 Ehcache 缓存管理器 在 Spring Boot 中,可以通过注解 @EnableCaching 和 @Configuration 注解来配置 Ehcache 缓存管理器。 示例: ```java @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { Resource resource = new ClassPathResource("ehcache.xml"); Configuration configuration = ConfigurationFactory.parseConfiguration(resource.getInputStream()); return CacheManagerBuilder.newCacheManagerBuilder() .withCache("userCache", UserCacheConfigurationBuilder.newUserCacheConfigurationBuilder().buildConfig(String.class, User.class)) .withCache("bookCache", BookCacheConfigurationBuilder.newBookCacheConfigurationBuilder().buildConfig(Long.class, Book.class)) .withConfiguration(configuration) .build(true); } } ``` 4. 使用 Ehcache 缓存管理器 在需要使用缓存的方法上添加 @Cacheable、@CachePut 或 @CacheEvict 注解来实现缓存的读取、写入和删除。 示例: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "userCache", key = "#id") public User getUserById(String id) { return userRepository.findById(id).orElse(null); } @CachePut(value = "userCache", key = "#user.id") public User saveOrUpdateUser(User user) { return userRepository.save(user); } @CacheEvict(value = "userCache", key = "#id") public void deleteUserById(String id) { userRepository.deleteById(id); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值