1

These two codes should do exactly the same thing, but the first one works and the second one doesnt work. Can anyone review the code and give the details about why the code failed during second approach.

The first code :

@Component public class AdminSqlUtil implements SqlUtil { @Autowired private ApplicationContext context; DataSource dataSource =(DataSource) context.getBean("adminDataSource"); public void runSqlFile(String SQLFileName) { Resource resource = context.getResource(SQLFileName); EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8")); try { ScriptUtils.executeSqlScript(dataSource.getConnection(), encodedResource); } catch (SQLException ex) { throw new RuntimeException(ex); } } 

The second code :

@Component public class AdminSqlUtil implements SqlUtil { @Autowired private ApplicationContext context; public void runSqlFile(String SQLFileName) { Resource resource = context.getResource(SQLFileName); EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8")); try { ScriptUtils.executeSqlScript((DataSource)context.getBean("adminDataSource").getConnection(), encodedResource); } catch (SQLException ex) { throw new RuntimeException(ex); } } 
7
  • Please add why it doesn't work as the second. Additionally format your question would help people determine why Commented Oct 13, 2020 at 0:02
  • @DarrenForsythe it seems so obvious. Commented Oct 13, 2020 at 0:07
  • Perhaps, but Stackoverflow has a bad enough rep for being elitist and un-welcoming. Commented Oct 13, 2020 at 0:11
  • @DarrenForsythe you are right. When I saw, someone downvoted this question, I asked myself what’s the point on downvoting someone with rep of 1. I believe it is actually a good question so upvoted it and make SO more welcoming which is not. Thanks for the hint by the way. Commented Oct 13, 2020 at 10:58
  • Hello everyone. No one is born an expert,I am a beginner in this field, in addition to that it is not my original specialty. I am originally a mathematician ,I do this just for pleasure،In any case, thank you for those who helped and have a good spirit @Iman ،And thanks also for those who mocked the question. Commented Oct 13, 2020 at 11:20

1 Answer 1

1

The first one has a private scope and the framework can not access it. You could have add @inject before your private scope variable so the framework can initialize it. However the best practice is to define a public dependency setter for that to work.

The second one on the other hand initiates the value at the start, which is not a dependency injection by the way. I am not talking about good and bad practice. It is wrong. We don’t initialize a variable which is suppose to be initialized by the framework.

So lets go with the first one, Try to add a setter for it.

Take a look at this link.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you for your answer .im beginner i the Spring framework , they are two data source a want to use the "adminDaraSource" one.
@omar Your welcome, once you know how it works, suddenly you become an expert in Spring. If you just started spring, I strongly recommend to read their blog. Like this one which explains memory performance in spring spring.io/blog/2015/12/10/spring-boot-memory-performance

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.