当前位置:网站首页>Autowired usage

Autowired usage

2022-06-23 02:51:00 Spicy drunk shrimp

Recommended documents :Spring @Autowired annotation _w3cschool

Usage cases

One 、 Interface , In the interface, a save Method

package com.example.autowired;

public interface UserRepository {

    void save();
}

Two 、 Implementation interface , Override methods in the interface

package com.example.autowired;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImps implements UserRepository{

    @Override
    public void save() {
        System.out.println(" Love to eat lollipops ");
    }
}

3、 ... and 、Autowired annotation , There are three forms of annotation , They are annotation fields 、 Constructors 、setting.

package com.example.autowired;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.autowired.UserRepository;

@Service
public class UserService {
//     The function of this annotation is to use Autowired To annotate the field 
//    @Autowired
//    private UserRepository userRepository;


    private final UserRepository userRepository;

//   This is to annotate the constructor 
    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void save(){
        userRepository.save();
    }
}

Four 、 Running results

package com.example.autowired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class run {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=(UserService) ctx.getBean("userService");
        userService.save();
    }
}

Need some xml To configure ,autowired It's through xml Injected .

5、 ... and 、xml To configure

<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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.example.autowired">

</context:component-scan>
</beans>

Running results

22:08:01.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:08:01.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userRepository'
22:08:01.346 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService'
22:08:01.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'userService' via constructor to bean named 'userRepository'
 Love to eat lollipops 
原网站

版权声明
本文为[Spicy drunk shrimp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201272345290077.html