At the beginning:This is the first blog I wrote on the nuggets platform. It is my understanding and summary of the knowledge of fully annotated Bean management in the learning process of spring5 in silicon valley. My knowledge is limited, if there is any incorrect place, please actively correct friends!

1. What are annotations

1. Annotations are special tags for code in the format of @ annotation name (attribute name = attribute value, attribute name = attribute value)

2. Use annotations, which can be applied to classes, methods and properties

3. The purpose of using annotations is to simplify XML configuration files

2. Create an annotation that the Bean object can use

1.@Componet

2.@Controller

3.@Repository

4.@Service

All four annotations perform the same function and can be used to create instances of bean objects

Step 1: Create a configuration class

MyConfig is a Configuration class @configuration // Enable packet scanning, @ComponentScan(basePackages = "day01.demo1") public class MyConfig {}Copy the code

Step 2: Initial code experience

@Component() // equivalent to XML configuration file <bean id = "a"></bean> Public class A {public void hello(){system.out.println (" hello "); }}Copy the code

Let’s get the object of A via Spring and call its method hello().

@Test
public void test(){
    ApplicationContext context = 
            new AnnotationConfigApplicationContext(MyConfig.class);
    A bean = context.getBean("a", A.class);
    bean.hello();
}
Copy the code

Running results:

If the value of @Component(value = “Hello”) changes and context calls getBean(), the “A” of this code should be changed to the value of the corresponding property “Hello”, otherwise the following error will be reported:

Step 3: Code refinement

UserDao public interface UserDao {void add(); }Copy the code
UserDaoImp @repository public class UserDaoImp implements UserDao {@override public void add() {@override public void add() { System.out.println("add2()..." ); }}Copy the code
@autoWired public class UserService {@autowired private UserDao UserDao; public void fun1(){ System.out.println(userDao); userDao.add(); }}Copy the code
@Test
public void test1(){
    ApplicationContext context =
            new AnnotationConfigApplicationContext(MyConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    userService.fun1();
}
Copy the code

Get an instance of the UserService class from the IOC container and call its method fun1(). At the same time, an instance of the UserDaoImp class is created from the IOC container and the userDao object is output. Finally, the userDao calls the rewritten method add().

Running results:

Note that the UserDao cannot be annotated because the interface cannot be instantiated. Instead, annotations (any of the four annotations will do) should be added to its implementation class to create the object.

Step 4: Master the details

Create an implementation class UserDaoImpl for the UserDao interface

Public class UserDaoImpl implements UserDao {@override public void add() { System.out.println("add()..." ); }}Copy the code

Note that using @autowired raises the following exception when an interface has multiple implementation classes:

Solution 1: It can be used with @qualifier

@qualifier public class UserService {@autowired @Qualifier(value = "userDaoImpl") Value is the value in the annotation of the class. Private UserDao UserDao; public void fun1(){ System.out.println(userDao); userDao.add(); }}Copy the code

Solution 2: Use the @Resource annotation

@service public class UserService {@resource (name = "userDaoImpl1")// This annotation is equivalent to @autowired and @Qualifier annotations from javax, It is not recommended to use private UserDao UserDao. public void fun1(){ System.out.println(userDao); userDao.add(); }}Copy the code

Conclusion: Self-summary: When developing with full annotations provided by spring5, you first need to create a Configuration class. This Configuration class needs to add two annotations (@configuration, @componentscan). When you need to inject properties, note that when an interface has multiple implementation classes, It is best to use @autowired + @Qualifier annotations!