commit
c576a3cb1e
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="wst.jsdt.web" version="1.0"/>
|
||||
<installed facet="jst.web" version="3.0"/>
|
||||
<installed facet="java" version="1.8"/>
|
||||
<installed facet="jst.web" version="3.1"/>
|
||||
</faceted-project>
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.caching.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.baeldung.caching.example.CustomerDataService;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CachingConfig {
|
||||
|
||||
@Bean
|
||||
public CustomerDataService customerDataService() {
|
||||
return new CustomerDataService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
final SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses")));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package org.baeldung.caching.config;
|
||||
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class MyAppConfig {
|
||||
// Your configuration code goes here.
|
||||
}
|
|
@ -10,12 +10,18 @@ import org.springframework.cache.annotation.Caching;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@CacheConfig(cacheNames = { "addressDemo" })
|
||||
@CacheConfig(cacheNames = { "addresses" })
|
||||
public class CustomerDataService {
|
||||
|
||||
@Autowired
|
||||
CacheManager cacheManager;
|
||||
|
||||
// this method configuration is equivalent to xml configuration
|
||||
@Cacheable(value = "addresses", key = "#customer.name")
|
||||
public String getAddress(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method returns the customer's address,
|
||||
only it doesn't find it the cache- addresses and directory.
|
||||
|
|
|
@ -10,8 +10,7 @@
|
|||
|
||||
<cache:annotation-driven/>
|
||||
|
||||
<context:annotation-config/>
|
||||
<bean class="org.baeldung.caching.config.MyAppConfig"/>
|
||||
<!-- <context:annotation-config/> -->
|
||||
|
||||
<!-- the service that you wish to make cacheable. -->
|
||||
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
|
||||
|
@ -21,7 +20,6 @@
|
|||
<set>
|
||||
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/>
|
||||
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/>
|
||||
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addressDemo"/>
|
||||
</set>
|
||||
</property>
|
||||
</bean>
|
||||
|
@ -34,7 +32,7 @@
|
|||
</cache:advice>
|
||||
|
||||
|
||||
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
|
||||
<!-- apply the behavior to all the implementations of CustomerDataService -->
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/>
|
||||
</aop:config>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package org.baeldung.caching.test;
|
||||
|
||||
import org.baeldung.caching.config.CachingConfig;
|
||||
import org.baeldung.caching.example.Customer;
|
||||
import org.baeldung.caching.example.CustomerDataService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
@Component
|
||||
public class SpringCachingBehaviorTest {
|
||||
|
@ -13,12 +13,16 @@ public class SpringCachingBehaviorTest {
|
|||
@Test
|
||||
public void testCaching() {
|
||||
@SuppressWarnings("resource")
|
||||
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
|
||||
final
|
||||
// final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.register(CachingConfig.class);
|
||||
context.refresh();
|
||||
final CustomerDataService service = context.getBean(CustomerDataService.class);
|
||||
|
||||
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
||||
service.getAddress1(cust);
|
||||
service.getAddress1(cust);
|
||||
service.getAddress(cust);
|
||||
service.getAddress(cust);
|
||||
|
||||
// fail("Unable to instantiate the CustomerDataService");
|
||||
}
|
||||
|
|
|
@ -6,11 +6,14 @@ import org.apache.http.HttpResponse;
|
|||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.impl.auth.DigestScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
|
@ -27,9 +30,11 @@ public class ClientPreemptiveDigestAuthentication {
|
|||
public static void main(final String[] args) throws Exception {
|
||||
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
|
||||
|
||||
final DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
|
||||
final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
||||
try {
|
||||
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
|
||||
// Create AuthCache instance
|
||||
final AuthCache authCache = new BasicAuthCache();
|
||||
|
@ -43,7 +48,7 @@ public class ClientPreemptiveDigestAuthentication {
|
|||
|
||||
// Add AuthCache to the execution context
|
||||
final BasicHttpContext localcontext = new BasicHttpContext();
|
||||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
|
||||
|
||||
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1");
|
||||
|
||||
|
@ -63,7 +68,7 @@ public class ClientPreemptiveDigestAuthentication {
|
|||
}
|
||||
} finally {
|
||||
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
|
||||
httpclient.getConnectionManager().shutdown();
|
||||
httpclient.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.net.URI;
|
|||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.impl.auth.DigestScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
|
@ -41,7 +41,7 @@ public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpCompon
|
|||
|
||||
// Add AuthCache to the execution context
|
||||
final BasicHttpContext localcontext = new BasicHttpContext();
|
||||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
|
||||
return localcontext;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue