JndiObjectFacotryBean method is ignoring #DependsOn with latest spring version - spring

with springboot 2.2.4 version JndiObjectFactoryBean gets called after JndiTemplate bean is created , but after upgarding version to 2.4.5 JndiObjectFacotryBean method is ignoring #DependsOn and its bean creating before JndiTemplate.
#Bean("jndiTemplate")
public JndiTemplate getJndiTemplate() {
jndiTemplate = new JndiTemplate();
Properties properties = new Properties();
properties.setProperty(initialContextName,
initialContextValue);
properties.setProperty(providerUrlName(),
providerUrlValue());
jndiTemplate.setEnvironment(properties);
return jndiTemplate;
}
#Bean("jndiObjectFactoryBean")
#DependsOn({ "jndiTemplate" })
public JndiObjectFactoryBean getJndiObjectFactoryBean () {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate); //NULL with springboot 2.4.5 and not null with 2.2.4 version
return jndiObjectFactoryBean;
}

According to spring-boot 2.4.5 release notes:
https://github.com/spring-projects/spring-boot/releases/tag/v2.4.5
you have upgraded Spring Framework to 5.3.6 version.
Try to use:
#Bean(name= "jndiObjectFactoryBean")
#DependsOn(value= { "jndiTemplate" })
You might also try to force the use of spring framework in spring boot 2.2.4 setting the following property in your pom, but I cannot say if you might have dependency problems in your application. At least you need a deep non regression test.
<properties>
<!-- Other dependencies -->
<spring-framework.version>5.2.3.RELEASE</spring-framework.version>
<!-- Other dependencies -->
</properties>

Related

Spring boot 3 and JMS configuration for Amazon SQS

The latest spring boot (i.e. 3.0.1) uses Spring JMS 6.0.3. And the latest Spring JMS that comes with the latest spring boot for ConnectionFactory uses jakarta.jms.ConnectionFactory, while the older JMS was using javax.jms.ConnectionFactory. This was my bean setup and configuration before:
#Configuration
#EnableJms
class JmsConfiguration {
#Autowired
private lateinit var sqsClient: SqsClient
#Bean
fun sqsConnectionFactory(): SQSConnectionFactory =
SQSConnectionFactory(ProviderConfiguration(), sqsClient)
#Bean
fun jmsListenerContainerFactory(): DefaultJmsListenerContainerFactory {
val factory = DefaultJmsListenerContainerFactory()
factory.setConnectionFactory(sqsConnectionFactory())
factory.setDestinationResolver(DynamicDestinationResolver())
factory.setSessionAcknowledgeMode(CLIENT_ACKNOWLEDGE)
return factory
}
#Bean
fun jmsTemplate(): JmsTemplate = JmsTemplate(sqsConnectionFactory())
}
However with the latest Spring boot, I cannot use SQSConnectionFactory, because it hasn't implemented its code based on jakarta rather javax.
Here are my SQS dependencies used:
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sqs</artifactId>
<version>2.19.19</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-messaging-lib</artifactId>
<version>2.0.3</version>
</dependency>
So now I cannot set the connection factory.setConnectionFactory(sqsConnectionFactory()), because it complains sqsConnectionFactory() is not type of jakarta ConnectionFactory. Is there any solution?

SEO user friendly url using Pretty faces and spring boot

I have followed this to configure spring boot work with JSF and the configuration is ok however I need to get rid of .xhtml suffix in all my jsf using pretty faces. I have so far managed to configure PrettyFaces RewriteFilter in my spring boot and added the URLMapping in my managed bean but it doesnt seem to work am getting 'Whitelabel Error Page'. Here is my login managed bean
#Scope(value = "session")
#Component(value = "loginMgr")
#URLMapping(id = "login",
pattern = "/login",
viewId = "/my_context/login.xhtml")
public class LoginManager {
}
and my pretty faces configuration bean
#Bean
public FilterRegistrationBean prettyFilter() {
System.out.println("pretty filter called");
RewriteFilter filter=new RewriteFilter();
FilterRegistrationBean prettyFilter = new FilterRegistrationBean(filter);
prettyFilter.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST,
DispatcherType.ASYNC, DispatcherType.ERROR);
prettyFilter.addUrlPatterns("/*");
return prettyFilter;
}
and this is my sping boot application.properties
spring.datasource.url= jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=password
#spring.jpa.show-sql=true
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
server.context-path=/my_context
spring.security.enabled=false
management.security.enabled=false
security.basic.enabled=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrateg
I am using jsf 2.2 and primefaces 5.3 and below are the pretty faces maven dependency
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-integration-faces</artifactId>
<version>3.4.1.Final</version>
</dependency>
and this
is the error am getting
If you would like to get rid of the .xhtml suffix and use MyFaces 2.3, you can simply set:
org.apache.myfaces.AUTOMATIC_EXTENSIONLESS_MAPPING to true.
So you dont need PrettyFaces or Rewrite.
NOTE:
it might throw a exception that #getServletRegistrations can't be called from a Listener not configured in web.xml or web-fragment.xml.
In this case you need to add org.apache.myfaces.webapp.StartupServletContextListener to your web.xml or web-fragment.xml..

Spring property placeholders in application-context.xml inside dependency jar

