27 Matching Annotations
  1. Feb 2023
    1. Address it straightforwardly In the end, I talked to this engineer and explained what the correct process would have been. It went like, “Your implementation was correct; the designs weren’t. It's fine to build it differently and to test your idea. I just wish you had gone to your product manager and your architect the next morning, and then we could have gone to the client together, to try to sell them on your idea.” Instead, it had turned into a catastrophe. We made it through, but we needed to do some cultural cleanup.

      instead, it had turned into a catastrophe ... whyyyy

    2. We've found that we often have a good idea of our client’s goals, and sometimes they ask us to do things we don’t think are aligned with their goals. This is why we encourage our people to challenge them.

      by create a document

    1. Spring tries to make it easy for you to register and automatically find properties across all these different sources, through its environment abstraction.

      ```java import org.springframework.core.env.Environment; public class MyApplication {

      public static void main(String[] args) {
             ApplicationContext ctx = new AnnotationConfigApplicationContext(someConfigClass);
             Environment env = ctx.getEnvironment(); // (1)
             String databaseUrl = env.getProperty("database.url"); // (2)
             boolean containsPassword = env.containsProperty("database.password");
             // etc
      }
      

      } ```

      instead of specificlt get resources (or config) from each file ... Spring can do it with single line

    2. Spring @Bean method can return you something that looks and feels like (in your case) a UserService, but actually isn’t. It can return you a proxy.

      So that they can inject other logic defined in other annoation

    3. What are Field and Setter Injection? Simply put, Spring does not have to go through a constructor to inject dependencies. It can also directly inject fields.

      ```java import javax.sql.DataSource; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired;

      @Component public class UserDao {

      @Autowired
      private DataSource dataSource;
      

      } ```

    4. With newer Spring versions, Spring is actually smart enough to inject these dependencies without an explicit @Autowired annotation in the constructor. So this would also work.

      ```java @Component public class UserDao {

      private DataSource dataSource;
      
      public UserDao(DataSource dataSource) {
          this.dataSource = dataSource;
      }
      

      } ```

    5. How does Spring know that it should take the DataSource that you specified as a @Bean method and then create new UserDAOs with that specific DataSource? Easy, with another marker annotation: @Autowired. Hence, your final code will look like this.

      ```java import javax.sql.DataSource; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired;

      @Component public class UserDao {

      private DataSource dataSource;
      
      public UserDao(@Autowired DataSource dataSource) {
          this.dataSource = dataSource;
      }
      

      } ```

    6. What this @ComponentScan annotation does, is tell Spring: Have a look at all Java classes in the same package as the context configuration if they look like a Spring Bean!

      ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;

      @Configuration @ComponentScan // (1) public class MyApplicationContextConfiguration {

      @Bean
      public DataSource dataSource() {
          MysqlDataSource dataSource = new MysqlDataSource();
          dataSource.setUser("root");
          dataSource.setPassword("s3cr3t");
          dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
          return dataSource;
      }
      
      // (2)
      
      // no more UserDao @Bean method!
      

      } ```

    7. How many instances of our DAOs should Spring create? To answer that question, you need to learn about bean scopes.
      • Singleton: All DAOs share the same DataSource.
      • Prototype: All DAOs get their own DataSource.
      • Session: new DataSource per HttpRequest or HttpSession or WebSocket
    8. the methods inside

      the getBean method from

      java ApplicationContext ctx = new AnnotationConfigApplicationContext(MyApplicationContextConfiguration.class); UserDao userDao = ctx.getBean(UserDao.class);

    9. But imagine you now want to run your application. Whereas you could call "new UserService()" previously, you’ll now have to make sure to call new UserDao(dataSource).

      Again, we introduce another problems. The application class need to know the logic of how UserDao and DataSource working

    10. The UserDAO actively has to know where to get its dependencies from, it has to call the application class

      We called it tight-coupled. It hard to modify and make it more general.

    11. It would be much nicer if we opened just one DataSource and re-used it, instead of opening and closing tons of them.

      And when we have duplicated code for such operation (database connection) this will introduce new problems ... too many database connection

    12. MysqlDataSource dataSource = new MysqlDataSource(); // (1) dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase"); dataSource.setUser("root"); dataSource.setPassword("s3cr3t");

      He create DataSource instance here in findById method, which will introduce more problem later on

    13. What is a dependency?

      The idea of dependency is when we want to use other class feature that's it

      from the example class UserDao is just a DAO class (data access object) and it needs feature of the DataSource class to prepare SQL statement. So, it needs to do something to get that object which is declare new DataSource()

      ```java public class UserDao {

      public User findById(Integer id) throws SQLException {
          try (Connection connection = dataSource.getConnection()) { // (1)
                 PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
                 // use the connection etc.
          }
      }
      

      } ```

    1. There are three main ways to inject your dependencies into your class. Constructor, Setter (Method) and Field injection

      Constructor

      ```java

      private DependencyA dependencyA; private DependencyB dependencyB; private DependencyC dependencyC;

      @Autowired public DI(DependencyA dependencyA, DependencyB dependencyB, DependencyC dependencyC) { this.dependencyA = dependencyA; this.dependencyB = dependencyB; this.dependencyC = dependencyC; } ```

      Setter

      ```java

      private DependencyA dependencyA; private DependencyB dependencyB; private DependencyC dependencyC;

      @Autowired public void setDependencyA(DependencyA dependencyA) { this.dependencyA = dependencyA; }

      @Autowired public void setDependencyB(DependencyB dependencyB) { this.dependencyB = dependencyB; }

      @Autowired public void setDependencyC(DependencyC dependencyC) { this.dependencyC = dependencyC; } ```

      Field injection

      ```java

      @Autowired private DependencyA dependencyA;

      @Autowired private DependencyB dependencyB;

      @Autowired private DependencyC dependencyC; ```

  2. Dec 2021
  3. Jul 2021
  4. Jun 2021
    1. Implementation details are things which users of your code will not typically use, see, or even know about.

      It's more like your test focus on how code are implement, but actually you need to focus on how you application should interact with user

      because code coverage doesn't mean your software doesn't have bug

      instead feature coverage, can guarantee that :D

    2. But the problem is that there was no test to verify that the button was wired up to setOpenIndex correctly

      yes, no test on button click event

    3. which some of those of you experienced with enzyme you might already be expecting

      I'm begin to test when we already have testing-library, so I didn't have that experience :D