Summary of important knowledge points of Spring framework (two)

1. Common notes

Insert picture description here
Detailed code

// 默认当前类名就是ID名称,首字母小写
@Component(value = "c")
// @Controller
// @Service(value = "c")
// @Repository(value = "c")
// @Scope(value = "singleton")     // 默认值,单例的
// @Scope(value = "prototype")         // 多例的
public class Car {
    // 注解注入值,属性set方法是可以省略不写的。
    // 只有一个属性,属性的名称是value,value是可以省略不写的
    @Value("大奔2")
    private String cname;
    @Value(value = "400000")
    private Double money;
    // 也不用提供set方法
    // 按类型自动装配的注解,和id名称没有关系
    @Autowired
    // 按id的名称注入,Qualifier不能单独使用,需要Autowired一起使用。
    // @Qualifier(value = "person")
    // @Resource Java提供的注解,按名称注入对象,属性名称是name
    // @Resource(name = "person")
    private Person person;
    /**
     * Car对象创建完成后,调用init方法进行初始化操作
     */
    @PostConstruct
    public void init(){
        System.out.println("操作...");
    }

Two, pure annotation

The pure annotation method is the main method of microservice architecture development, so it is also very important. The purpose of pure annotation is to replace all configuration files. But you need to write configuration classes.
(1) Writing entity classes

@Component
public class Order {
    @Value("北京")
    private String address;
    @Override
    public String toString() {
        return "Order{" +
                "address='" + address + '\'' +
                '}';
    }}

(2) Write the configuration class and replace the applicationContext.xml configuration file

// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "cn.tx.demo4")
public class SpringConfig {

}

(3) Test method

public class Demo4 {
    /**
     * 编写程序,需要加载配置类
     */
    @Test
    public void run1(){
        // 创建工厂,加载配置类
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        // 获取到对象
        Order order = (Order) ac.getBean("order");
        System.out.println(order);
    }
}

(4) Summary of common notes

@Configuration declaration is a configuration class

@ComponentScan scans the specific package structure

@Import annotation Spring configuration files can be divided into multiple configurations, and multiple configuration classes are written. Used to import other configuration classes

@Bean annotation can only be written on the method, indicating that an object is created using this method, and the object is created and saved in the IOC container

// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "cn.tx.demo4")
// @ComponentScan(value = {"cn.tx.demo4","cn.tx.demo3"})
// 引入新的配置类
@Import(value = {SpringConfig2.class})
public class SpringConfig {
    / * 创建连接池对象,返回对象,把该方法创建后的对象存入到连接池中,使用@Bean注解解决
         <!--配置连接池对象-->
         <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
             <property name="driverClassName" value="com.mysql.jdbc.Driver" />
             <property name="url" value="jdbc:mysql:///spring_db" />
             <property name="username" value="root" />
             <property name="password" value="root" />
         </bean>
     *
     * @return
     */
    @Bean(name="dataSource")
    public DataSource createDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }}

Two, Spring framework integrates JUnit unit testing

Every time you perform a unit test, you need to write code to create a factory and load configuration files, which is cumbersome. Spring provides the technology of integrating Junit unit testing, which can simplify test development. Let's learn about it below.

(1) First of all, since the Junit unit test is to be performed, the jar package is required, and the coordinate dependency of the spring-test is imported.

(2) Configuration file + annotation method

① Write classes and methods, and hand them to the IOC container for management

public class User {
    public void sayHello(){
        System.out.println("Hello....");
    }}

②Write the configuration file applicationContext.xml

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

    <!--整合单元测试-->
    <bean id="user" class="cn.tx.demo5.User"/>
</beans>

③Write test code

@RunWith(value = SpringJUnit4ClassRunner.class)     // 运行单元测试
@ContextConfiguration(value = "classpath:applicationContext.xml")   // 加载类路径下的配置文件
public class Demo5 {
    // 测试哪一个对象,把该对象注入进来,在测试环境下,可以使用注解的方式注入测试的对象
    // 按类型自动注入
    @Autowired
    private User user;

    @Test
    public void run1(){
        // 创建工厂,加载配置文件......
        // 调用对象的方法
        user.sayHello();
    }
}

(3) Pure annotation method

① On the basis of (2), replace the configuration file with a configuration class

②In the test class, load the configuration file under the class path and change to record the configuration class @ContextConfiguration(classes=configuration class name.class)

Welcome everyone, comments and points out the shortcomings.
The next section summarizes the important knowledge points of the Spring framework (3) .

Guess you like

Origin blog.csdn.net/javaScript1997/article/details/108046635