For my spring boot application I use annotation based configuration and a WebApplicationInitalizer.
One of my dependencies provides a spring configuration in an xml, included in the jar. I use #ImportResource to load the context xml. This seems to work, except for the fact that inside this xml, there are property placeholders, for example ${poolsize:10}
Apparently, spring does not automatically replace these placeholders (I get a NumberFormatException). Is there some extra configuration I need to add?
Our startup class:
public class Application implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// config
rootContext.register(JmsConfiguration.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
}
}
And the configuration class (we use spring-jms):
#Configuration
#EnableJms
#ComponentScan(basePackages = { "..." })
public class JmsConfiguration implements JmsListenerConfigurer {
// config for jms listener and jaxb, nothing to do with property handling
}
Perhaps I'm mistaken in thinking that using a WebapplicationInitializer is how to use spring boot. Perhaps we don't even need spring boot? The only spring boot related dependency we use is:
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring dependencies we use:
org.springframework:spring-context:jar:4.3.8.RELEASE:compile
org.springframework:spring-jms:jar:4.3.8.RELEASE:compile
org.springframework:spring-oxm:jar:4.3.8.RELEASE:compile
org.springframework:spring-context:jar:4.3.8.RELEASE:compile
org.springframework:spring-beans:jar:4.3.8.RELEASE:compile
I figured it out thanks #M. Deinum
We don't need spring boot to use the WebApplicationInitializer, but (at least without spring boot) we have to declare our own PropertySourcesPlaceholderConfigurer:
#Configuration
#ImportResource(locations = {"classpath*:/library-context.xml"})
public class MyConfiguration {
#Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
NB. This works out of the box in spring boot thanks to #EnableAutoConfiguration and #PropertyPlaceholderAutoConfiguration which contains the exact same PropertySourcesPlaceholderConfigurer bean

Spring Boot soap + hibernate configuration

I have already a soap ws working with spring boot.
My doubt now its how to add in WebAppInitializer file the injection of Hibernate.
My file its like this now
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(ctx);
servlet.setTransformWsdlLocations(true);
Dynamic dynamic = servletContext.addServlet("dispatcher",servlet);
dynamic.addMapping("/soapws/*");
dynamic.setLoadOnStartup(1);
}
}
How to add the hibernate configuration/injection?
You can use the spring boot starter jpa. Include the below dependency in your pom.xml (if using maven).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
And then, specify hibernate options in application.properties. For example,
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
etc
Spring Boot tries to guess the location of your #Entity definitions, based on the #EnableAutoConfiguration it finds. To get more control, you can use the #EntityScan annotation
Refer to this for more details :
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html

Weblogic jndi NameNotFoundException occur with java config

I been searching again for this issue where I cannot locate the jndi database by using java config. Before this I use xml and its work perfectly but in java config it cause an issue;
Xml code:
<!-- Jndi database connection -->
<jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}"
resource-ref="true" />
<beans:bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" >
<beans:property name="dataSource" ref="dbDataSource"></beans:property>
</beans:bean>
Java config now:
#Bean(name = "dbDataSource")
public DataSource dataSource(#Value("${db.jndi}") String jndiName)
{
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource(jndiName);
}
#Bean
public JdbcTemplate jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}
Properties file:
db.jndi=jndi/myData
JNDI name in weblogic:
jndi/myData
After change to java config, sometimes the system can read the database but rarely occur, until I clean and restart my computer then it can find the database, but usually its always trigger:
javax.naming.NameNotFoundException: Unable to resolve 'jndi.myData'. Resolved 'jndi'; remaining name 'myData'
Why the application cannot find the database correctly?
Thanks!!!
I've had the same issue. If you're using 4.x version of spring that's probably the cause.
You should also check Weblogic's JNDI Tree. If your data source disapears from the tree after rebuilding the project, that's another symptom
If that's the case, what's happening is:
Your Datasource implements Closeable (and therefore AutoCloseable) and the context will always invoke the shutdown method regardless of your Bean definition
as seen here : SPR-12551: Document how to prevent a JNDI DataSource retrieved using JavaConfig to be removed on shutdown of the context
It's been marked as a documentation issue as this is the "expected" behaviour:
This issue was solely about documentation since we decided not to implement anything at the framework level
the solution, is to define the destroy method of the bean as empty, such as:
#Bean(name = "dbDataSource", destroyMethod="")
public DataSource dataSource(#Value("${db.jndi}") String jndiName)
{
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource(jndiName);
}
#Bean
public JdbcTemplate jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}
This is described in this issue (SPR-13022:Destroy callback cannot be disabled for AutoCloseable beans) .
PS: By the way, it seems like on early 4.x version of spring you couldn't override this behaviour by assingning destroyMethod. It apears that this bug was fixed on version 4.2 RC1.
I've had the same issue and I solved problem. I used to jndi datasource on weblogic. After I restart application, I notice my jndi datasource remove from Weblogic's JNDI Tree. Xml configuration works successfuly but java configuration don't work.
My old spring version: 4.1.6.RELEASE Upgrade to 4.3.9.RELEASE
Xml configuration like this;
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>${db-jndi.name}</value>
</property>
</bean>
Java configuration like this;
#Bean(name = "dataSource")
public DataSource dataSource() throws IllegalArgumentException, NamingException
{
JndiTemplate jndiTemplate = new JndiTemplate();
DataSource dataSource = (DataSource) jndiTemplate.lookup(env.getProperty("db-jndi.name"));
logger.info("DataSource initialized in jndi ");
return dataSource;
}
Then i changed
#Bean(name = "dataSource")
to
#Bean(name = "dataSource", destroyMethod = "")
And it's works successfuly.
It looks like your datasource hasn't been deployed. You should look for JNDI tree for the server you tried to deploy datasource. (https://docs.oracle.com/cd/E12839_01/apirefs.1111/e13952/taskhelp/jndi/ViewObjectsInTheJNDITree.html) If you don't see "jndi.myData" on JNDI tree, you can assume that your datasource haven't been deployed. So you can go to your datasource monitoring tab and test the datasource. (https://docs.oracle.com/cd/E17904_01/apirefs.1111/e13952/taskhelp/jdbc/jdbc_datasources/TestDataSources.html)

Resources