Is it possible to set the @Qualifier annotation to a private static variable in a Java class?












0















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
}

}









share|improve this question























  • 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
















0















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
}

}









share|improve this question























  • 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














0












0








0








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
}

}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 15:04









TheSilverNimbusTheSilverNimbus

124




124













  • 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

















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












1 Answer
1






active

oldest

votes


















0














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
}

}





share|improve this answer
























  • Thanks Yogesh! This is the solution I ended up going with. :)

    – TheSilverNimbus
    Jan 3 at 7:26











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
});


}
});














draft saved

draft discarded


















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









0














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
}

}





share|improve this answer
























  • Thanks Yogesh! This is the solution I ended up going with. :)

    – TheSilverNimbus
    Jan 3 at 7:26
















0














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
}

}





share|improve this answer
























  • Thanks Yogesh! This is the solution I ended up going with. :)

    – TheSilverNimbus
    Jan 3 at 7:26














0












0








0







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
}

}





share|improve this answer













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
}

}






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules