当前位置:网站首页>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
边栏推荐
- Detailed explanation of online reputation management
- Docker builds MySQL master-slave
- Goframe framework (RK boot): fast implementation of server-side JWT verification
- Special exercise split line-----------------------------
- Hypervisor Necromancy; Recover kernel protector (1)
- Aikuai multi dialing + load balancing overlay bandwidth
- Essentials of fleet video playback and fleet videoplayer video playback components
- SAP WM cannot automatically obtain the special movement mark in the material master data when receiving Po goods?
- Reading redis source code (VI) multi threading of redis 6.0
- Reinforcement learning series (III) -gym introduction and examples
猜你喜欢

Spark broadcast variables and accumulators (cases attached)

Quick sorting C language code + auxiliary diagram + Notes

Microservice Optimization: internal communication of microservices using grpc

Evolution history of mobile communication

8. greed

C language series - Section 4 - arrays

Docker installs mysql5.7 and mounts the configuration file

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

Xgboost Guide

Custom shapes for ugui skill learning
随机推荐
How to make traditional Chinese medicine labels with pictures
C language series - Section 4 - arrays
Use Sakura FRP intranet penetration service to build your own website / game server
Analysis of resolv Conf common parameters
5 trends brought to us by customers
Supervisor multi process management exception automatic restart visual management
Why do I use index, query or slow?
Goframe framework (RK boot): fast implementation of CSRF verification
51. numerical arrangement
Reading redis source code (II) underlying data structure
PHP Base64 image processing Encyclopedia
Simple implementation of promise basic method
SetTimeout and setinterval execution time
6. template for integer and real number dichotomy
Section 6: basic configuration I of spingboot
Canvas draw the clock
WM view of commodity master data in SAP retail preliminary level
Operate attribute chestnut through reflection
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
Vs code remote SSH configuration