当前位置:网站首页>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
边栏推荐
- Vs code remote SSH configuration
- [SaaS examination certification] apaas_ Tencent Qianfan magic pen
- 862. triple sorting
- HTTP cache
- Quic implementation in rust --- Quinn
- Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
- RI Geng series: potential memory leak caused by abuse of mutable in Google Pb structure
- New uniapp+uniui background management uniuadmin
- Applet control version update best practices
- Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
猜你喜欢
![Buuctf misc-[bjdctf2020] Nani](/img/4e/ac6bf2f64cb68136581814da73db66.jpg)
Buuctf misc-[bjdctf2020] Nani

Performance testing -- Interpretation and practice of 16 enterprise level project framework

Performance test -- 14 detailed explanation of performance test report and precautions

Evolution history of mobile communication
![Buuctf misc-[actf freshman competition 2020]outline](/img/a4/ac9d14a69e0759d1e7c65740415bf7.jpg)
Buuctf misc-[actf freshman competition 2020]outline

Docker installs mysql5.7 and mounts the configuration file

Microservice Optimization: internal communication of microservices using grpc

Understand GB, gbdt and xgboost step by step

Small knowledge points of asset

Vulnhub DC-5
随机推荐
Buuctf misc-[bjdctf2020] Nani
Schedule tasks to periodically restart remote services or restart machines
5. concept of ruler method
Custom shapes for ugui skill learning
How to set up an H5 demo of easyplayer locally to play h265 video streams?
Call rest port to implement nailing notification
Transformation solution of digital intelligent supply chain platform for project management in engineering industry
Line to line interviewer | one side of ByteDance
Reading redis source code (V) master-slave replication and sentinel mechanism
Unity official case nightmare shooter development summary < I > realization of the role's attack function
How to batch make decreasing serial number barcode
Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
Aikuai multi dialing + load balancing overlay bandwidth
3. install and deploy Mgr cluster | explain Mgr in simple terms
Learning records - things inherited by subclass parent of C #
Analysis of resolv Conf common parameters
How to make traditional Chinese medicine labels with pictures
Reinforcement learning series (IV) -policygradient example
Handlebars dynamic adjustment
Pond sampling