spring05 - development with annotations

8. Using annotation development

After spring 4, if you want to use annotation form, you must introduce aop package

To use annotations, you need to import context constraints and add annotation support!

<beans xmlns="http://www.springframework.org/schema/beans"



We used to use bean tags for bean injection, but in actual development, we usually use annotations!

1. Configure which packages to scan for annotations

<!--Specify annotation scan package-->
<context:component-scan base-package="com.sen.pojo"/>

2. Write classes under the specified package and add comments

// Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file
public class User {
   public String name = "Qin Jiang";

3. Testing

public void test(){
   ApplicationContext applicationContext =
       new ClassPathXmlApplicationContext("beans.xml");
   User user = (User) applicationContext.getBean("user");

8.2. How to inject attributes

Using annotation injection attributes

1. You can add @ value("value") to the direct name without providing a set method

// Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file
public class User {
   @Value("Qin Jiang")
   // Equivalent to < property name = "name" value = "Qinjiang" / > in the configuration file
   public String name;

2. If a set method is provided, add @ value("value") to the set method;

public class User {

   public String name;

   @Value("Qin Jiang")
   public void setName(String name) {
       this.name = name;

8.3 notes derived

These annotations replace the configuration steps in the configuration file! More convenient and fast!

@Three derived annotations of Component

For better layering, Spring can use the other three annotations with the same functions. At present, which function is used is the same.

  • @Controller: web layer
  • @Service: service layer
  • @Repository: dao layer

Writing these annotations is equivalent to giving this class to the Spring management assembly!

8.4 automatic assembly notes

  • @Autowired auto assembly type and name
    • If Autowired cannot uniquely auto assemble attributes, you need to specify a unique attribute through @Qualifier (value= "* *")
  • @The Nullable field is marked with this annotation, indicating that this field can be null;
  • @Resource is automatically assembled by name and type

8.5 scope


  • Singleton: by default, Spring creates this object in singleton mode. Close the factory and all objects will be destroyed.
  • prototype: multi instance mode. Close the factory and all objects will not be destroyed. The internal garbage collection mechanism will recycle
public class User {
   public String name;

8.6 summary

XML and annotation comparison

  • XML can be applied to any scenario, with clear structure and convenient maintenance
  • Annotations are not self provided classes and cannot be used. Development is simple and convenient

xml and annotation integrated development: Recommended Best Practices

  • xml management Bean
  • Annotation complete attribute injection
  • In the process of using, you can not scan. The scanning is for the annotation on the class


  • Make annotation driven registration to make annotations effective
  • It is used to activate the annotations on the bean s that have been registered in the spring container, that is, to register with spring
  • If you do not scan the package, you need to configure the bean manually
  • If it is driven without annotation, the injected value is null!

9. Configuring Spring in java

JavaConfig was originally a sub project of Spring. It provides Bean definition information through Java classes. In the version of Spring 4, JavaConfig has officially become the core function of Spring 4.


1. Write an entity class, Dog

@Component  //Mark this class as a component of Spring and put it in the container!
public class Dog {
   public String name = "dog";

2. Create a new config configuration package and write a MyConfig configuration class

@Configuration  //Represents that this is a configuration class
public class MyConfig {

   @Bean //Register a bean through the method. The return value here is the bean type, and the method name is the bean id!
   public Dog dog(){
       return new Dog();


3. Testing

public void test2(){
   ApplicationContext applicationContext =
           new AnnotationConfigApplicationContext(MyConfig.class);
   Dog dog = (Dog) applicationContext.getBean("dog");

4. Output results successfully!

Use @ Configuration to declare Configuration classes

Method 1: define a method in the configuration class and declare it with @ Bean annotation

Method 2: Use @ Component annotation on User class and declare @ componentscan ("path of User class") on configuration class. This will automatically scan @ component and generate Bean

Posted by iluv8250 on Sat, 16 Apr 2022 23:55:46 +0930