Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private BeanB dependency;
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause1;
public class BeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB1 implements IBeanB {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB2 implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause2;
public interface IBeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause3;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
public BeanA() {
super();
throw new NullPointerException();
}
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause4;
import org.springframework.stereotype.Component;
@Component
public abstract class BeanA implements IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause4;
public interface IBeanA {
//
}
@@ -0,0 +1,13 @@
package org.baeldung.ex.beancreationexception.cause5;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements IBeanA {
public BeanA(final String name) {
super();
System.out.println(name);
}
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause5;
public interface IBeanA {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause6;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause6;
import org.springframework.stereotype.Component;
@Component
public class BeanB implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause6;
public interface IBeanB {
//
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.cause8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements IBeanA {
private IBeanB beanB;
@Autowired
public BeanA(final IBeanB beanB) {
super();
this.beanB = beanB;
}
}
@@ -0,0 +1,15 @@
package org.baeldung.ex.beancreationexception.cause8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanB implements IBeanB {
final IBeanA beanA;
@Autowired
public BeanB(final IBeanA beanA) {
super();
this.beanA = beanA;
}
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause8;
public interface IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause8;
public interface IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public abstract class BeanA implements IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public class BeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public interface IBeanA {
//
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause1")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause2")
public class Cause2ContextWithJavaConfig {
public Cause2ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause4")
@ImportResource("classpath:beancreationexception_cause4.xml")
public class Cause4ContextWithJavaConfig {
public Cause4ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause5")
public class Cause5ContextWithJavaConfig {
public Cause5ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause6")
@ImportResource("classpath:beancreationexception_cause6.xml")
public class Cause6ContextWithJavaConfig {
public Cause6ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause7")
@ImportResource("classpath:beancreationexception_cause7.xml")
public class Cause7ContextWithJavaConfig {
public Cause7ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause8")
public class Cause8ContextWithJavaConfig {
public Cause8ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,30 @@
package org.baeldung.ex.beancreationexception.spring;
import org.baeldung.ex.beancreationexception.cause9.BeanB;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause9")
@ImportResource("classpath:beancreationexception_cause9.xml")
public class Cause9ContextWithJavaConfig {
@Autowired
BeanFactory beanFactory;
public Cause9ContextWithJavaConfig() {
super();
}
// beans
@Bean
public BeanB beanB() {
beanFactory.getBean("beanA");
return new BeanB();
}
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beandefinitionstoreexception.cause2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Value("${some.property2}")
private String someProperty2;
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause1")
@ImportResource("beans.xml")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,27 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause2")
public class Cause2ContextWithJavaConfig {
@Value("${some.property}")
private String someProperty;
public Cause2ContextWithJavaConfig() {
super();
}
// beans
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,26 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause1")
@Import(PersistenceConfig.class)
public class Cause1DataContextWithJavaConfig {
public Cause1DataContextWithJavaConfig() {
super();
}
// beans
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause2")
@Import(PersistenceConfig.class)
public class Cause2DataContextWithJavaConfig {
public Cause2DataContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause3")
@Import(PersistenceConfig.class)
public class Cause3DataContextWithJavaConfig {
public Cause3DataContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,29 @@
package org.baeldung.ex.mappingexception.cause1.persistence.model;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause2.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause3.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause4.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause1.persistence" })
public class Cause1PersistenceConfig {
@Autowired
private Environment env;
public Cause1PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause1.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,78 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause2.persistence" })
public class Cause2PersistenceConfig {
@Autowired
private Environment env;
public Cause2PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
// sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause2.persistence.model" });
// sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,79 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.baeldung.ex.mappingexception.cause3.persistence.model.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause2.persistence" })
public class Cause3PersistenceConfig {
@Autowired
private Environment env;
public Cause3PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
// sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause2.persistence.model" });
sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-derby.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause1NonTransientConfig {
@Autowired
private Environment env;
public Cause1NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,75 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-derby.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause4NonTransientConfig {
@Autowired
private Environment env;
public Cause4NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,75 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql-incorrect.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause5NonTransientConfig {
@Autowired
private Environment env;
public Cause5NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private BeanB dependency;
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause1;
public class BeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB1 implements IBeanB {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB2 implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
public interface IBeanB {
//
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause3;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements InitializingBean {
@Autowired
private ApplicationContext context;
@Override
public void afterPropertiesSet() {
context.getBean("someBeanName");
}
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause1")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause2")
public class Cause2ContextWithJavaConfig {
public Cause2ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,26 @@
package org.baeldung.persistence;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class Setup implements ApplicationListener<ContextRefreshedEvent> {
private boolean setupDone;
public Setup() {
super();
}
//
@Override
public final void onApplicationEvent(final ContextRefreshedEvent event) {
if (!setupDone) {
System.out.println();
setupDone = true;
}
}
}
@@ -0,0 +1,65 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.base.Preconditions;
@SuppressWarnings("unchecked")
public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
// API
protected final void setClazz(final Class<T> clazzToSet) {
clazz = Preconditions.checkNotNull(clazzToSet);
}
@Override
public final T findOne(final long id) {
return (T) getCurrentSession().get(clazz, id);
}
@Override
public final List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
@Override
public final void create(final T entity) {
Preconditions.checkNotNull(entity);
// getCurrentSession().persist(entity);
getCurrentSession().saveOrUpdate(entity);
}
@Override
public final T update(final T entity) {
Preconditions.checkNotNull(entity);
return (T) getCurrentSession().merge(entity);
}
@Override
public final void delete(final T entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().delete(entity);
}
@Override
public final void deleteById(final long entityId) {
final T entity = findOne(entityId);
Preconditions.checkState(entity != null);
delete(entity);
}
protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}
@@ -0,0 +1,44 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
@Override
public T findOne(final long id) {
return getDao().findOne(id);
}
@Override
public List<T> findAll() {
return getDao().findAll();
}
@Override
public void create(final T entity) {
getDao().create(entity);
}
@Override
public T update(final T entity) {
return getDao().update(entity);
}
@Override
public void delete(final T entity) {
getDao().delete(entity);
}
@Override
public void deleteById(final long entityId) {
getDao().deleteById(entityId);
}
protected abstract IOperations<T> getDao();
}
@@ -0,0 +1,20 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
public interface IOperations<T extends Serializable> {
T findOne(final long id);
List<T> findAll();
void create(final T entity);
T update(final T entity);
void delete(final T entity);
void deleteById(final long entityId);
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildDao extends IOperations<Child> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentDao extends IOperations<Parent> {
//
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.model.Child;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
@Autowired
private SessionFactory sessionFactory;
public ChildDao() {
super();
setClazz(Child.class);
}
// API
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.model.Foo;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao {
@Autowired
private SessionFactory sessionFactory;
public FooDao() {
super();
setClazz(Foo.class);
}
// API
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.model.Parent;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ParentDao extends AbstractHibernateDao<Parent> implements IParentDao {
@Autowired
private SessionFactory sessionFactory;
public ParentDao() {
super();
setClazz(Parent.class);
}
// API
}
@@ -0,0 +1,51 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToOne(mappedBy = "child")
private Parent parent;
public Child() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(final Parent parent) {
this.parent = parent;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Child [id=").append(id).append("]");
return builder.toString();
}
}
@@ -0,0 +1,83 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
@@ -0,0 +1,60 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
@JoinColumn(name = "child_fk")
private Child child;
public Parent() {
super();
}
public Parent(final Child child) {
super();
this.child = child;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public Child getChild() {
return child;
}
public void setChild(final Child child) {
this.child = child;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Parent [id=").append(id).append("]");
return builder.toString();
}
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildService extends IOperations<Child> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooService extends IOperations<Foo> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentService extends IOperations<Parent> {
//
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.model.Child;
import org.baeldung.persistence.service.IChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ChildService extends AbstractService<Child> implements IChildService {
@Autowired
private IChildDao dao;
public ChildService() {
super();
}
// API
@Override
protected IOperations<Child> getDao() {
return dao;
}
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FooService extends AbstractService<Foo> implements IFooService {
@Autowired
private IFooDao dao;
public FooService() {
super();
}
// API
@Override
protected IOperations<Foo> getDao() {
return dao;
}
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.model.Parent;
import org.baeldung.persistence.service.IParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ParentService extends AbstractService<Parent> implements IParentService {
@Autowired
private IParentDao dao;
public ParentService() {
super();
}
// API
@Override
protected IOperations<Parent> getDao() {
return dao;
}
}
@@ -0,0 +1,30 @@
package org.baeldung.properties.core;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
public class ComponentInXmlUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentInXmlUsingProperties(final String propertyValue) {
super();
System.out.println("Constructor Injection - Property Value resolted to: " + propertyValue);
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,30 @@
package org.baeldung.properties.core;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class ComponentUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentUsingProperties() {
super();
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,25 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("org.baeldung.properties.core")
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
public PropertiesWithJavaConfig() {
super();
}
// beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfigOther {
public PropertiesWithJavaConfigOther() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForProperties.xml")
@ComponentScan("org.baeldung.core")
public class PropertiesWithXmlConfig {
public PropertiesWithXmlConfig() {
super();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForPropertiesOne.xml")
@ComponentScan("org.baeldung.core")
public class PropertiesWithXmlConfigOne {
public PropertiesWithXmlConfigOne() {
super();
}
}
@@ -0,0 +1,14 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForPropertiesTwo.xml")
public class PropertiesWithXmlConfigTwo {
public PropertiesWithXmlConfigTwo() {
super();
}
}
@@ -0,0 +1,15 @@
package org.baeldung.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@ComponentScan("org.baeldung.core")
public class CoreConfig extends WebMvcConfigurerAdapter {
public CoreConfig() {
super();
}
}
@@ -0,0 +1,41 @@
package org.baeldung.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer {
/**
* Register and configure all Servlet container components necessary to power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
System.out.println("MainWebAppInitializer.onStartup()");
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("org.baeldung.spring.config");
// root.getEnvironment().setDefaultProfiles("embedded");
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Handles requests into the application
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);
final Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
}
}
}
@@ -0,0 +1,39 @@
package org.baeldung.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
public MvcConfig() {
super();
}
// API
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/sample.html");
}
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.spring.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}