BeanFactory vs ApplicationContext

The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext?. They have a table with a comparison, I’ll post a snippet: Bean Factory Bean instantiation/wiring Application Context Bean instantiation/wiring Automatic BeanPostProcessor registration Automatic BeanFactoryPostProcessor registration Convenient MessageSource access (for i18n) ApplicationEvent publication So if you need any of the points presented on the Application … Read more

How to inject in @FacesValidator with @EJB, @PersistenceContext, @Inject, @Autowired

JSF 2.3+ If you’re already on JSF 2.3 or newer, and want to inject CDI-supported artifacts via e.g. @EJB, @PersistenceContext or @Inject, then simply add managed=true to the @FacesValidator annotation to make it CDI-managed. @FacesValidator(value=”emailExistValidator”, managed=true) JSF 2.2- If you’re not on JSF 2.3 or newer yet, then you basically need to make it a … Read more

Spring Boot, Spring Data JPA with multiple DataSources

There is another way to have multiple dataSources by using @EnableAutoConfiguration and application.properties. Basically put multiple dataSource configuration info on application.properties and generate default setup (dataSource and entityManagerFactory) automatically for first dataSource by @EnableAutoConfiguration. But for next dataSource, create dataSource, entityManagerFactory and transactionManager all manually by the info from property file. Below is my example … Read more

how to read System environment variable in Spring applicationContext

You are close :o) Spring 3.0 adds Spring Expression Language. You can use <util:properties id=”dbProperties” location=”classpath:config_#{systemProperties[‘env’]}/db.properties” /> Combined with java … -Denv=QA should solve your problem. Note also a comment by @yiling: In order to access system environment variable, that is OS level variables as amoe commented, we can simply use “systemEnvironment” instead of “systemProperties” … Read more

inject bean reference into a Quartz job in Spring?

You can use this SpringBeanJobFactory to automatically autowire quartz objects using spring: import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.scheduling.quartz.SpringBeanJobFactory; public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware { private transient AutowireCapableBeanFactory beanFactory; @Override public void setApplicationContext(final ApplicationContext context) { beanFactory = context.getAutowireCapableBeanFactory(); } @Override protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { … Read more

Type safety: Unchecked cast

The problem is that a cast is a runtime check – but due to type erasure, at runtime there’s actually no difference between a HashMap<String,String> and HashMap<Foo,Bar> for any other Foo and Bar. Use @SuppressWarnings(“unchecked”) and hold your nose. Oh, and campaign for reified generics in Java 🙂

setMaxResults for Spring-Data-JPA annotation?

As of Spring Data JPA 1.7.0 (Evans release train). You can use the newly introduced Top and First keywords that allow you to define query methods like this: findTop10ByLastnameOrderByFirstnameAsc(String lastname); Spring Data will automatically limit the results to the number you defined (defaulting to 1 if omitted). Note that the ordering of the results becomes … Read more

Spring Boot JPA – configuring auto reconnect

I assume that boot is configuring the DataSource for you. In this case, and since you are using MySQL, you can add the following to your application.properties up to 1.3 spring.datasource.testOnBorrow=true spring.datasource.validationQuery=SELECT 1 As djxak noted in the comment, 1.4+ defines specific namespaces for the four connections pools Spring Boot supports: tomcat, hikari, dbcp, dbcp2 … Read more

Error creating bean with name ‘entityManagerFactory’ defined in class path resource : Invocation of init method failed

I would start by adding the following dependency: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.4.Final</version> </dependency> and <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.3.Final</version> </dependency> UPDATE: Or simply add the following dependency. <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency>