Spring (4)-use annotation development, use java to configure spring

1 Use annotations

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

org.springframework:spring-aop:5.2.0.RELEASE

In the configuration file, you have to introduce a context constraint

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

Implementation of Bean
We used bean tags for bean injection before, but in actual development, we generally use annotations!
beans.xml
1 Configure annotations under which packages are scanned

<!--    开启注解支持-->
    <context:annotation-config/>
<!--指定扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.zs.pojo"/>

2 Write the class under the specified package and add the annotation
User.class

package com.zs.pojo;

import org.springframework.stereotype.Component;

@Component //组件
public class User {
    
    
    public String name = "zs";
}

3 How to inject attributes

package com.zs.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component //组件
public class User {
    
    

    @Value("zs") // 简单属性注入
    public String name;
}

4 Derivative Annotations
@Component Three Derivative Annotations
In order to better layer, Spring can use other three annotations, the functions are the same, which function is the same currently.
@Controller: web layer
@Service: service layer
@Repository: dao layer
Writing these annotations is equivalent to giving this class to Spring to manage and assemble!

5 Scope
@scope
singleton: By default, Spring will create this object in singleton mode. Close the factory and all objects will be destroyed.
Prototype: Multi-example mode. Close the factory, all objects will not be destroyed. The internal garbage collection mechanism will recycle

@Component //组件
@Scope("singleton")
public class User {
    
    
    @Value("zs") // 简单属性注入
    public String name;
}

6 Summary
Comparison of XML and annotations
1 XML can be used in any scenario, with clear structure and easy maintenance.
2 Annotations are not self-provided classes and cannot be used. Development is simple and convenient.
XML and annotation integration development: recommended best practices
1 xml management bean
2 annotations complete attribute injection

Enable annotation support

<context:annotation-config/>  

2 Configure spring using java

It does not apply to spring xml configuration at all, it is all done by java.
1 Write an entity class

package com.zs.pojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Data
@Repository //这个注解的意思,就是说明这个类被spring接管了,注册到容器中
public class User {
    
    
    @Value("zs") //属性注入值
    public String name;
}

2 Create a new config configuration package

package com.zs.config;

import com.zs.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration // 这个也会被spring容器托管,注册到容器中 因为它本身是一个@Component
// @Configuration代表这是一个配置类 就和我们之前看的beans.xml
@Import(UserConfig1.class) // 导入另一个配置类UserConfig1
public class UserConfig {
    
    
    // 注册一个bean 就相当于我们之前写的一个bean标签
    // 这个方法的名字就相当于bean标签中的id属性
    // 这个方法的返回值 就相当于bean标签中的class
    @Bean
    public User getUser() {
    
    
        return new User(); //就是返回要注入到bean的对象
    }
}

Insert picture description here
3 Test category

package com.zs.test;

import com.zs.config.UserConfig;
import com.zs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

    @org.junit.Test
    public  void test() {
    
    
        // AnnotationConfigApplicationContext 来获取容器
        ApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
        User user = (User) context.getBean("getUser");
        System.out.println(user.name);

    }
}

In addition
Insert picture description here

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112987117
Recommended