Is it possible to set the @Qualifier annotation to a private static variable in a Java class?
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:
- The Java EE 6 Tutorial: Using 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
}
}
java spring annotations
add a comment |
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:
- The Java EE 6 Tutorial: Using 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
}
}
java spring annotations
You have to use@Autowired
to injectPlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for thePlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......
– Ken Chan
Jan 1 at 15:20
add a comment |
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:
- The Java EE 6 Tutorial: Using 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
}
}
java spring annotations
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:
- The Java EE 6 Tutorial: Using 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
}
}
java spring annotations
java spring annotations
asked Jan 1 at 15:04
TheSilverNimbusTheSilverNimbus
124
124
You have to use@Autowired
to injectPlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for thePlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......
– Ken Chan
Jan 1 at 15:20
add a comment |
You have to use@Autowired
to injectPlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for thePlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......
– Ken Chan
Jan 1 at 15:20
You have to use
@Autowired
to inject PlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for the PlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......– Ken Chan
Jan 1 at 15:20
You have to use
@Autowired
to inject PlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for the PlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......– Ken Chan
Jan 1 at 15:20
add a comment |
1 Answer
1
active
oldest
votes
You can implement ApplicationContextAware
interface to get context object and using this context object you can get the bean even in static context.
public class ApplicationBeansProvider implments ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
and then in your code you can do something like
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE;
private static PlatformTransactionManager manager = ApplicationBeansProvider.getBean("transactionManager");
static {
DataSource source =
( ( JpaTransactionManager ) manager ).getDataSource();
TEMPLATE = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996509%2fis-it-possible-to-set-the-qualifier-annotation-to-a-private-static-variable-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can implement ApplicationContextAware
interface to get context object and using this context object you can get the bean even in static context.
public class ApplicationBeansProvider implments ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
and then in your code you can do something like
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE;
private static PlatformTransactionManager manager = ApplicationBeansProvider.getBean("transactionManager");
static {
DataSource source =
( ( JpaTransactionManager ) manager ).getDataSource();
TEMPLATE = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
add a comment |
You can implement ApplicationContextAware
interface to get context object and using this context object you can get the bean even in static context.
public class ApplicationBeansProvider implments ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
and then in your code you can do something like
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE;
private static PlatformTransactionManager manager = ApplicationBeansProvider.getBean("transactionManager");
static {
DataSource source =
( ( JpaTransactionManager ) manager ).getDataSource();
TEMPLATE = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
add a comment |
You can implement ApplicationContextAware
interface to get context object and using this context object you can get the bean even in static context.
public class ApplicationBeansProvider implments ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
and then in your code you can do something like
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE;
private static PlatformTransactionManager manager = ApplicationBeansProvider.getBean("transactionManager");
static {
DataSource source =
( ( JpaTransactionManager ) manager ).getDataSource();
TEMPLATE = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}
You can implement ApplicationContextAware
interface to get context object and using this context object you can get the bean even in static context.
public class ApplicationBeansProvider implments ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
and then in your code you can do something like
private final class Repository {
private Repository() {}
private static final JdbcTemplate TEMPLATE;
private static PlatformTransactionManager manager = ApplicationBeansProvider.getBean("transactionManager");
static {
DataSource source =
( ( JpaTransactionManager ) manager ).getDataSource();
TEMPLATE = new JdbcTemplate( source );
}
public Map<String, Object> fetchData() {
return TEMPLATE1.queryForList( "..." ); // TODO: something
}
}
answered Jan 1 at 15:35
Yogesh BadkeYogesh Badke
1,95611116
1,95611116
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
add a comment |
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
Thanks Yogesh! This is the solution I ended up going with. :)
– TheSilverNimbus
Jan 3 at 7:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996509%2fis-it-possible-to-set-the-qualifier-annotation-to-a-private-static-variable-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have to use
@Autowired
to injectPlatformTransactionManager
,but it does not work on the static fields . So do your requirement is that you must use static field for thePlatformTransactionManager
? If not , I suggest you to consider changing it to use non-static field for the dependencies which normally are regards as a much better practise than using static field ......– Ken Chan
Jan 1 at 15:20