From df2f1cbaf89e927b6025b38ddb3791ed85d6bbdc Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Sat, 30 May 2015 15:25:10 +0530 Subject: [PATCH 01/18] Updated the caching behavior code: --- .../baeldung/caching/config/myAppConfig.java | 9 ++ .../baeldung/caching/example/Customer.java | 74 +++++++++++++++ .../caching/example/CustomerDataService.java | 93 +++++++++++++++++++ .../org/baeldung/caching/resouces/config.xml | 30 ++++++ .../test/SpringCachingBehaviorTest.java | 31 +++++++ 5 files changed, 237 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/Customer.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/resouces/config.xml create mode 100644 spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java new file mode 100644 index 0000000000..b5313c6eea --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java @@ -0,0 +1,9 @@ + +@Configuration +@EnableCaching + +public class myAppConfig { + + // Your configuration code goes here. + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java new file mode 100644 index 0000000000..ee8c736141 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -0,0 +1,74 @@ +/** + * The Class Customer. + */ +public class Customer { + + /** The id. */ + private int id; + + /** The name. */ + private String name; + + /** The address. */ + private String customerAddress; + + Customer(String name, String address){ + this.name = name; + this.address = address; + } + + + /** + * Gets the id. + * + * @return the id + */ + public int getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(int id) { + this.id = id; + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the address. + * + * @return the address + */ + public String getCustomerAddress() { + return this.customerAddress; + } + + /** + * Sets the name. + * + * @param name the new name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Sets the address. + * + * @param name the new address + */ + public void setCustomerAddress(String address) { + this.customerAddress = this.name + "," + address; + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java new file mode 100644 index 0000000000..e773cddec6 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -0,0 +1,93 @@ +package org.baeldung.caching.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * The Class CustomerDataService. + */ +@Component +@CacheConfig("addressDemo") +public class CustomerDataService { + + /** The cache manager. */ + @Autowired + CacheManager cacheManager; + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @Cacheable("addresses", “directory”) + + public String getAddress1(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @CacheEvict(value="addresses", allEntries=true) + + public String getAddress2(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) + + public String getAddress3(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @Cacheable // parameter not required as we have declared it using @CacheConfig + + public String getAddress4(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + + @CachePut(value="addresses", condition=”#customer.name=’Tom’”) + @CachePut(value="addresses", unless=”#result.length>64”) + + public String getAddress5(Customer customer) { + + return customer.getAddress(); + } + + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml new file mode 100644 index 0000000000..2ca26f4468 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java new file mode 100644 index 0000000000..5097177803 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -0,0 +1,31 @@ + + +package org.baeldung.caching.test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.stereotype.Component; + + + +/** + * The Class CustomerDataService. + */ +@Component + +public class SpringCachingBehaviorTest { +/** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + @SuppressWarnings("resource") + ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); + example = context.getBean(CustomerDataService.class); + Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + example.getAddress(cust); + } + +} From ec712af5f0d35673f28addc6f6e039988fba2f35 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Fri, 12 Jun 2015 12:43:10 +0530 Subject: [PATCH 02/18] First commmit for Spring Multi-tenancy with Hibernate example. --- .../baeldung/multitenancy/entities/Book.java | 19 ++++ ...urrentSessionTenantIdentifierResolver.java | 33 ++++++ .../implementation/SessionFactoryBean.java | 50 +++++++++ .../SimpleMultiTenantConnectionProvider.java | 35 ++++++ .../service/MultiTenantService.java | 38 +++++++ .../resources/applicationContext.xml | 105 ++++++++++++++++++ .../resources/database.properties | 1 + 7 files changed, 281 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java new file mode 100644 index 0000000000..88cfda8013 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java @@ -0,0 +1,19 @@ +package com.baeldung.multitenancy.entities; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + Long id; + + @NotNull + @Column(name = "name") + String name; + + // standard getters and setters +} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java new file mode 100644 index 0000000000..41eddcd291 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java @@ -0,0 +1,33 @@ +package com.baeldung.multitenancy.implementation; + +import org.hibernate.context.spi.CurrentTenantIdentifierResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.servlet.http.HttpServletRequest; +import java.text.MessageFormat; + +/** + * CurrentSessionTenantIdentifierResolver-- here we fetch the tenantId from the request. + * User: baeldung + * Date: 9/06/15 + */ + +public class CurrentSessionTenantIdentifierResolver implements CurrentTenantIdentifierResolver { + + @Autowired + private HttpServletRequest request; + + @Override + public String resolveCurrentTenantIdentifier() { + String tenantId = request.getHeader("X-TenantId"); + return tenantId; + } + + @Override + public boolean validateExistingCurrentSessions() { + // Additional logic to ensure appropriate connections are made. + return false; + } +} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java new file mode 100644 index 0000000000..48b371d9ae --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java @@ -0,0 +1,50 @@ +package com.baeldung.multitenancy.implementation; + +import org.hibernate.tool.hbm2ddl.SchemaExport; + +import javax.sql.DataSource; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Map; + +/** + * Created with IntelliJ IDEA. + * User: vtajzich + * Date: 8/21/13 + */ +public class SessionFactoryBean extends org.springframework.orm.hibernate4.LocalSessionFactoryBean { + + private Map dataSourceMap; + + @Override + public void afterPropertiesSet() throws IOException { + super.afterPropertiesSet(); + + for (Map.Entry entry : dataSourceMap.entrySet()) { + + Connection connection = null; + + try { + + connection = entry.getValue().getConnection(); + + SchemaExport export = new SchemaExport(getConfiguration(), connection); + + export.setOutputFile(entry.getKey() + "-schema.sql"); + export.setDelimiter(";"); + + // define your rules, here. + //The below code allows creation, but does not allow dropping entries + export.execute(true, true, false, true); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + public void setDataSourceMap(Map dataSourceMap) { + this.dataSourceMap = dataSourceMap; + } +} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java new file mode 100644 index 0000000000..b83f42d1ca --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java @@ -0,0 +1,35 @@ +package com.baeldung.multitenancy.implementation; + +import org.hibernate.service.jdbc.connections.spi.AbstractDataSourceBasedMultiTenantConnectionProviderImpl; + +import javax.sql.DataSource; +import java.util.Map; + +/** + * CurrentSessionTenantIdentifierResolver-- here we fetch the DB conection for the given tenantIdentifier. + * User: baeldung + * Date: 9/06/15 + */ + +public class SimpleMultiTenantConnectionProvider extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl { + + private Map dataSourceMap; + + @Override + protected DataSource selectAnyDataSource() { + return (DataSource) dataSourceMap.values().toArray()[0]; + } + + @Override + protected DataSource selectDataSource(String tenantIdentifier) { + return dataSourceMap.get(tenantIdentifier); + } + + public Map getDataSourceMap() { + return dataSourceMap; + } + + public void setDataSourceMap(Map dataSourceMap) { + this.dataSourceMap = dataSourceMap; + } +} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java new file mode 100644 index 0000000000..a9e14f4144 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java @@ -0,0 +1,38 @@ +package com.baeldung.multitenancy.service; + +import com.baeldung.multitenancy.entities.Book; +import org.hibernate.SessionFactory; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * MultitenantService-- we use the SessionFactory to save and fetch entities. + * User: baeldung + * Date: 9/06/15 + */ + +@Service +public class MultiTenantService { + + @AutoWired + private SessionFactory sessionFactory; + + public void save(Book book) { + sessionFactory.getCurrentSession().save(book); + } + + public Book findBook(Long id) { + return (Book) sessionFactory. + getCurrentSession().get(Book.class, id); + } + + public List findAllBooks() { + return sessionFactory. + getCurrentSession().createQuery("from Books").list(); + } + + public void setSessionFactory(SessionFactory sessionFactory){ + this.sessionFactory = sessionFactory; + } +} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml new file mode 100644 index 0000000000..dfdd2bd346 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties new file mode 100644 index 0000000000..8e7cdcbf54 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties @@ -0,0 +1 @@ +database.driverClassName=com.mysql.jdbc.Driver From 8a49637a14f8344420ab23268bcbd831ff99a562 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Fri, 12 Jun 2015 20:10:43 +0530 Subject: [PATCH 03/18] Added configuration class for the application --- .../com/baeldung/multitenancy/MTConfig.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java new file mode 100644 index 0000000000..0d966acc4d --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java @@ -0,0 +1,62 @@ +package com.baeldung.multitenancy; + +import org.hibernate.tool.hbm2ddl.SchemaExport; +import com.baeldung.multitenancy.implementation; +import javax.sql.DataSource; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Map; + + + +@Configuration +public class MTConfig { + + @Bean + public SessionFactoryBean sessionFactory(Map dataSourceMap, DataSource dataSource){ + + SessionFactoryBean sf = new SessionFactoryBean(); + sf.setDataSourceMap(dataSourceMap); + sf.setDataSource(dataSource); + + try{ + Properties prop = new Properties(); + prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); + prop.setProperty("hibernate.show_sql", "true"); + prop.setProperty("hibernate.multiTenancy", "DATABASE"); + prop.setProperty("hibernate.tenant_identifier_resolver", "currentSessionTenantIdentifierResolver"); + prop.setProperty("hibernate.multi_tenant_connection_provider", "simpleMultiTenantConnectionProvider"); + + sf = new AnnotationConfiguration().setProperties(prop).configure().buildSessionFactory(); + return sf; + + } catch (Throwable ex) { + System.err.println("Failed to load the SessionFactory: " + ex); + throw new ExceptionInInitializerError(ex); + } + + } + + @Bean(name = "currentSessionTenantIdentifierResolver") + @Scope("request", proxyMode = ScopedProxyMode.INTERFACES) + public CurrentSessionTenantIdentifierResolver currentSessionTenantIdentifierResolver(){ + + CurrentSessionTenantIdentifierResolver cstir = new CurrentSessionTenantIdentifierResolver(); + return cstir; + + } + + @Bean(name = "simpleMultiTenantConnectionProvider") + public SimpleMultiTenantConnectionProvider simpleMultiTenantConnectionProvider(Map dataSourceMap){ + + SimpleMultiTenantConnectionProvider smtcp = new SimpleMultiTenantConnectionProvider(); + smtcp.setDataSourceMap(dataSourceMap); + return cstir; + + } + + + + +} From e53f998d0d14b04792e7fae56f2097349a70b409 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Mon, 15 Jun 2015 18:38:38 +0530 Subject: [PATCH 04/18] Upading the pull request after code cleanup. --- .../baeldung/caching/config/myAppConfig.java | 2 +- .../baeldung/caching/example/Customer.java | 94 ++++++------------- .../caching/example/CustomerDataService.java | 15 +-- .../test/SpringCachingBehaviorTest.java | 15 ++- .../com/baeldung/multitenancy/MTConfig.java | 3 +- 5 files changed, 49 insertions(+), 80 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java index b5313c6eea..042e57bc35 100644 --- a/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java +++ b/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java @@ -2,7 +2,7 @@ @Configuration @EnableCaching -public class myAppConfig { +public class MyAppConfig { // Your configuration code goes here. diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java index ee8c736141..c796cc9c60 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -1,74 +1,42 @@ -/** - * The Class Customer. - */ + public class Customer { - /** The id. */ - private int id; - /** The name. */ - private String name; - - /** The address. */ - private String customerAddress; - - Customer(String name, String address){ - this.name = name; - this.address = address; - } + private int id; - /** - * Gets the id. - * - * @return the id - */ - public int getId() { - return id; - } + private String name; - /** - * Sets the id. - * - * @param id the new id - */ - public void setId(int id) { - this.id = id; - } - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } + private String customerAddress; - /** - * Gets the address. - * - * @return the address - */ - public String getCustomerAddress() { - return this.customerAddress; - } + Customer(String name, String address){ + this.name = name; + this.customerAddress = address; + } - /** - * Sets the name. - * - * @param name the new name - */ - public void setName(String name) { - this.name = name; - } - /** - * Sets the address. - * - * @param name the new address - */ - public void setCustomerAddress(String address) { - this.customerAddress = this.name + "," + address; - } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public String getCustomerAddress() { + return this.customerAddress; + } + + public void setName(String name) { + this.name = name; + } + + public void setCustomerAddress(String address) { + this.customerAddress = this.name + "," + address; + } } diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index e773cddec6..4c72d83245 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -18,7 +18,8 @@ public class CustomerDataService { CacheManager cacheManager; /** - * Gets the address. + * The method returns the customer's address, + only it doesn't find it the cache- addresses and directory. * * @param customer the customer * @return the address @@ -32,8 +33,9 @@ public class CustomerDataService { } /** - * Gets the address. - * + * The method returns the customer's address, + but refreshes all the entries in the cache to load new ones. + * * @param customer the customer * @return the address */ @@ -46,7 +48,8 @@ public class CustomerDataService { } /** - * Gets the address. + * The method returns the customer's address, + but not before selectively evicting the cache as per specified paramters. * * @param customer the customer * @return the address @@ -60,7 +63,7 @@ public class CustomerDataService { } /** - * Gets the address. + * The method uses the class level cache to look up for entries. * * @param customer the customer * @return the address @@ -74,7 +77,7 @@ public class CustomerDataService { } /** - * Gets the address. + * The method selectively caches the results that meet the predefined criteria. * * @param customer the customer * @return the address diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java index 5097177803..259f7719eb 100644 --- a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -6,26 +6,23 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; +import static org.junit.Assert.*; +import org.junit.Test; -/** - * The Class CustomerDataService. - */ @Component public class SpringCachingBehaviorTest { -/** - * The main method. - * - * @param args the arguments - */ - public static void main(String[] args) { + + @Test + public void testCaching() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); example = context.getBean(CustomerDataService.class); Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); example.getAddress(cust); + fail("Unable to instantiate the CustomerDataService"); } } diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java index 0d966acc4d..223d35bf5a 100644 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java +++ b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.util.Map; +import org.apache.log4j.Logger; @@ -32,7 +33,7 @@ public class MTConfig { return sf; } catch (Throwable ex) { - System.err.println("Failed to load the SessionFactory: " + ex); + logger.error("Failed to load the SessionFactory: " , ex); throw new ExceptionInInitializerError(ex); } From f06e545485077db34f03f585a87796f88190a57e Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Mon, 15 Jun 2015 19:57:00 +0530 Subject: [PATCH 05/18] Removed caching code --- .../baeldung/caching/config/myAppConfig.java | 9 -- .../baeldung/caching/example/Customer.java | 42 -------- .../caching/example/CustomerDataService.java | 96 ------------------- .../org/baeldung/caching/resouces/config.xml | 30 ------ .../test/SpringCachingBehaviorTest.java | 28 ------ 5 files changed, 205 deletions(-) delete mode 100644 spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java delete mode 100644 spring-all/src/main/java/org/baeldung/caching/example/Customer.java delete mode 100644 spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java delete mode 100644 spring-all/src/main/java/org/baeldung/caching/resouces/config.xml delete mode 100644 spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java deleted file mode 100644 index 042e57bc35..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/config/myAppConfig.java +++ /dev/null @@ -1,9 +0,0 @@ - -@Configuration -@EnableCaching - -public class MyAppConfig { - - // Your configuration code goes here. - -} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java deleted file mode 100644 index c796cc9c60..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ /dev/null @@ -1,42 +0,0 @@ - -public class Customer { - - - private int id; - - - private String name; - - - private String customerAddress; - - Customer(String name, String address){ - this.name = name; - this.customerAddress = address; - } - - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public String getCustomerAddress() { - return this.customerAddress; - } - - public void setName(String name) { - this.name = name; - } - - public void setCustomerAddress(String address) { - this.customerAddress = this.name + "," + address; - } -} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java deleted file mode 100644 index 4c72d83245..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.baeldung.caching.example; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; - -/** - * The Class CustomerDataService. - */ -@Component -@CacheConfig("addressDemo") -public class CustomerDataService { - - /** The cache manager. */ - @Autowired - CacheManager cacheManager; - - /** - * The method returns the customer's address, - only it doesn't find it the cache- addresses and directory. - * - * @param customer the customer - * @return the address - */ - - @Cacheable("addresses", “directory”) - - public String getAddress1(Customer customer) { - - return customer.getAddress(); - } - - /** - * The method returns the customer's address, - but refreshes all the entries in the cache to load new ones. - * - * @param customer the customer - * @return the address - */ - - @CacheEvict(value="addresses", allEntries=true) - - public String getAddress2(Customer customer) { - - return customer.getAddress(); - } - - /** - * The method returns the customer's address, - but not before selectively evicting the cache as per specified paramters. - * - * @param customer the customer - * @return the address - */ - - @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) - - public String getAddress3(Customer customer) { - - return customer.getAddress(); - } - - /** - * The method uses the class level cache to look up for entries. - * - * @param customer the customer - * @return the address - */ - - @Cacheable // parameter not required as we have declared it using @CacheConfig - - public String getAddress4(Customer customer) { - - return customer.getAddress(); - } - - /** - * The method selectively caches the results that meet the predefined criteria. - * - * @param customer the customer - * @return the address - */ - - - @CachePut(value="addresses", condition=”#customer.name=’Tom’”) - @CachePut(value="addresses", unless=”#result.length>64”) - - public String getAddress5(Customer customer) { - - return customer.getAddress(); - } - - -} diff --git a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml deleted file mode 100644 index 2ca26f4468..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java deleted file mode 100644 index 259f7719eb..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ /dev/null @@ -1,28 +0,0 @@ - - -package org.baeldung.caching.test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.stereotype.Component; -import static org.junit.Assert.*; -import org.junit.Test; - - - -@Component - -public class SpringCachingBehaviorTest { - - @Test - public void testCaching() { - @SuppressWarnings("resource") - ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); - example = context.getBean(CustomerDataService.class); - Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); - example.getAddress(cust); - fail("Unable to instantiate the CustomerDataService"); - } - -} From 2916432e9040da6f7d5c1fea108a1231d4a09fa6 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Mon, 15 Jun 2015 20:02:47 +0530 Subject: [PATCH 06/18] Removed the files for new PR request --- .../com/baeldung/multitenancy/MTConfig.java | 63 ----------- .../baeldung/multitenancy/entities/Book.java | 19 ---- ...urrentSessionTenantIdentifierResolver.java | 33 ------ .../implementation/SessionFactoryBean.java | 50 --------- .../SimpleMultiTenantConnectionProvider.java | 35 ------ .../service/MultiTenantService.java | 38 ------- .../resources/applicationContext.xml | 105 ------------------ .../resources/database.properties | 1 - 8 files changed, 344 deletions(-) delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml delete mode 100644 spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java deleted file mode 100644 index 223d35bf5a..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/MTConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.multitenancy; - -import org.hibernate.tool.hbm2ddl.SchemaExport; -import com.baeldung.multitenancy.implementation; -import javax.sql.DataSource; -import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Map; -import org.apache.log4j.Logger; - - - -@Configuration -public class MTConfig { - - @Bean - public SessionFactoryBean sessionFactory(Map dataSourceMap, DataSource dataSource){ - - SessionFactoryBean sf = new SessionFactoryBean(); - sf.setDataSourceMap(dataSourceMap); - sf.setDataSource(dataSource); - - try{ - Properties prop = new Properties(); - prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); - prop.setProperty("hibernate.show_sql", "true"); - prop.setProperty("hibernate.multiTenancy", "DATABASE"); - prop.setProperty("hibernate.tenant_identifier_resolver", "currentSessionTenantIdentifierResolver"); - prop.setProperty("hibernate.multi_tenant_connection_provider", "simpleMultiTenantConnectionProvider"); - - sf = new AnnotationConfiguration().setProperties(prop).configure().buildSessionFactory(); - return sf; - - } catch (Throwable ex) { - logger.error("Failed to load the SessionFactory: " , ex); - throw new ExceptionInInitializerError(ex); - } - - } - - @Bean(name = "currentSessionTenantIdentifierResolver") - @Scope("request", proxyMode = ScopedProxyMode.INTERFACES) - public CurrentSessionTenantIdentifierResolver currentSessionTenantIdentifierResolver(){ - - CurrentSessionTenantIdentifierResolver cstir = new CurrentSessionTenantIdentifierResolver(); - return cstir; - - } - - @Bean(name = "simpleMultiTenantConnectionProvider") - public SimpleMultiTenantConnectionProvider simpleMultiTenantConnectionProvider(Map dataSourceMap){ - - SimpleMultiTenantConnectionProvider smtcp = new SimpleMultiTenantConnectionProvider(); - smtcp.setDataSourceMap(dataSourceMap); - return cstir; - - } - - - - -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java deleted file mode 100644 index 88cfda8013..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/entities/Book.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.multitenancy.entities; - -import javax.persistence.*; -import javax.validation.constraints.NotNull; - -@Entity -public class Book { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id") - Long id; - - @NotNull - @Column(name = "name") - String name; - - // standard getters and setters -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java deleted file mode 100644 index 41eddcd291..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/CurrentSessionTenantIdentifierResolver.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.multitenancy.implementation; - -import org.hibernate.context.spi.CurrentTenantIdentifierResolver; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.servlet.http.HttpServletRequest; -import java.text.MessageFormat; - -/** - * CurrentSessionTenantIdentifierResolver-- here we fetch the tenantId from the request. - * User: baeldung - * Date: 9/06/15 - */ - -public class CurrentSessionTenantIdentifierResolver implements CurrentTenantIdentifierResolver { - - @Autowired - private HttpServletRequest request; - - @Override - public String resolveCurrentTenantIdentifier() { - String tenantId = request.getHeader("X-TenantId"); - return tenantId; - } - - @Override - public boolean validateExistingCurrentSessions() { - // Additional logic to ensure appropriate connections are made. - return false; - } -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java deleted file mode 100644 index 48b371d9ae..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SessionFactoryBean.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.multitenancy.implementation; - -import org.hibernate.tool.hbm2ddl.SchemaExport; - -import javax.sql.DataSource; -import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Map; - -/** - * Created with IntelliJ IDEA. - * User: vtajzich - * Date: 8/21/13 - */ -public class SessionFactoryBean extends org.springframework.orm.hibernate4.LocalSessionFactoryBean { - - private Map dataSourceMap; - - @Override - public void afterPropertiesSet() throws IOException { - super.afterPropertiesSet(); - - for (Map.Entry entry : dataSourceMap.entrySet()) { - - Connection connection = null; - - try { - - connection = entry.getValue().getConnection(); - - SchemaExport export = new SchemaExport(getConfiguration(), connection); - - export.setOutputFile(entry.getKey() + "-schema.sql"); - export.setDelimiter(";"); - - // define your rules, here. - //The below code allows creation, but does not allow dropping entries - export.execute(true, true, false, true); - - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - } - - public void setDataSourceMap(Map dataSourceMap) { - this.dataSourceMap = dataSourceMap; - } -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java deleted file mode 100644 index b83f42d1ca..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/implementation/SimpleMultiTenantConnectionProvider.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.multitenancy.implementation; - -import org.hibernate.service.jdbc.connections.spi.AbstractDataSourceBasedMultiTenantConnectionProviderImpl; - -import javax.sql.DataSource; -import java.util.Map; - -/** - * CurrentSessionTenantIdentifierResolver-- here we fetch the DB conection for the given tenantIdentifier. - * User: baeldung - * Date: 9/06/15 - */ - -public class SimpleMultiTenantConnectionProvider extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl { - - private Map dataSourceMap; - - @Override - protected DataSource selectAnyDataSource() { - return (DataSource) dataSourceMap.values().toArray()[0]; - } - - @Override - protected DataSource selectDataSource(String tenantIdentifier) { - return dataSourceMap.get(tenantIdentifier); - } - - public Map getDataSourceMap() { - return dataSourceMap; - } - - public void setDataSourceMap(Map dataSourceMap) { - this.dataSourceMap = dataSourceMap; - } -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java b/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java deleted file mode 100644 index a9e14f4144..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/java/com/baeldung/multitenancy/service/MultiTenantService.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.multitenancy.service; - -import com.baeldung.multitenancy.entities.Book; -import org.hibernate.SessionFactory; -import org.springframework.stereotype.Service; - -import java.util.List; - -/** - * MultitenantService-- we use the SessionFactory to save and fetch entities. - * User: baeldung - * Date: 9/06/15 - */ - -@Service -public class MultiTenantService { - - @AutoWired - private SessionFactory sessionFactory; - - public void save(Book book) { - sessionFactory.getCurrentSession().save(book); - } - - public Book findBook(Long id) { - return (Book) sessionFactory. - getCurrentSession().get(Book.class, id); - } - - public List findAllBooks() { - return sessionFactory. - getCurrentSession().createQuery("from Books").list(); - } - - public void setSessionFactory(SessionFactory sessionFactory){ - this.sessionFactory = sessionFactory; - } -} diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml deleted file mode 100644 index dfdd2bd346..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/applicationContext.xml +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties b/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties deleted file mode 100644 index 8e7cdcbf54..0000000000 --- a/spring-all/src/main/java/org/baeldung/multi-tenancy/resources/database.properties +++ /dev/null @@ -1 +0,0 @@ -database.driverClassName=com.mysql.jdbc.Driver From 729b5218900ed57b2fd12b3e11c666ef0eb5f1f7 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Mon, 15 Jun 2015 20:19:00 +0530 Subject: [PATCH 07/18] Caching behavior code added with code cleanup --- .../baeldung/caching/config/MyAppConfig.java | 9 ++ .../baeldung/caching/example/Customer.java | 42 ++++++++ .../caching/example/CustomerDataService.java | 96 +++++++++++++++++++ .../org/baeldung/caching/resouces/config.xml | 30 ++++++ .../test/SpringCachingBehaviorTest.java | 28 ++++++ 5 files changed, 205 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/Customer.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/resouces/config.xml create mode 100644 spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java new file mode 100644 index 0000000000..042e57bc35 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java @@ -0,0 +1,9 @@ + +@Configuration +@EnableCaching + +public class MyAppConfig { + + // Your configuration code goes here. + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java new file mode 100644 index 0000000000..c796cc9c60 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -0,0 +1,42 @@ + +public class Customer { + + + private int id; + + + private String name; + + + private String customerAddress; + + Customer(String name, String address){ + this.name = name; + this.customerAddress = address; + } + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public String getCustomerAddress() { + return this.customerAddress; + } + + public void setName(String name) { + this.name = name; + } + + public void setCustomerAddress(String address) { + this.customerAddress = this.name + "," + address; + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java new file mode 100644 index 0000000000..4c72d83245 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -0,0 +1,96 @@ +package org.baeldung.caching.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * The Class CustomerDataService. + */ +@Component +@CacheConfig("addressDemo") +public class CustomerDataService { + + /** The cache manager. */ + @Autowired + CacheManager cacheManager; + + /** + * The method returns the customer's address, + only it doesn't find it the cache- addresses and directory. + * + * @param customer the customer + * @return the address + */ + + @Cacheable("addresses", “directory”) + + public String getAddress1(Customer customer) { + + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but refreshes all the entries in the cache to load new ones. + * + * @param customer the customer + * @return the address + */ + + @CacheEvict(value="addresses", allEntries=true) + + public String getAddress2(Customer customer) { + + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but not before selectively evicting the cache as per specified paramters. + * + * @param customer the customer + * @return the address + */ + + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) + + public String getAddress3(Customer customer) { + + return customer.getAddress(); + } + + /** + * The method uses the class level cache to look up for entries. + * + * @param customer the customer + * @return the address + */ + + @Cacheable // parameter not required as we have declared it using @CacheConfig + + public String getAddress4(Customer customer) { + + return customer.getAddress(); + } + + /** + * The method selectively caches the results that meet the predefined criteria. + * + * @param customer the customer + * @return the address + */ + + + @CachePut(value="addresses", condition=”#customer.name=’Tom’”) + @CachePut(value="addresses", unless=”#result.length>64”) + + public String getAddress5(Customer customer) { + + return customer.getAddress(); + } + + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml new file mode 100644 index 0000000000..2ca26f4468 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java new file mode 100644 index 0000000000..259f7719eb --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -0,0 +1,28 @@ + + +package org.baeldung.caching.test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.stereotype.Component; +import static org.junit.Assert.*; +import org.junit.Test; + + + +@Component + +public class SpringCachingBehaviorTest { + + @Test + public void testCaching() { + @SuppressWarnings("resource") + ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); + example = context.getBean(CustomerDataService.class); + Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + example.getAddress(cust); + fail("Unable to instantiate the CustomerDataService"); + } + +} From 371445d1d74d0f994a13e40a63dca5d263fe3759 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Wed, 17 Jun 2015 09:56:50 +0530 Subject: [PATCH 08/18] Update Customer.java --- .../java/org/baeldung/caching/example/Customer.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java index c796cc9c60..b2f39b6029 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -1,21 +1,20 @@ public class Customer { - private int id; - - private String name; - - private String customerAddress; + Customer(){ + this.name=""; + this.customerAddress=""; + } + Customer(String name, String address){ this.name = name; this.customerAddress = address; } - public int getId() { return id; } From 90e8b48955abb6a83b727c3c23f550f66fb297cd Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Wed, 17 Jun 2015 09:57:49 +0530 Subject: [PATCH 09/18] Update CustomerDataService.java --- .../java/org/baeldung/caching/example/CustomerDataService.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index 4c72d83245..a33923a197 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -6,9 +6,6 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; -/** - * The Class CustomerDataService. - */ @Component @CacheConfig("addressDemo") public class CustomerDataService { From e71147dd3550b93b40fdbcd2d524d83b025da9d8 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Wed, 17 Jun 2015 10:03:27 +0530 Subject: [PATCH 10/18] Update CustomerDataService.java --- .../caching/example/CustomerDataService.java | 114 +++++++++--------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index a33923a197..dbd46f7ef1 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -10,84 +10,84 @@ import org.springframework.stereotype.Component; @CacheConfig("addressDemo") public class CustomerDataService { - /** The cache manager. */ - @Autowired - CacheManager cacheManager; + /** The cache manager. */ + @Autowired + CacheManager cacheManager; - /** - * The method returns the customer's address, - only it doesn't find it the cache- addresses and directory. - * - * @param customer the customer - * @return the address - */ + /** + * The method returns the customer's address, + only it doesn't find it the cache- addresses and directory. + * + * @param customer the customer + * @return the address + */ - @Cacheable("addresses", “directory”) + @Cacheable("addresses", “directory”) - public String getAddress1(Customer customer) { + public String getAddress1(Customer customer) { - return customer.getAddress(); - } + return customer.getAddress(); + } - /** - * The method returns the customer's address, - but refreshes all the entries in the cache to load new ones. - * - * @param customer the customer - * @return the address - */ + /** + * The method returns the customer's address, + but refreshes all the entries in the cache to load new ones. + * + * @param customer the customer + * @return the address + */ - @CacheEvict(value="addresses", allEntries=true) + @CacheEvict(value="addresses", allEntries=true) - public String getAddress2(Customer customer) { + public String getAddress2(Customer customer) { - return customer.getAddress(); - } + return customer.getAddress(); + } - /** - * The method returns the customer's address, - but not before selectively evicting the cache as per specified paramters. - * - * @param customer the customer - * @return the address - */ + /** + * The method returns the customer's address, + but not before selectively evicting the cache as per specified paramters. + * + * @param customer the customer + * @return the address + */ - @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) - public String getAddress3(Customer customer) { + public String getAddress3(Customer customer) { - return customer.getAddress(); - } + return customer.getAddress(); + } - /** - * The method uses the class level cache to look up for entries. - * - * @param customer the customer - * @return the address - */ + /** + * The method uses the class level cache to look up for entries. + * + * @param customer the customer + * @return the address + */ - @Cacheable // parameter not required as we have declared it using @CacheConfig + @Cacheable // parameter not required as we have declared it using @CacheConfig - public String getAddress4(Customer customer) { + public String getAddress4(Customer customer) { - return customer.getAddress(); - } + return customer.getAddress(); + } - /** - * The method selectively caches the results that meet the predefined criteria. - * - * @param customer the customer - * @return the address - */ + /** + * The method selectively caches the results that meet the predefined criteria. + * + * @param customer the customer + * @return the address + */ - @CachePut(value="addresses", condition=”#customer.name=’Tom’”) - @CachePut(value="addresses", unless=”#result.length>64”) + @CachePut(value="addresses", condition=”#customer.name=’Tom’”) + @CachePut(value="addresses", unless=”#result.length>64”) - public String getAddress5(Customer customer) { + public String getAddress5(Customer customer) { - return customer.getAddress(); - } + return customer.getAddress(); + } } From adfa9cb485ff5ff312741b3957fbdc9d7bdd6112 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Wed, 17 Jun 2015 10:20:25 +0530 Subject: [PATCH 11/18] Update CustomerDataService.java --- .../java/org/baeldung/caching/example/CustomerDataService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index dbd46f7ef1..38e980bc08 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -10,7 +10,6 @@ import org.springframework.stereotype.Component; @CacheConfig("addressDemo") public class CustomerDataService { - /** The cache manager. */ @Autowired CacheManager cacheManager; From 96671227b4979b7c96f17e19f914712d43fc2e53 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:50:11 +0530 Subject: [PATCH 12/18] Update Customer.java --- .../java/org/baeldung/caching/example/Customer.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java index b2f39b6029..f715cbb85a 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -5,7 +5,7 @@ public class Customer { private String name; private String customerAddress; - Customer(){ + public Customer(){ this.name=""; this.customerAddress=""; } @@ -26,15 +26,15 @@ public class Customer { public String getName() { return name; } + + public void setName(String name) { + this.name = name; + } public String getCustomerAddress() { return this.customerAddress; } - public void setName(String name) { - this.name = name; - } - public void setCustomerAddress(String address) { this.customerAddress = this.name + "," + address; } From 1105949abf5f79f16e8f7eb7358cc74a106c9b50 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:52:07 +0530 Subject: [PATCH 13/18] Update CustomerDataService.java --- .../caching/example/CustomerDataService.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index 38e980bc08..ef41840254 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -20,11 +20,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Cacheable("addresses", “directory”) - public String getAddress1(Customer customer) { - return customer.getAddress(); } @@ -35,11 +32,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @CacheEvict(value="addresses", allEntries=true) - public String getAddress2(Customer customer) { - return customer.getAddress(); } @@ -50,11 +44,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) - public String getAddress3(Customer customer) { - return customer.getAddress(); } @@ -64,11 +55,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Cacheable // parameter not required as we have declared it using @CacheConfig - public String getAddress4(Customer customer) { - return customer.getAddress(); } @@ -78,13 +66,9 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - - @CachePut(value="addresses", condition=”#customer.name=’Tom’”) @CachePut(value="addresses", unless=”#result.length>64”) - public String getAddress5(Customer customer) { - return customer.getAddress(); } From d54190eaa8a98fc23f050dada14cd42e9501e8ec Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:53:33 +0530 Subject: [PATCH 14/18] Update SpringCachingBehaviorTest.java --- .../org/baeldung/caching/test/SpringCachingBehaviorTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java index 259f7719eb..0341104d1b 100644 --- a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -9,12 +9,8 @@ import org.springframework.stereotype.Component; import static org.junit.Assert.*; import org.junit.Test; - - @Component - public class SpringCachingBehaviorTest { - @Test public void testCaching() { @SuppressWarnings("resource") @@ -24,5 +20,4 @@ public class SpringCachingBehaviorTest { example.getAddress(cust); fail("Unable to instantiate the CustomerDataService"); } - } From 5637f0dcac2bd9ad5cdea67a2be5cbda87b69688 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:54:13 +0530 Subject: [PATCH 15/18] Update SpringCachingBehaviorTest.java --- .../org/baeldung/caching/test/SpringCachingBehaviorTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java index 0341104d1b..989049bee0 100644 --- a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -1,5 +1,3 @@ - - package org.baeldung.caching.test; import org.springframework.beans.factory.annotation.Autowired; From ff2a3c0f5be25bfc087388a8bd3de629db7ea319 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:54:45 +0530 Subject: [PATCH 16/18] Update CustomerDataService.java --- .../java/org/baeldung/caching/example/CustomerDataService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index ef41840254..625caa802e 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -71,6 +71,4 @@ public class CustomerDataService { public String getAddress5(Customer customer) { return customer.getAddress(); } - - } From e3e702920b4e716f6c926786e331093c42b41777 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Fri, 19 Jun 2015 01:55:37 +0530 Subject: [PATCH 17/18] Update MyAppConfig.java --- .../src/main/java/org/baeldung/caching/config/MyAppConfig.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java index 042e57bc35..d02727b421 100644 --- a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java +++ b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java @@ -1,9 +1,6 @@ @Configuration @EnableCaching - public class MyAppConfig { - // Your configuration code goes here. - } From 9eb3faa607b1f9715e87c7c85aee7ed6d68b96e6 Mon Sep 17 00:00:00 2001 From: abhibavishi Date: Sat, 20 Jun 2015 15:27:11 +0530 Subject: [PATCH 18/18] Update Customer.java --- .../src/main/java/org/baeldung/caching/example/Customer.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java index f715cbb85a..f4e6accae0 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -6,8 +6,6 @@ public class Customer { private String customerAddress; public Customer(){ - this.name=""; - this.customerAddress=""; } Customer(String name, String address){