-1

class A
{
m1()
{
constant loading logic;
}
m2()
{
constant loading logic;
}
}

class B
{

@Autowired A a;

m3()
{
a.m1();
a.m2();
}

}

For clarity constant loading logic is :

@Autowired
private Environment env; //spring environment
String accessKey = env.getProperty("aws_access_key");
 String secretKey = env.getProperty("aws_secret_access_key");

I have a class A which has a @Component spring annotation.class A is an @autowired property for another class B. class A has 2 methods m1 and m2. class B calls m1 and m2.

I want to load certain common properties(constants) and use these constants in both m1 and m2.Is there an efficient way to do this instead of repeating the same constant loading logic to be written in both methods m1 and m2 ?

chungunuvavaa
  • 99
  • 1
  • 10
  • 1
    It's really unclear what the constants have to do with anything else at this point. If you could give sample code demonstrating the problem, it would be much easier to help you. – Jon Skeet Nov 29 '15 at 17:17
  • They are access keys and other keys that need to be set in both m1 and m2.. – chungunuvavaa Nov 29 '15 at 17:19
  • 1
    Can you load it in class A default (no arguments) constructor ? – TMG Nov 29 '15 at 17:20
  • It's still far less clear written as a description than it would be in sample code. We still don't know what "constant loading logic" is being repeated – Jon Skeet Nov 29 '15 at 17:20
  • updated the question..:) – chungunuvavaa Nov 29 '15 at 17:23
  • Oh, you didn't say that constant lading logic also requires an autowired bean. Then forget my previous comment and see this answer: http://stackoverflow.com/a/6336013/4358405 (the other answer there should work too) – TMG Nov 29 '15 at 17:29
  • @TMG can you please look at the update and edit it with your answer.I am not understanding your link... – chungunuvavaa Nov 29 '15 at 17:35

1 Answers1

0
@Component
class A{
  String accessKey;
  String secretKey;

  @Autowired
  public A(Environment env){
    accessKey = env.getProperty("aws_access_key");
    secretKey = env.getProperty("aws_secret_access_key");
  }
  public void m1(){
    // whatever logic here
  }
  public void m2(){
    // whatever logic here
  }
}
TMG
  • 2,620
  • 1
  • 17
  • 40
  • so from class B..i always have to call the class A constructor (by creating an object everytime) before invoking m1 and m2 ? – chungunuvavaa Nov 29 '15 at 17:43
  • nope, you can just autowire A in B and Spring should be able to call A's constructor – TMG Nov 29 '15 at 17:45
  • Throwing exception:error creating bean with name A...could not find default constructor...I just autowired A in B like this @Autowired public A a; .Default constructor is not able to be autowired. – chungunuvavaa Nov 29 '15 at 18:03
  • Did you annotated A's constructor with @Autowired, like in my example above? Just to double check I've copy pasted A class from my answer and autowired it in another bean. Works fine for me :-) – TMG Nov 29 '15 at 18:12
  • copy paste your autowired field in B.Is it public or private ? – chungunuvavaa Nov 30 '15 at 02:33