I'm currently using JDBC Templates to fetch data from a database. For this, I've created a "static" Repository class (i.e. a class marked "final" and with a single private constructor). In it, I'm trying to set the values of two private static PlatformTransactionManager
class variables.
However, my IntelliJ tells me that the value of the class variables are always null and I have no idea how to solve this issue.
I was hoping on just using them as local variables in the static constructor, since what I really want are the JdbcTemplate constants that I'll prepare using the PTMs. Since, I don't know how to do that, I tried making them into private static final
fields. But IntelliJ didn't allow that either.
To try to solve this, I have looked at these threads:
- Passing variables to @Qualifier annotation in Spring
- How to make spring inject value into a static field
. . . as well as this note on Qualifiers:
Important Notes:
- The project I'm working on does not have XML configuration files. There are [*]Config files in the project that handle the configurations.
- One more thing to note is that I don't quite understand annotations in general (be they in Java, C#, etc.). That is, I know the basic idea behind them, but I have no idea how they really work . . . and I don't remember much of the Spring framework anymore (since I've been working on Core Java and C#.NET for quite some time now). So . . . I'd appreciate any help to solve this issue.
The following is a sample of what my source code looks like:
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE1;
private static final JdbcTemplate TEMPLATE2;
@Qualifier( "transactionManager1" )
private static PlatformTransactionManager manager1;
@Qualifier( "transactionManager2" )
private static PlatformTransactionManager manager2;
static {
// NOTE: For this one, IntelliJ shows me an error stating, "Value 'manager1'
// is always 'null'."
DataSource source =
( ( JpaTransactionManager ) manager1 ).getDataSource();
TEMPLATE1 = new JdbcTemplate( source );
// NOTE: Here, there is no error ... at least, IntelliJ isn't showing any.
source = ( ( JpaTransactionManager ) manager2 ).getDataSource();
TEMPLATE2 = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}